def get_nearest_feature( image, this_point, n=2000 ): """ Get the n-nearest features to a specified image coordinate. Features are determined using cvGoodFeaturesToTrack. """ _red = cv.cvScalar (0, 0, 255, 0); _green = cv.cvScalar (0, 255, 0, 0); _blue = cv.cvScalar (255,0,0,0); _white = cv.cvRealScalar (255) _black = cv.cvRealScalar (0) quality = 0.01 min_distance = 4 N_best = n win_size = 11 grey = cv.cvCreateImage (cv.cvGetSize (image), 8, 1) eig = cv.cvCreateImage (cv.cvGetSize (image), 32, 1) temp = cv.cvCreateImage (cv.cvGetSize (image), 32, 1) # create a grey version of the image cv.cvCvtColor ( image, grey, cv.CV_BGR2GRAY) points = cv.cvGoodFeaturesToTrack ( grey, eig, temp, N_best, quality, min_distance, None, 3, 0, 0.04) # refine the corner locations better_points = cv.cvFindCornerSubPix ( grey, points, cv.cvSize (win_size, win_size), cv.cvSize (-1, -1), cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) eigs = [] for i in range(len(points)): eigs.append(cv.cvGetMat(eig)[int(points[i].y)][int(points[i].x)]) mypoints = np.matrix(np.zeros((len(points)*2),dtype=float)).reshape(len(points),2) dists = [] for i,point in enumerate(points): mypoints[i,0]=point.x mypoints[i,1]=point.y dists.append( np.linalg.norm(mypoints[i,:]-this_point) ) dists = np.array(dists) sorteddists = dists.argsort() cv.cvDrawCircle ( image, points[ sorteddists[0] ], 5, _green, 2, 8, 0 ) return better_points[ sorteddists[0] ]
def get_nearest_feature(image, this_point, n=2000): """ Get the n-nearest features to a specified image coordinate. Features are determined using cvGoodFeaturesToTrack. """ _red = cv.cvScalar(0, 0, 255, 0) _green = cv.cvScalar(0, 255, 0, 0) _blue = cv.cvScalar(255, 0, 0, 0) _white = cv.cvRealScalar(255) _black = cv.cvRealScalar(0) quality = 0.01 min_distance = 4 N_best = n win_size = 11 grey = cv.cvCreateImage(cv.cvGetSize(image), 8, 1) eig = cv.cvCreateImage(cv.cvGetSize(image), 32, 1) temp = cv.cvCreateImage(cv.cvGetSize(image), 32, 1) # create a grey version of the image cv.cvCvtColor(image, grey, cv.CV_BGR2GRAY) points = cv.cvGoodFeaturesToTrack(grey, eig, temp, N_best, quality, min_distance, None, 3, 0, 0.04) # refine the corner locations better_points = cv.cvFindCornerSubPix( grey, points, cv.cvSize(win_size, win_size), cv.cvSize(-1, -1), cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) eigs = [] for i in range(len(points)): eigs.append(cv.cvGetMat(eig)[int(points[i].y)][int(points[i].x)]) mypoints = np.matrix(np.zeros((len(points) * 2), dtype=float)).reshape(len(points), 2) dists = [] for i, point in enumerate(points): mypoints[i, 0] = point.x mypoints[i, 1] = point.y dists.append(np.linalg.norm(mypoints[i, :] - this_point)) dists = np.array(dists) sorteddists = dists.argsort() cv.cvDrawCircle(image, points[sorteddists[0]], 5, _green, 2, 8, 0) return better_points[sorteddists[0]]
def __FindCorner(self, filename): #find the corners of images, and save all corner points in self.vKeyPoints self.img = highgui.cvLoadImage (filename) greyimg = cv.cvCreateImage(cv.cvSize(self.img.width, self.img.height), 8,1) hsvimg = cv.cvCreateImage(cv.cvGetSize(self.img), 8, 3) cv.cvCvtColor(self.img, hsvimg, cv.CV_RGB2HSV) cv.cvCvtColor (hsvimg, greyimg, cv.CV_BGR2GRAY) eigImage = cv.cvCreateImage(cv.cvGetSize(greyimg), cv.IPL_DEPTH_32F, 1) tempImage = cv.cvCreateImage(cv.cvGetSize(greyimg), cv.IPL_DEPTH_32F, 1) self.points = cv.cvGoodFeaturesToTrack(greyimg, eigImage,tempImage, 2000, 0.01, 5, None, 3,0,0.01 ) self.points2 = cv.cvFindCornerSubPix(greyimg, self.points,cv.cvSize(20, 20), cv.cvSize(-1, -1), cv.cvTermCriteria(cv.CV_TERMCRIT_ITER |cv.CV_TERMCRIT_EPS, 20, 0.03)) cv.cvReleaseImage(eigImage) cv.cvReleaseImage(tempImage)
def HarrisPoints(self, imgfile): self.points = [] self.drawimg = highgui.cvLoadImage (imgfile) c = 1 try: gray = cv.cvCreateImage (cv.cvGetSize (self.drawimg), 8, 1) cv.cvCvtColor(self.drawimg, gray, cv.CV_BGR2GRAY) eig = cv.cvCreateImage (cv.cvGetSize (self.drawimg), 32, 1) tmpimg = cv.cvCreateImage (cv.cvGetSize (self.drawimg), 32, 1) p = cv.cvGoodFeaturesToTrack(gray, eig, tmpimg, 100, 0.1, 20, None, 7, 1, 0.04 ) for x in p: cv.cvCircle( self.drawimg, x, 3, cv.CV_RGB(0,255,0), 8, 0 ); self.points.append(x) except Exception,e: print e print 'ERROR: problem handling '+ imgfile
def HarrisPoints(self, imgfile): self.points = [] self.drawimg = highgui.cvLoadImage(imgfile) c = 1 try: gray = cv.cvCreateImage(cv.cvGetSize(self.drawimg), 8, 1) cv.cvCvtColor(self.drawimg, gray, cv.CV_BGR2GRAY) eig = cv.cvCreateImage(cv.cvGetSize(self.drawimg), 32, 1) tmpimg = cv.cvCreateImage(cv.cvGetSize(self.drawimg), 32, 1) p = cv.cvGoodFeaturesToTrack(gray, eig, tmpimg, 100, 0.1, 20, None, 7, 1, 0.04) for x in p: cv.cvCircle(self.drawimg, x, 3, cv.CV_RGB(0, 255, 0), 8, 0) self.points.append(x) except Exception, e: print e print 'ERROR: problem handling ' + imgfile
def getGoodFeatures(image): # XXX: BETA but working # TODO: Clean up!! Comments properly # This is mostly copy/paster from /usr/share/opencv/samples/python/lkdemo.py """Find features using OpenCV's cvFindGoodFeatures""" win_size = 10 MAX_COUNT = 500 # create the images we need grey = cv.cvCreateImage (cv.cvGetSize (image), 8, 1) eig = cv.cvCreateImage (cv.cvGetSize (grey), 32, 1) temp = cv.cvCreateImage (cv.cvGetSize (grey), 32, 1) # Make a b/w copy cv.cvCvtColor(image, grey, cv.CV_BGR2GRAY) # the default parameters quality = 0.01 min_distance = 10 # search the good points points = cv.cvGoodFeaturesToTrack ( grey, eig, temp, MAX_COUNT, quality, min_distance, None, 3, 1, 0.04) # refine the corner locations cv.cvFindCornerSubPix ( grey, points, cv.cvSize (win_size, win_size), cv.cvSize (-1, -1), cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) # release the temporary images for good meassure cv.cvReleaseImage (eig) cv.cvReleaseImage (temp) cv.cvReleaseImage (grey) return points
cv.cvCopy (frame, image) # create a grey version of the image cv.cvCvtColor (image, grey, cv.CV_BGR2GRAY) if night_mode: # night mode: only display the points cv.cvSetZero (image) if need_to_init: # we want to search all the good points # create the wanted images # search the good points points [1] = cv.cvGoodFeaturesToTrack ( grey, eig, temp, MAX_COUNT, quality, min_distance, None, 3, 0, 0.04) # refine the corner locations cv.cvFindCornerSubPix ( grey, points [1], cv.cvSize (win_size, win_size), cv.cvSize (-1, -1), cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) elif len (points [0]) > 0: # we have points, so display them # calculate the optical flow [points [1], status], something = cv.cvCalcOpticalFlowPyrLK (
def harrisResponse(frame): """pyvision/point/DetectorHarris.py Runs at 10.5 fps... """ #gray = cv.cvCreateImage( cv.cvGetSize(image), 8, 1 ) #corners = cv.cvCreateImage( cv.cvGetSize(image), 32, 1 ) #cv.cvCvtColor( image, gray, cv.CV_BGR2GRAY ) #cv.cvCornerHarris(gray,corners,15) # This could be done in a persistant way # create the images we need image = cv.cvCreateImage(cv.cvGetSize(frame), 8, 3) grey = cv.cvCreateImage(cv.cvGetSize(frame), 8, 1) prev_grey = cv.cvCreateImage(cv.cvGetSize(frame), 8, 1) pyramid = cv.cvCreateImage(cv.cvGetSize(frame), 8, 1) prev_pyramid = cv.cvCreateImage(cv.cvGetSize(frame), 8, 1) eig = cv.cvCreateImage(cv.cvGetSize(frame), cv.IPL_DEPTH_32F, 1) temp = cv.cvCreateImage(cv.cvGetSize(frame), cv.IPL_DEPTH_32F, 1) points = [[], []] # copy the frame, so we can draw on it cv.cvCopy(frame, image) # create a grey version of the image cv.cvCvtColor(image, grey, cv.CV_BGR2GRAY) # search the good points points[1] = cv.cvGoodFeaturesToTrack(grey, eig, temp, MAX_COUNT, quality, min_distance, None, 3, 0, 0.04) # refine the corner locations cv.cvFindCornerSubPix( grey, points[1], cv.cvSize(win_size, win_size), cv.cvSize(-1, -1), cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) if len(points[0]) > 0: # we have points, so display them # calculate the optical flow [points[1], status], something = cv.cvCalcOpticalFlowPyrLK( prev_grey, grey, prev_pyramid, pyramid, points[0], len(points[0]), (win_size, win_size), 3, len(points[0]), None, cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03), flags) # initializations point_counter = -1 new_points = [] for the_point in points[1]: # go trough all the points # increment the counter point_counter += 1 if add_remove_pt: # we have a point to add, so see if it is close to # another one. If yes, don't use it dx = pt.x - the_point.x dy = pt.y - the_point.y if dx * dx + dy * dy <= 25: # too close add_remove_pt = 0 continue if not status[point_counter]: # we will disable this point continue # this point is a correct point new_points.append(the_point) # draw the current point cv.cvCircle(image, cv.cvPointFrom32f(the_point), 3, cv.cvScalar(0, 255, 0, 0), -1, 8, 0) # set back the points we keep points[1] = new_points # swapping prev_grey, grey = grey, prev_grey prev_pyramid, pyramid = pyramid, prev_pyramid points[0], points[1] = points[1], points[0] return image
cv.cvSetZero(image) if need_to_init: # we want to search all the good points # create the wanted images eig = cv.cvCreateImage(cv.cvGetSize(grey), 32, 1) temp = cv.cvCreateImage(cv.cvGetSize(grey), 32, 1) # the default parameters quality = 0.01 min_distance = 10 # search the good points points[1] = cv.cvGoodFeaturesToTrack(grey, eig, temp, MAX_COUNT, quality, min_distance, None, 3, 0, 0.04) # refine the corner locations cv.cvFindCornerSubPix( grey, points[1], cv.cvSize(win_size, win_size), cv.cvSize(-1, -1), cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) # release the temporary images cv.cvReleaseImage(eig) cv.cvReleaseImage(temp) elif len(points[0]) > 0: # we have points, so display them
def harrisResponse(frame): """pyvision/point/DetectorHarris.py Runs at 10.5 fps... """ #gray = cv.cvCreateImage( cv.cvGetSize(image), 8, 1 ) #corners = cv.cvCreateImage( cv.cvGetSize(image), 32, 1 ) #cv.cvCvtColor( image, gray, cv.CV_BGR2GRAY ) #cv.cvCornerHarris(gray,corners,15) # This could be done in a persistant way # create the images we need image = cv.cvCreateImage (cv.cvGetSize (frame), 8, 3) grey = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1) prev_grey = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1) pyramid = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1) prev_pyramid = cv.cvCreateImage (cv.cvGetSize (frame), 8, 1) eig = cv.cvCreateImage (cv.cvGetSize (frame), cv.IPL_DEPTH_32F, 1) temp = cv.cvCreateImage (cv.cvGetSize (frame), cv.IPL_DEPTH_32F, 1) points = [[], []] # copy the frame, so we can draw on it cv.cvCopy (frame, image) # create a grey version of the image cv.cvCvtColor (image, grey, cv.CV_BGR2GRAY) # search the good points points [1] = cv.cvGoodFeaturesToTrack ( grey, eig, temp, MAX_COUNT, quality, min_distance, None, 3, 0, 0.04) # refine the corner locations cv.cvFindCornerSubPix ( grey, points [1], cv.cvSize (win_size, win_size), cv.cvSize (-1, -1), cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03)) if len (points [0]) > 0: # we have points, so display them # calculate the optical flow [points [1], status], something = cv.cvCalcOpticalFlowPyrLK ( prev_grey, grey, prev_pyramid, pyramid, points [0], len (points [0]), (win_size, win_size), 3, len (points [0]), None, cv.cvTermCriteria (cv.CV_TERMCRIT_ITER|cv.CV_TERMCRIT_EPS, 20, 0.03), flags) # initializations point_counter = -1 new_points = [] for the_point in points [1]: # go trough all the points # increment the counter point_counter += 1 if add_remove_pt: # we have a point to add, so see if it is close to # another one. If yes, don't use it dx = pt.x - the_point.x dy = pt.y - the_point.y if dx * dx + dy * dy <= 25: # too close add_remove_pt = 0 continue if not status [point_counter]: # we will disable this point continue # this point is a correct point new_points.append (the_point) # draw the current point cv.cvCircle (image, cv.cvPointFrom32f(the_point), 3, cv.cvScalar (0, 255, 0, 0), -1, 8, 0) # set back the points we keep points [1] = new_points # swapping prev_grey, grey = grey, prev_grey prev_pyramid, pyramid = pyramid, prev_pyramid points [0], points [1] = points [1], points [0] return image