def test_encode_decode_cv1(self): import cv fmts = [ cv.IPL_DEPTH_8U, cv.IPL_DEPTH_8S, cv.IPL_DEPTH_16U, cv.IPL_DEPTH_16S, cv.IPL_DEPTH_32S, cv.IPL_DEPTH_32F, cv.IPL_DEPTH_64F ] cvb_en = CvBridge() cvb_de = CvBridge() for w in range(100, 800, 100): for h in range(100, 800, 100): for f in fmts: for channels in (1, 2, 3, 4): original = cv.CreateImage((w, h), f, channels) cv.Set(original, (1, 2, 3, 4)) rosmsg = cvb_en.cv_to_imgmsg(original) newimg = cvb_de.imgmsg_to_cv(rosmsg) self.assert_( cv.GetElemType(original) == cv.GetElemType(newimg)) self.assert_( cv.GetSize(original) == cv.GetSize(newimg)) self.assert_( len(original.tostring()) == len(newimg.tostring()))
def cv_to_imgmsg(self, cvim, encoding="passthrough"): """ Convert an OpenCV :ctype:`CvArr` type (that is, an :ctype:`IplImage` or :ctype:`CvMat`) to a ROS sensor_msgs::Image message. :param cvim: An OpenCV :ctype:`IplImage` or :ctype:`CvMat` :param encoding: The encoding of the image data, one of the following strings: * ``"passthrough"`` * one of the standard strings in sensor_msgs/image_encodings.h :rtype: A sensor_msgs.msg.Image message :raises CvBridgeError: when the ``cvim`` has a type that is incompatible with ``encoding`` If encoding is ``"passthrough"``, then the message has the same encoding as the image's OpenCV type. Otherwise desired_encoding must be one of the standard image encodings This function returns a sensor_msgs::Image message on success, or raises :exc:`opencv_latest.cv_bridge.CvBridgeError` on failure. """ warnings.warn("Will be removed in Indigo. Use cv2_to_imgmsg instead", DeprecationWarning) import cv img_msg = sensor_msgs.msg.Image() (img_msg.width, img_msg.height) = cv.GetSize(cvim) if encoding == "passthrough": img_msg.encoding = self.cvtype_to_name[cv.GetElemType(cvim)] else: img_msg.encoding = encoding # Verify that the supplied encoding is compatible with the type of the OpenCV image if self.encoding_as_cvtype(encoding) != cv.GetElemType(cvim): raise CvBridgeError, "encoding specified as %s, but image has incompatible type %s" % ( encoding, self.cvtype_to_name[cv.GetElemType(cvim)]) img_msg.data = cvim.tostring() img_msg.step = len(img_msg.data) / img_msg.height return img_msg
def test_crop_oriented_boxes(self): box_num = 0 for cropped_image in oriented_bounding_boxes.cropped_oriented_boxes( "test/test_case_bbox.xml", "test/test_case.png"): box_num += 1 gold_standard = cv.LoadImage("test/test_case_%03d.png" % box_num) self.assertEquals(cv.GetElemType(cropped_image), cv.GetElemType(gold_standard)) self.assertEquals(cv.GetSize(cropped_image), cv.GetSize(gold_standard)) diff = cv.CloneImage(gold_standard) cv.AbsDiff(cropped_image, gold_standard, diff) self.assertEquals(cv.Sum(diff), (0, 0, 0, 0))
def cv_to_imgmsg(cvim, encoding = "passthrough"): try: return bridge.cv_to_imgmsg(cvim, encoding) except: img_msg = Image() (img_msg.width, img_msg.height) = cv.GetSize(cvim) if encoding == "passthrough": img_msg.encoding = bridge.cvtype_to_name[cv.GetElemType(cvim)] else: img_msg.encoding = encoding if encoding_as_cvtype(encoding) != cv.GetElemType(cvim): raise CvBridgeError, "invalid encoding" img_msg.data = cvim.tostring() img_msg.step = len(img_msg.data) / img_msg.height return img_msg
def detect(self, image): #resize the image base on the scaling parameters we've been configured with scaled_width = int(.5 + image.width * self.width_scaling) scaled_height = int(.5 + image.height * self.height_scaling) #in cvMat its row, col so height comes before width image_scaled = cv.CreateMat(scaled_height, scaled_width, cv.GetElemType(image)) cv.Resize(image, image_scaled, cv.CV_INTER_LINEAR) #Here, we'll actually call the openCV detector found, corners = cv.FindChessboardCorners(image_scaled, (self.corners_x, self.corners_y), cv.CV_CALIB_CB_ADAPTIVE_THRESH) if found: board_corners = self.get_board_corners(corners) #find the perimeter of the checkerboard perimeter = 0.0 for i in range(len(board_corners)): next = (i + 1) % 4 xdiff = board_corners[i][0] - board_corners[next][0] ydiff = board_corners[i][1] - board_corners[next][1] perimeter += math.sqrt(xdiff * xdiff + ydiff * ydiff) #estimate the square size in pixels square_size = perimeter / ((self.corners_x - 1 + self.corners_y - 1) * 2) radius = int(square_size * 0.5 + 0.5) corners = cv.FindCornerSubPix(image_scaled, corners, (radius, radius), (-1, -1), (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 30, 0.1)) if self.display: #uncomment to debug chessboard detection cv.DrawChessboardCorners(image_scaled, (self.corners_x, self.corners_y), corners, 1) cv.NamedWindow("image_scaled") cv.ShowImage("image_scaled", image_scaled) cv.WaitKey(5) object_points = None #we'll also generate the object points if the user has specified spacing if self.spacing_x != None and self.spacing_y != None: object_points = cv.CreateMat(3, self.corners_x * self.corners_y, cv.CV_32FC1) for y in range(self.corners_y): for x in range(self.corners_x): cv.SetReal2D(object_points, 0, y*self.corners_x + x, x * self.spacing_x) cv.SetReal2D(object_points, 1, y*self.corners_x + x, y * self.spacing_y) cv.SetReal2D(object_points, 2, y*self.corners_x + x, 0.0) #not sure why opencv functions return non opencv compatible datatypes... but they do so we'll convert corners_cv = cv.CreateMat(2, self.corners_x * self.corners_y, cv.CV_32FC1) for i in range(self.corners_x * self.corners_y): cv.SetReal2D(corners_cv, 0, i, corners[i][0]) cv.SetReal2D(corners_cv, 1, i, corners[i][1]) return (corners_cv, object_points) else: rospy.logdebug("Didn't find checkerboard") return (None, None)
def cv_to_imgmsg(self, cvim, encoding="passthrough"): """ Convert an OpenCV :ctype:`CvArr` type (that is, an :ctype:`IplImage` or :ctype:`CvMat`) to a ROS sensor_msgs::Image message. :param cvim: An OpenCV :ctype:`IplImage` or :ctype:`CvMat` :param encoding: The encoding of the image data, one of the following strings: * ``"passthrough"`` * ``"rgb8"`` * ``"rgba8"`` * ``"bgr8"`` * ``"bgra8"`` * ``"mono8"`` * ``"mono16"`` :rtype: A sensor_msgs.msg.Image message :raises CvBridgeError: when the ``cvim`` has a type that is incompatible with ``encoding`` If encoding is ``"passthrough"``, then the message has the same encoding as the image's OpenCV type. Otherwise encoding must be one of the strings "rgb8", "bgr8", "rgba8", "bgra8", "mono8" or "mono16", in which case the OpenCV image must have the appropriate type: ``CV_8UC3`` for "rgb8", "bgr8" ``CV_8UC4`` for "rgba8", "bgra8" ``CV_8UC1`` for "mono8" ``CV_16UC1`` for "mono16" This function returns a sensor_msgs::Image message on success, or raises :exc:`opencv_latest.cv_bridge.CvBridgeError` on failure. """ img_msg = sensor_msgs.msg.Image() (img_msg.width, img_msg.height) = cv.GetSize(cvim) if encoding == "passthrough": img_msg.encoding = self.cvtype_names[cv.GetElemType(cvim)] else: img_msg.encoding = encoding # Verify that the supplied encoding is compatible with the type of the OpenCV image if self.encoding_as_cvtype(encoding) != cv.GetElemType(cvim): raise CvBridgeError, "encoding specified as %s, but image has incompatible type %s" % ( encoding, self.cvtype_names[cv.GetElemType(cvim)]) img_msg.step = img_msg.width * cv.CV_MAT_CN(cv.GetElemType(cvim)) img_msg.data = cvim.tostring() return img_msg
def downsample_and_detect(self, img): """ Downsample the input image to approximately VGA resolution and detect the calibration target corners in the full-size image. Combines these apparently orthogonal duties as an optimization. Checkerboard detection is too expensive on large images, so it's better to do detection on the smaller display image and scale the corners back up to the correct size. Returns (scrib, corners, downsampled_corners, board, (x_scale, y_scale)). """ # Scale the input image down to ~VGA size (width, height) = cv.GetSize(img) scale = math.sqrt( (width*height) / (640.*480.) ) if scale > 1.0: scrib = cv.CreateMat(int(height / scale), int(width / scale), cv.GetElemType(img)) cv.Resize(img, scrib) else: scrib = cv.CloneMat(img) # Due to rounding, actual horizontal/vertical scaling may differ slightly x_scale = float(width) / scrib.cols y_scale = float(height) / scrib.rows if self.pattern == Patterns.Chessboard: # Detect checkerboard (ok, downsampled_corners, board) = self.get_corners(scrib, refine = True) # Scale corners back to full size image corners = None if ok: if scale > 1.0: # Refine up-scaled corners in the original full-res image # TODO Does this really make a difference in practice? corners_unrefined = [(c[0]*x_scale, c[1]*y_scale) for c in downsampled_corners] radius = int(math.ceil(scale)) if img.channels == 3: mono = cv.CreateMat(img.rows, img.cols, cv.CV_8UC1) cv.CvtColor(img, mono, cv.CV_BGR2GRAY) else: mono = img corners = cv.FindCornerSubPix(mono, corners_unrefined, (radius,radius), (-1,-1), ( cv.CV_TERMCRIT_EPS+cv.CV_TERMCRIT_ITER, 30, 0.1 )) else: corners = downsampled_corners else: # Circle grid detection is fast even on large images (ok, corners, board) = self.get_corners(img) # Scale corners to downsampled image for display downsampled_corners = None if ok: print corners if scale > 1.0: downsampled_corners = [(c[0]/x_scale, c[1]/y_scale) for c in corners] else: downsampled_corners = corners return (scrib, corners, downsampled_corners, board, (x_scale, y_scale))
def detect(self, image): #resize the image base on the scaling parameters we've been configured with scaled_width = int(.5 + image.width * self.width_scaling) scaled_height = int(.5 + image.height * self.height_scaling) #in cvMat its row, col so height comes before width image_scaled = cv.CreateMat(scaled_height, scaled_width, cv.GetElemType(image)) cv.Resize(image, image_scaled, cv.CV_INTER_LINEAR) found, corners = cv.FindChessboardCorners( image_scaled, (self.corners_x, self.corners_y), cv.CV_CALIB_CB_ADAPTIVE_THRESH) if found: rospy.logdebug("Found cb") board_corners = self.get_board_corners(corners) #find the perimeter of the checkerboard perimeter = 0.0 for i in range(len(board_corners)): next = (i + 1) % 4 xdiff = board_corners[i][0] - board_corners[next][0] ydiff = board_corners[i][1] - board_corners[next][1] perimeter += math.sqrt(xdiff * xdiff + ydiff * ydiff) #estimate the square size in pixels square_size = perimeter / ( (self.corners_x - 1 + self.corners_y - 1) * 2) radius = int(square_size * 0.5 + 0.5) corners = cv.FindCornerSubPix( image_scaled, corners, (radius, radius), (-1, -1), (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 30, 0.1)) #cv.DrawChessboardCorners(image_scaled, (self.corners_x, self.corners_y), corners, 1) #cv.NamedWindow("image_scaled") #cv.ShowImage("image_scaled", image_scaled) #cv.WaitKey() object_points = None #we'll also generate the object points if they've been requested if self.spacing_x != None and self.spacing_y != None: object_points = [None] * (self.corners_x * self.corners_y) for i in range(self.corners_y): for j in range(self.corners_x): object_points[i * self.corners_x + j] = (j * self.spacing_x, i * self.spacing_y) return (corners, object_points) else: rospy.logdebug("Didn't find checkerboard") return (None, None)
def __init__(self, process_noise_cov=(1.0, 800.0)): self.kal = cv.CreateKalman(2, 1, 0) cv.SetIdentity(self.kal.transition_matrix, 1.0) cv.SetIdentity(self.kal.measurement_matrix, 1.0) cv.SetIdentity(self.kal.process_noise_cov, 1.0) self.kal.process_noise_cov[0, 0] = process_noise_cov[0] self.kal.process_noise_cov[1, 1] = process_noise_cov[1] cv.SetIdentity(self.kal.measurement_noise_cov, 1.0) cv.SetIdentity(self.kal.error_cov_post, 1.0) self.measurement = cv.CreateMat(1, 1, cv.GetElemType(self.kal.state_pre)) self.t_previous = None
def cv_to_imgmsg(self, cvim, encoding = "passthrough"): """ Convert an OpenCV CvArr type (that is, an IplImage or CvMat) to a ROS sensor_msgs Image message. If encoding is "passthrough", then the message has the same encoding as the image's OpenCV type. Otherwise encoding must be one of the defined strings "rgb8", "bgr8", "rgba8", "bgra8", "mono8" or "mono16". In this case, the image must have the appropriate type: CV_8UC3 (for "rgb8", "bgr8"), CV_8UC4 (for "rgba8", "bgra8"), CV_8UC1 (for "mono8"), or CV_16UC1 (for "mono16"). """ img_msg = sensor_msgs.msg.Image() (img_msg.width, img_msg.height) = cv.GetSize(cvim) if encoding == "passthrough": img_msg.encoding = self.cvtype_names[cv.GetElemType(cvim)] else: img_msg.encoding = encoding # Verify that the supplied encoding is compatible with the type of the OpenCV image if self.encoding_as_cvtype(encoding) != cv.GetElemType(cvim): raise CvBridgeError, "encoding specified as %s, but image has incompatible type %s" % (encoding, self.cvtype_names[cv.GetElemType(cvim)]) img_msg.step = img_msg.width * cv.CV_MAT_CN(cv.GetElemType(cvim)) img_msg.data = cvim.tostring() return img_msg
def toBinary(img): # convert to grayscale if cv.GetElemType(img) == cv.CV_8UC3: # Create an image to store the output version on result = cv.CreateMat(img.rows, img.cols, cv.CV_8UC1) cv.CvtColor(img, result, cv.CV_RGB2GRAY) else: result = cv.CloneMat(img) # apply threshold # TODO: Make use of better thresholding algorithms thr = 230 cv.Threshold(result, result, thr, 255, cv.CV_THRESH_BINARY) return result
def cvShiftDFT(src_arr, dst_arr): size = cv.GetSize(src_arr) dst_size = cv.GetSize(dst_arr) if dst_size != size: cv.Error(cv.CV_StsUnmatchedSizes, "cv.ShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__) if (src_arr is dst_arr): tmp = cv.CreateMat(size[1] / 2, size[0] / 2, cv.GetElemType(src_arr)) cx = size[0] / 2 cy = size[1] / 2 # image center q1 = cv.GetSubRect(src_arr, (0, 0, cx, cy)) q2 = cv.GetSubRect(src_arr, (cx, 0, cx, cy)) q3 = cv.GetSubRect(src_arr, (cx, cy, cx, cy)) q4 = cv.GetSubRect(src_arr, (0, cy, cx, cy)) d1 = cv.GetSubRect(src_arr, (0, 0, cx, cy)) d2 = cv.GetSubRect(src_arr, (cx, 0, cx, cy)) d3 = cv.GetSubRect(src_arr, (cx, cy, cx, cy)) d4 = cv.GetSubRect(src_arr, (0, cy, cx, cy)) if (src_arr is not dst_arr): if (not cv.CV_ARE_TYPES_EQ(q1, d1)): cv.Error( cv.CV_StsUnmatchedFormats, "cv.ShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__) cv.Copy(q3, d1) cv.Copy(q4, d2) cv.Copy(q1, d3) cv.Copy(q2, d4) else: cv.Copy(q3, tmp) cv.Copy(q1, q3) cv.Copy(tmp, q1) cv.Copy(q4, tmp) cv.Copy(q2, q4) cv.Copy(tmp, q2)
def cvShiftDFT(src_arr, dst_arr): size = cv.GetSize(src_arr) dst_size = cv.GetSize(dst_arr) if dst_size != size: cv.Error(cv.CV_StsUnmatchedSizes, "cv.ShiftDFT", "as imagens devem ter tamanhos iguais", __FILE__, __LINE__) if (src_arr is dst_arr): tmp = cv.CreateMat(size[1] / 2, size[0] / 2, cv.GetElemType(src_arr)) cx = size[0] / 2 cy = size[1] / 2 # image center q1 = cv.GetSubRect(src_arr, (0, 0, cx, cy)) q2 = cv.GetSubRect(src_arr, (cx, 0, cx, cy)) q3 = cv.GetSubRect(src_arr, (cx, cy, cx, cy)) q4 = cv.GetSubRect(src_arr, (0, cy, cx, cy)) d1 = cv.GetSubRect(src_arr, (0, 0, cx, cy)) d2 = cv.GetSubRect(src_arr, (cx, 0, cx, cy)) d3 = cv.GetSubRect(src_arr, (cx, cy, cx, cy)) d4 = cv.GetSubRect(src_arr, (0, cy, cx, cy)) if (src_arr is not dst_arr): if (not cv.CV_ARE_TYPES_EQ(q1, d1)): cv.Error(cv.CV_StsUnmatchedFormats, "cv.ShiftDFT", "as imagens devem ter o mesmo formato", __FILE__, __LINE__) cv.Copy(q3, d1) cv.Copy(q4, d2) cv.Copy(q1, d3) cv.Copy(q2, d4) else: cv.Copy(q3, tmp) cv.Copy(q1, q3) cv.Copy(tmp, q1) cv.Copy(q4, tmp) cv.Copy(q2, q4) cv.Copy(tmp, q2)
def detect(self, msg): """ Downsample the input image to approximately VGA resolution and detect the calibration target corners in the full-size image. Combines these apparently orthogonal duties as an optimization. Checkerboard detection is too expensive on large images, so it's better to do detection on the smaller display image and scale the corners back up to the correct size. Returns (scrib, corners, downsampled_corners, board, (x_scale, y_scale)). """ mono = self.mk_gray(msg) # Scale the input image down to ~VGA size (width, height) = cv.GetSize(mono) #print "width: " + str(width) + " height: " + str(height) scale = math.sqrt( (width*height) / (640.*480.) ) if scale > 1.0: scrib = cv.CreateMat(int(height / scale), int(width / scale), cv.GetElemType(mono)) cv.Resize(mono, scrib) else: scrib = cv.CloneMat(mono) # Due to rounding, actual horizontal/vertical scaling may differ slightly x_scale = float(width) / scrib.cols y_scale = float(height) / scrib.rows (ok, downsampled_corners) = self.get_corners(scrib, refine = True) # Scale corners back to full size image if scale > 1.0: # Refine up-scaled corners in the original full-res image # TODO Does this really make a difference in practice? corners_unrefined = [(c[0]*x_scale, c[1]*y_scale) for c in downsampled_corners] radius = int(math.ceil(scale)) corners = cv.FindCornerSubPix(mono, corners_unrefined, (radius,radius), (-1,-1), ( cv.CV_TERMCRIT_EPS+cv.CV_TERMCRIT_ITER, 30, 0.1 )) else: corners = downsampled_corners return (ok, corners, mono, (x_scale, y_scale))
def __init__(self): self.initialized = False self.kal = cv.CreateKalman(4, 4, 0) cv.SetIdentity(self.kal.transition_matrix) cv.SetIdentity(self.kal.measurement_matrix) cv.SetIdentity(self.kal.process_noise_cov, 1.0) self.kal.process_noise_cov[2, 2] = 0.5 self.kal.process_noise_cov[3, 3] = 0.5 cv.SetIdentity(self.kal.measurement_noise_cov, 80.0) self.kal.measurement_noise_cov[0, 0] = 5E-5 #15.341 self.kal.measurement_noise_cov[0, 1] = 8E-6 # 2.354 self.kal.measurement_noise_cov[1, 0] = 8E-6 #1E-2 # 2.354 self.kal.measurement_noise_cov[ 1, 1] = 5E-5 # Likely to be similar to the x value. self.kal.measurement_noise_cov[2, 2] = 40.0 #0.22291 self.kal.measurement_noise_cov[3, 3] = 40.0 #0.21742 self.measurement = cv.CreateMat(4, 1, cv.GetElemType(self.kal.state_pre)) self.t = None self.zf = None
def mat_to_array(cv_mat): cvtype_to_dtype = { cv.CV_8UC1: 'uint8', cv.CV_8SC1: 'int8', cv.CV_16UC1: 'uint16', cv.CV_16SC1: 'int16', cv.CV_32SC1: 'int32', cv.CV_32FC1: 'float32', cv.CV_64FC1: 'float64', cv.CV_8UC2: 'uint8', cv.CV_8SC2: 'int8', cv.CV_16UC2: 'uint16', cv.CV_16SC2: 'int16', cv.CV_32SC2: 'int32', cv.CV_32FC2: 'float32', cv.CV_64FC2: 'float64', cv.CV_8UC3: 'uint8', cv.CV_8SC3: 'int8', cv.CV_16UC3: 'uint16', cv.CV_16SC3: 'int16', cv.CV_32SC3: 'int32', cv.CV_32FC3: 'float32', cv.CV_64FC3: 'float64', cv.CV_8UC4: 'uint8', cv.CV_8SC4: 'int8', cv.CV_16UC4: 'uint16', cv.CV_16SC4: 'int16', cv.CV_32SC4: 'int32', cv.CV_32FC4: 'float32', cv.CV_64FC4: 'float64', } ch_count = { cv.CV_8UC1: 1, cv.CV_8SC1: 1, cv.CV_16UC1: 1, cv.CV_16SC1: 1, cv.CV_32SC1: 1, cv.CV_32FC1: 1, cv.CV_64FC1: 1, cv.CV_8UC2: 2, cv.CV_8SC2: 2, cv.CV_16UC2: 2, cv.CV_16SC2: 2, cv.CV_32SC2: 2, cv.CV_32FC2: 2, cv.CV_64FC2: 2, cv.CV_8UC3: 3, cv.CV_8SC3: 3, cv.CV_16UC3: 3, cv.CV_16SC3: 3, cv.CV_32SC3: 3, cv.CV_32FC3: 3, cv.CV_64FC3: 3, cv.CV_8UC4: 4, cv.CV_8SC4: 4, cv.CV_16UC4: 4, cv.CV_16SC4: 4, cv.CV_32SC4: 4, cv.CV_32FC4: 4, cv.CV_64FC4: 4, } arrdtype = cvtype_to_dtype[cv.GetElemType(cv_mat)] ch = ch_count[cv.GetElemType(cv_mat)] a = numpy.fromstring(cv_mat.tostring(), dtype=arrdtype, count=cv_mat.rows * cv_mat.cols * ch) if cv_mat.rows == 1: row_count = ch col_count = cv_mat.cols elif cv_mat.cols == 1: row_count = cv_mat.rows col_count = ch else: row_count = cv_mat.rows col_count = cv_mat.cols a.shape = (row_count, col_count) return a
def enlarge_image(image, factor): """ Enlarge the image to the given size Image must be of type cv.cvmat""" if type(image).__name__=='cvmat': new_image = cv.CreateMat(int(round(image.height * factor)), int(round(image.width * factor)), cv.GetElemType(image)) cv.Resize(image, new_image) image = new_image logging.debug('Image has been enlarged with factor %.3f (face-detector.py)' % (factor)) return image else: logging.error('Unkown Image Type (tools.py)')
def downsize_image(image): """ Resize the image to the given size Image must be of type cv.cvmat""" height_factor = float(image.height/parameter.max_facesize[0]) width_factor = float(image.width/parameter.max_facesize[1]) if height_factor > width_factor: new_face = cv.CreateMat(image.height/height_factor, image.width/height_factor, cv.GetElemType(image)) downsize_factor = height_factor else: new_face = cv.CreateMat(int(image.height/width_factor), int(image.width/width_factor), cv.GetElemType(image)) downsize_factor = width_factor cv.Resize(image, new_face) return new_face, downsize_factor
def resizeImage(img, W, H): result = cv.CreateMat(H, W, cv.GetElemType(img)) cv.Resize(img, result) return result
def get_projector_line_associations(self): rospy.loginfo("Scanning...") positives = [] negatives = [] for i in range(self.number_of_projection_patterns): positives.append( self.get_picture_of_projection( self.predistorted_positive_projections[i])) negatives.append( self.get_picture_of_projection( self.predistorted_negative_projections[i])) rospy.loginfo("Thresholding...") strike_sum = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_32SC1) cv.SetZero(strike_sum) gray_codes = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_32SC1) cv.SetZero(gray_codes) for i in range(self.number_of_projection_patterns): difference = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_8UC1) cv.Sub(positives[i], negatives[i], difference) absolute_difference = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_8UC1) cv.AbsDiff(positives[i], negatives[i], absolute_difference) #Mark all the pixels that were "too close to call" and add them to the running total strike_mask = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_8UC1) cv.CmpS(absolute_difference, self.threshold, strike_mask, cv.CV_CMP_LT) strikes = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_32SC1) cv.Set(strikes, 1, strike_mask) cv.Add(strikes, strike_sum, strike_sum) #Set the corresponding bit in the gray_code bit_mask = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_8UC1) cv.CmpS(difference, 0, bit_mask, cv.CV_CMP_GT) bit_values = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_32SC1) cv.Set(bit_values, 2**i, bit_mask) cv.Or(bit_values, gray_codes, gray_codes) rospy.loginfo("Decoding...") # Decode every gray code into binary projector_line_associations = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_32SC1) cv.Copy(gray_codes, projector_line_associations) for i in range( cv.CV_MAT_DEPTH(cv.GetElemType(projector_line_associations)), -1, -1): projector_line_associations_bitshifted_right = cv.CreateMat( self.camera_info.height, self.camera_info.width, cv.CV_32SC1) #Using ConvertScale with a shift of -0.5 to do integer division for bitshifting right cv.ConvertScale(projector_line_associations, projector_line_associations_bitshifted_right, (2**-(2**i)), -0.5) cv.Xor(projector_line_associations, projector_line_associations_bitshifted_right, projector_line_associations) rospy.loginfo("Post processing...") # Remove all pixels that were "too close to call" more than once strikeout_mask = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_8UC1) cv.CmpS(strike_sum, 1, strikeout_mask, cv.CV_CMP_GT) cv.Set(projector_line_associations, -1, strikeout_mask) # Remove all pixels that don't decode to a valid scanline number invalid_scanline_mask = cv.CreateMat(self.camera_info.height, self.camera_info.width, cv.CV_8UC1) cv.InRangeS(projector_line_associations, 0, self.number_of_scanlines, invalid_scanline_mask) cv.Not(invalid_scanline_mask, invalid_scanline_mask) cv.Set(projector_line_associations, -1, invalid_scanline_mask) self.display_scanline_associations(projector_line_associations) return projector_line_associations
def enhanceImage(img): W, H = cv.GetSize(img) result = cv.CreateMat(H, W, cv.GetElemType(img)) cv.Smooth(img, result, smoothtype=cv.CV_BLUR, param1=3) return result