iCub-main
python_imaging.py
Go to the documentation of this file.
1 import numpy
2 import yarp
3 import scipy.ndimage
4 import matplotlib.pylab
5 
6 # Initialise YARP
7 yarp.Network.init()
8 
9 
11  # Create the array with random data
12  img_array = numpy.random.uniform(0., 255., (240, 320)).astype(numpy.float32)
13 
14  # Create the yarp image and wrap it around the array
15  yarp_image = yarp.ImageFloat()
16  yarp_image.setExternal(img_array, img_array.shape[1], img_array.shape[0])
17 
18  # Alternatively, if using Python 2.x, try:
19  # yarp_image.setExternal(img_array.__array_interface__['data'][0], img_array.shape[1], img_array.shape[0])
20 
21  # Create the yarp port, connect it to the running instance of yarpview and send the image
22  output_port = yarp.Port()
23  output_port.open("/python-image-port")
24  yarp.Network.connect("/python-image-port", "/view01")
25  output_port.write(yarp_image)
26 
27  # Cleanup
28  output_port.close()
29 
30 
32  # Create a port and connect it to the iCub simulator virtual camera
33  input_port = yarp.Port()
34  input_port.open("/python-image-port")
35  yarp.Network.connect("/icubSim/cam", "/python-image-port")
36 
37  # Create numpy array to receive the image and the YARP image wrapped around it
38  img_array = numpy.zeros((240, 320, 3), dtype=numpy.uint8)
39  yarp_image = yarp.ImageRgb()
40  yarp_image.resize(320, 240)
41  yarp_image.setExternal(img_array, img_array.shape[1], img_array.shape[0])
42 
43  # Alternatively, if using Python 2.x, try:
44  # yarp_image.setExternal(img_array.__array_interface__['data'][0], img_array.shape[1], img_array.shape[0])
45 
46  # Read the data from the port into the image
47  input_port.read(yarp_image)
48 
49  # display the image that has been read
50  matplotlib.pylab.imshow(img_array)
51 
52  # Cleanup
53  input_port.close()
54 
55 
57  """Object that reads an image from a port, transforms it and sends the result to another port.
58 
59  The object reads RGB images and sends monochrome ones with calculated sobel filter.
60  Note: this class uses the alternative implementation of setExternal together with numpy array interface.
61  """
62 
63  def __init__(self, input_port_name, output_port_name):
64  # Prepare ports
65  self._input_port_input_port = yarp.Port()
66  self._input_port_name_input_port_name = input_port_name
67  self._input_port_input_port.open(self._input_port_name_input_port_name)
68 
69  self._output_port_output_port = yarp.Port()
70  self._output_port_name_output_port_name = output_port_name
71  self._output_port_output_port.open(self._output_port_name_output_port_name)
72 
73  # Prepare image buffers
74  # Input
75  self._input_buf_image_input_buf_image = yarp.ImageRgb()
76  self._input_buf_image_input_buf_image.resize(320, 240)
77  self._input_buf_array_input_buf_array = numpy.zeros((240, 320, 3), dtype = numpy.uint8)
78  self._input_buf_image_input_buf_image.setExternal(self._input_buf_array_input_buf_array.__array_interface__['data'][0], self._input_buf_array_input_buf_array.shape[1], self._input_buf_array_input_buf_array.shape[0])
79  # Output
80  self._output_buf_image_output_buf_image = yarp.ImageFloat()
81  self._output_buf_image_output_buf_image.resize(320, 240)
82  self._output_buf_array_output_buf_array = numpy.zeros((240, 320), dtype = numpy.float32)
83  self._output_buf_image_output_buf_image.setExternal(self._output_buf_array_output_buf_array.__array_interface__['data'][0], self._output_buf_array_output_buf_array.shape[1], self._output_buf_array_output_buf_array.shape[0])
84 
85  def run(self):
86  while True: # Break this by hitting CTRL-C
87  # Read an image from the port
88  self._input_port_input_port.read(self._input_buf_image_input_buf_image)
89 
90  # Make sure the image has not been re-allocated
91  assert self._input_buf_array_input_buf_array.__array_interface__['data'][0] == self._input_buf_image_input_buf_image.getRawImage().__long__()
92 
93  # Let's calculate the sobel filter
94  self._output_buf_array_output_buf_array[:,:] = scipy.ndimage.filters.sobel(self._input_buf_array_input_buf_array.mean(2) / 255.)
95  # Normalise the result
96  self._output_buf_array_output_buf_array -= self._output_buf_array_output_buf_array.min()
97  self._output_buf_array_output_buf_array /= self._output_buf_array_output_buf_array.max()
98  self._output_buf_array_output_buf_array *= 255.
99 
100  # Send the result to the output port
101  self._output_port_output_port.write(self._output_buf_image_output_buf_image)
102 
103  def cleanup(self):
104  self._input_port_input_port.close()
105  self._output_port_output_port.close()
106 
107 
108 if __name__=="__main__":
109  # Example demonstrating how to use the SobelFilter class implemented above
110  # This assumes iCub simulator is running with world camera at /icubSim/cam
111  # and an instance of yarpview with port name /view01
112 
113  image_filter = SobelFilter("/sobel:in", "/sobel:out")
114 
115  try:
116  assert yarp.Network.connect("/sobel:out", "/view01")
117  assert yarp.Network.connect("/icubSim/cam", "/sobel:in")
118 
119  image_filter.run()
120  finally:
121  image_filter.cleanup()
def __init__(self, input_port_name, output_port_name)
bool read(yarp::os::Searchable &cfgtotal, pc104Data &pc104data)
Definition: ethParser.cpp:92
const FSC max
Definition: strain.h:48
const FSC min
Definition: strain.h:49
bool write(const std::string filename, const FullRegulation &reg)