Skip to content

Commit cc5ff68

Browse files
Update leap_listener.py
1 parent 17a3370 commit cc5ff68

File tree

1 file changed

+1
-89
lines changed

1 file changed

+1
-89
lines changed

leap_listener.py

+1-89
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class LeapEventListener(Leap.Listener):
1616
Subclass of Leap.Listener implementing the callback functions for some interesting events
1717
'''
1818

19-
maps_initialized = False
20-
2119
def on_init(self, controller):
2220
maps_initialized = False
2321
print("Controller initialized")
@@ -48,91 +46,5 @@ def on_images(self, controller):
4846
images = controller.images
4947
left_image = images[0]
5048
right_image = images[1]
49+
# you can correct the distortion here...
5150

52-
if left_image.is_valid and right_image.is_valid:
53-
#print("left_image w - h: ", left_image.width, " - ", left_image.height) # 640 - 240
54-
## Display distorted raw images
55-
# cv2.imshow('Left image', self.get_image_as_numpy_array(left_image))
56-
# cv2.imshow('Right image', self.get_image_as_numpy_array(right_image))
57-
58-
if not self.maps_initialized: # The following code doesn’t handle the cases where the distortion maps can change
59-
print("Converting distortion maps...")
60-
left_coordinates, left_coefficients = self.convert_distortion_maps(left_image)
61-
right_coordinates, right_coefficients = self.convert_distortion_maps(right_image)
62-
maps_initialized = True
63-
64-
print("Start computing undistorted images...")
65-
undistorted_left = self.undistort(left_image, left_coordinates, left_coefficients, 400, 400)
66-
undistorted_right = self.undistort(right_image, right_coordinates, right_coefficients, 400, 400)
67-
68-
# display images
69-
cv2.imshow('Left Camera', undistorted_left)
70-
cv2.imshow('Right Camera', undistorted_right)
71-
72-
if cv2.waitKey(1) & 0xFF == ord('q'):
73-
#closing all open windows
74-
cv2.destroyAllWindows()
75-
76-
def get_image_as_numpy_array(self, image):
77-
'''
78-
Get the numpy array related to the leap motion infrared image data
79-
It is useful to show the image (e.g., using the cv2.imshow method)
80-
'''
81-
image_buffer_ptr = image.data_pointer # pointer to the image data buffer to directly access the memory in the data buffer using ctypes or numpy
82-
ctype_array_def = ctypes.c_ubyte * image.width * image.height
83-
84-
# as ctypes array
85-
as_ctype_array = ctype_array_def.from_address(int(image_buffer_ptr))
86-
# as numpy array
87-
as_numpy_array = np.ctypeslib.as_array(as_ctype_array)
88-
return as_numpy_array
89-
90-
def convert_distortion_maps(self, image):
91-
distortion_length = image.distortion_width * image.distortion_height
92-
xmap = np.zeros(distortion_length//2, dtype=np.float32)
93-
ymap = np.zeros(distortion_length//2, dtype=np.float32)
94-
95-
for i in range(0, distortion_length, 2):
96-
xmap[distortion_length//2 - i//2 - 1] = image.distortion[i] * image.width
97-
ymap[distortion_length//2 - i//2 - 1] = image.distortion[i + 1] * image.height
98-
99-
xmap = np.reshape(xmap, (image.distortion_height, image.distortion_width//2))
100-
ymap = np.reshape(ymap, (image.distortion_height, image.distortion_width//2))
101-
102-
#resize the distortion map to equal desired destination image size
103-
resized_xmap = cv2.resize(xmap,
104-
(image.width, image.height),
105-
0, 0,
106-
cv2.INTER_LINEAR)
107-
resized_ymap = cv2.resize(ymap,
108-
(image.width, image.height),
109-
0, 0,
110-
cv2.INTER_LINEAR)
111-
112-
#Use faster fixed point maps
113-
coordinate_map, interpolation_coefficients = cv2.convertMaps(resized_xmap,
114-
resized_ymap,
115-
cv2.CV_32FC1,
116-
nninterpolation = False)
117-
118-
return coordinate_map, interpolation_coefficients
119-
120-
def undistort(self, image, coordinate_map, coefficient_map, width, height):
121-
destination = np.empty((width, height), dtype = np.ubyte)
122-
123-
#wrap image data in numpy array
124-
as_numpy_array = self.get_image_as_numpy_array(image)
125-
img = np.reshape(as_numpy_array, (image.height, image.width))
126-
127-
#remap image to destination
128-
destination = cv2.remap(img,
129-
coordinate_map,
130-
coefficient_map,
131-
interpolation = cv2.INTER_LINEAR)
132-
133-
#resize output to desired destination size
134-
destination = cv2.resize(destination,
135-
(width, height),
136-
0, 0,
137-
cv2.INTER_LINEAR)
138-
return destination

0 commit comments

Comments
 (0)