def detectObject(image): grayscale = cv.cvCreateImage(size, 8, 1) cv.cvFlip(image, None, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) cv.cvEqualizeHist(grayscale, grayscale) cascade = cv.cvLoadHaarClassifierCascade(haar_file, cv.cvSize(1, 1)) objects = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100, 100)) # Draw dots where hands are if objects: for i in objects: #cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)), # cv.cvPoint(int(i.x+i.width), int(i.y+i.height)), # cv.CV_RGB(0,255,0), 3, 8, 0) center = cv.cvPoint(int(i.x + i.width / 2), int(i.y + i.height / 2)) cv.cvCircle(image, center, 10, cv.CV_RGB(0, 0, 0), 5, 8, 0) # Left side check if center.x > box_forward_left[ 0].x and center.x < box_backwards_left[ 1].x and center.y > box_forward_left[ 0].y and center.y < box_backwards_left[1].y: set_speed('left', center) # Right side check if center.x > box_forward_right[ 0].x and center.x < box_backwards_right[ 1].x and center.y > box_forward_right[ 0].y and center.y < box_backwards_right[1].y: set_speed('right', center)
def detectObject(image): grayscale = cv.cvCreateImage(size, 8, 1) cv.cvFlip(image, None, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) cv.cvEqualizeHist(grayscale, grayscale) cascade = cv.cvLoadHaarClassifierCascade(haar_file, cv.cvSize(1,1)) objects = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100,100)) # Draw dots where hands are if objects: for i in objects: #cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)), # cv.cvPoint(int(i.x+i.width), int(i.y+i.height)), # cv.CV_RGB(0,255,0), 3, 8, 0) center = cv.cvPoint(int(i.x+i.width/2), int(i.y+i.height/2)) cv.cvCircle(image, center, 10, cv.CV_RGB(0,0,0), 5,8, 0) # Left side check if center.x > box_forward_left[0].x and center.x < box_backwards_left[1].x and center.y > box_forward_left[0].y and center.y < box_backwards_left[1].y: set_speed('left', center) # Right side check if center.x > box_forward_right[0].x and center.x < box_backwards_right[1].x and center.y > box_forward_right[0].y and center.y < box_backwards_right[1].y: set_speed('right', center)
def circ(n): pt = cv.cvPoint(int(round(n[0,0])),int(round(n[1,0]))) cv.cvCircle(self.disp.buffer, pt, size, color_scalar, cv.CV_FILLED, cv.CV_AA) pt2 = cv.cvPoint(pt.x + 2, pt.y + 2) cv.cvPutText(self.disp.buffer, text, pt, self.disp.font, cv.cvScalar(255,255,255)) cv.cvPutText(self.disp.buffer, text, pt2, self.disp.font, cv.cvScalar(50,50,50))
def circ(n): pt = cv.cvPoint(int(round(n[0, 0])), int(round(n[1, 0]))) cv.cvCircle(self.disp.buffer, pt, size, color_scalar, cv.CV_FILLED, cv.CV_AA) pt2 = cv.cvPoint(pt.x + 2, pt.y + 2) cv.cvPutText(self.disp.buffer, text, pt, self.disp.font, cv.cvScalar(255, 255, 255)) cv.cvPutText(self.disp.buffer, text, pt2, self.disp.font, cv.cvScalar(50, 50, 50))
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 draw_weighted_Pose2D(display, max_weight, particles): for p in particles: if type(p) is types.TupleType: part, weight = p rpos = part.pos else: part = p rpos = p.pos x = mt.cos(part.angle) * .07 y = mt.sin(part.angle) * .07 dir = rpos.copy() dir[0,0] = dir[0,0] + x dir[1,0] = dir[1,0] + y pos = display.to_screen(rpos) dirp = display.to_screen(dir) if type(p) is types.TupleType: color = round(255.0 * (weight/max_weight)) cv.cvCircle(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])), 2, cv.cvScalar(255, 255-color, 255), cv.CV_FILLED, cv.CV_AA) cv.cvCircle(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])), 2, cv.cvScalar(200, 200, 200), 8, cv.CV_AA) else: cv.cvCircle(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])), 2, cv.cvScalar(150, 150, 150), cv.CV_FILLED, cv.CV_AA) cv.cvLine(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])), cv.cvPoint((int) (dirp[0,0]), (int) (dirp[1,0])), cv.cvScalar(100,200,100), 1, cv.CV_AA, 0)
def clear(self): cv.cvRectangle(self.buffer, cv.cvPoint(0,0), cv.cvPoint(self.buffer.width, self.buffer.height), cv.cvScalar(255,255,255), cv.CV_FILLED) if self.draw_grid: line_color = 230 lc = cv.cvScalar(line_color,line_color,line_color) for i in xrange(1, as_int(self.meters_disp)+3): cv.cvCircle(self.buffer, cv.cvPoint(self.w/2,self.h), as_int(self.pixels_per_meter * (i-.5)), #lc, 1) lc, 1, cv.CV_AA) cv.cvCircle(self.buffer, cv.cvPoint(self.w/2,self.h), as_int(self.pixels_per_meter * i), #lc, 1) lc, 1, cv.CV_AA) for i in xrange(360/30): x = (self.w/2) + math.cos(math.radians(i*30)) * self.pixels_per_meter * (self.meters_disp+2) y = self.h + math.sin(math.radians(i*30)) * self.pixels_per_meter * (self.meters_disp+2) cv.cvLine(self.buffer, cv.cvPoint(self.w/2,self.h), cv.cvPoint(as_int(x),as_int(y)), lc, 1, cv.CV_AA) if self.draw_center: cv.cvCircle(self.buffer, cv.cvPoint(self.w/2,self.h), 3, cv.cvScalar(0,0,200), cv.CV_FILLED, cv.CV_AA)
def filter_and_render_mixed(image, corners): """ Takes a numpy array of corners and a cvMat image. """ n = 15 footprint = ones((n,n)) mx = maximum_filter(corners, footprint = footprint) local_maxima = (corners == mx) * (corners != zeros(corners.shape)) # make sure to remove completly dark points points = nonzero(local_maxima) del local_maxima points = array([points[0],points[1]]).transpose() L = [] for each in points: L.append((corners[each[0],each[1]],each[0],each[1],None)) i = cv.cvPoint(int(each[0]),int(each[1])) cv.cvCircle(image, i, 2, cv.CV_RGB(0,0,200),3 ) #cv.cvCvtColor(grayimage, image, cv.CV_GRAY2RGB) return image
def draw_weighted_Pose2D(display, max_weight, particles): for p in particles: if type(p) is types.TupleType: part, weight = p rpos = part.pos else: part = p rpos = p.pos x = mt.cos(part.angle) * 0.07 y = mt.sin(part.angle) * 0.07 dir = rpos.copy() dir[0, 0] = dir[0, 0] + x dir[1, 0] = dir[1, 0] + y pos = display.to_screen(rpos) dirp = display.to_screen(dir) if type(p) is types.TupleType: color = round(255.0 * (weight / max_weight)) cv.cvCircle( display.buffer, cv.cvPoint((int)(pos[0, 0]), (int)(pos[1, 0])), 2, cv.cvScalar(255, 255 - color, 255), cv.CV_FILLED, cv.CV_AA, ) cv.cvCircle( display.buffer, cv.cvPoint((int)(pos[0, 0]), (int)(pos[1, 0])), 2, cv.cvScalar(200, 200, 200), 8, cv.CV_AA, ) else: cv.cvCircle( display.buffer, cv.cvPoint((int)(pos[0, 0]), (int)(pos[1, 0])), 2, cv.cvScalar(150, 150, 150), cv.CV_FILLED, cv.CV_AA, ) cv.cvLine( display.buffer, cv.cvPoint((int)(pos[0, 0]), (int)(pos[1, 0])), cv.cvPoint((int)(dirp[0, 0]), (int)(dirp[1, 0])), cv.cvScalar(100, 200, 100), 1, cv.CV_AA, 0, )
def filter_and_render_mixed(image, corners): """ Takes a numpy array of corners and a cvMat image. """ n = 15 footprint = ones((n, n)) mx = maximum_filter(corners, footprint=footprint) local_maxima = (corners == mx) * (corners != zeros( corners.shape)) # make sure to remove completly dark points points = nonzero(local_maxima) del local_maxima points = array([points[0], points[1]]).transpose() L = [] for each in points: L.append((corners[each[0], each[1]], each[0], each[1], None)) i = cv.cvPoint(int(each[0]), int(each[1])) cv.cvCircle(image, i, 2, cv.CV_RGB(0, 0, 200), 3) #cv.cvCvtColor(grayimage, image, cv.CV_GRAY2RGB) return image
def draw_weighted_2D(display, max_weight, particles): for p in particles: if type(p) is types.TupleType: rpos, weight = p else: rpos = p pos = display.to_screen(rpos) if type(p) is types.TupleType: color = round(255.0 * (weight/max_weight)) cv.cvCircle(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])), 3, cv.cvScalar(255, 255-color, 255), cv.CV_FILLED, cv.CV_AA) cv.cvCircle(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])), 3, cv.cvScalar(200, 200, 200), 1, cv.CV_AA) else: cv.cvCircle(display.buffer, cv.cvPoint((int) (pos[0,0]), (int) (pos[1,0])), 2, cv.cvScalar(150, 150, 150), cv.CV_FILLED, cv.CV_AA)
def clear(self): cv.cvRectangle(self.buffer, cv.cvPoint(0, 0), cv.cvPoint(self.buffer.width, self.buffer.height), cv.cvScalar(255, 255, 255), cv.CV_FILLED) if self.draw_grid: line_color = 230 lc = cv.cvScalar(line_color, line_color, line_color) for i in xrange(1, as_int(self.meters_disp) + 3): cv.cvCircle( self.buffer, cv.cvPoint(self.w / 2, self.h), as_int(self.pixels_per_meter * (i - .5)), #lc, 1) lc, 1, cv.CV_AA) cv.cvCircle( self.buffer, cv.cvPoint(self.w / 2, self.h), as_int(self.pixels_per_meter * i), #lc, 1) lc, 1, cv.CV_AA) for i in xrange(360 / 30): x = (self.w / 2) + math.cos(math.radians( i * 30)) * self.pixels_per_meter * (self.meters_disp + 2) y = self.h + math.sin(math.radians( i * 30)) * self.pixels_per_meter * (self.meters_disp + 2) cv.cvLine(self.buffer, cv.cvPoint(self.w / 2, self.h), cv.cvPoint(as_int(x), as_int(y)), lc, 1, cv.CV_AA) if self.draw_center: cv.cvCircle(self.buffer, cv.cvPoint(self.w / 2, self.h), 3, cv.cvScalar(0, 0, 200), cv.CV_FILLED, cv.CV_AA)
def draw_point(self, x, y): cv.cvCircle(self.__image, [x,y], 3, cv.cvScalar(0, 255, 0, 0), -1, 8, 0)
for b in range (polylines_size): pt [a][b] = cv.cvPoint (random.randrange (-width, 2 * width), random.randrange (-height, 2 * height)) cv.cvFillPoly (image, pt, random_color (random), line_type, 0) highgui.cvShowImage (window_name, image) highgui.cvWaitKey (delay) # draw some circles for i in range (number): pt1 = cv.cvPoint (random.randrange (-width, 2 * width), random.randrange (-height, 2 * height)) cv.cvCircle (image, pt1, random.randrange (0, 300), random_color (random), random.randrange (-1, 9), line_type, 0) highgui.cvShowImage (window_name, image) highgui.cvWaitKey (delay) # draw some text for i in range (number): pt1 = cv.cvPoint (random.randrange (-width, 2 * width), random.randrange (-height, 2 * height)) font = cv.cvInitFont (random.randrange (0, 8), random.randrange (0, 100) * 0.05 + 0.01, random.randrange (0, 100) * 0.05 + 0.01, random.randrange (0, 5) * 0.1, random.randrange (0, 10), line_type)
#if len(centrePointeur)!=1: # printListPointeur(centrePointeur) # utilsation de ControlPeriph MouseControl.mouse_warp(souris, centrePointeur[0].x, centrePointeur[0].y, 0, 0) # envoie des coordonnees de pointeur via protocole Tuio ##if first2==0: ## sendTuioMess() first = 0 if GUI == 1: #on trace un cercle sur les pointeurs for i in range(0, len(centrePointeur)): cv.cvCircle(frame, centrePointeur[i], 5, color, 2, 6, 0) #if first2==0: # cv.cvPutText(frame, str(id_Pointeur[i]), centrePointeur[i], font, color2) cv.cvPutText(frame, str(len(centrePointeur)), cv.cvPoint(20, 30), font, color) # affiche le nombre de pointeur # message "config saved" temporaire if mess_saved < 20 and mess_saved != 0: cv.cvPutText(frame, "config saved", cv.cvPoint(20, 100), font, color) mess_saved = mess_saved + 1 if mess_saved == 19: mess_saved = 0 # we can now display the images highgui.cvShowImage('Camera', frame)
def circ(n): pt = cv.cvPoint(int(round(n[0,0])),int(round(n[1,0]))) cv.cvCircle(self.buffer, pt, size, color, cv.CV_FILLED, cv.CV_AA)
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
# generate a random point points.append (cv.cvPoint ( my_random.randrange (0, image.width / 2) + image.width / 4, my_random.randrange (0, image.width / 2) + image.width / 4 )) # compute the convex hull hull = cv.cvConvexHull2 (points, cv.CV_CLOCKWISE, 0) # start with an empty image cv.cvSetZero (image) for i in range (count): # draw all the points cv.cvCircle (image, points [i], 2, cv.cvScalar (0, 0, 255, 0), cv.CV_FILLED, cv.CV_AA, 0) # start the line from the last point pt0 = points [hull [-1]] for point_index in hull: # connect the previous point to the current one # get the current one pt1 = points [point_index] # draw cv.cvLine (image, pt0, pt1, cv.cvScalar (0, 255, 0, 0), 1, cv.CV_AA, 0)
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, [the_point.x, the_point.y], 3, cv.cvScalar(0, 255, 0, 0), -1, 8, 0) # set back the points we keep points[1] = new_points if add_remove_pt: # we want to add a point points[1].append(cv.cvPointTo32f(pt)) # refine the corner locations points[1][-1] = cv.cvFindCornerSubPix( grey, [points[1][-1]], cv.cvSize(win_size, win_size), cv.cvSize(-1, -1), cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03),
def circ(n): pt = cv.cvPoint(int(round(n[0, 0])), int(round(n[1, 0]))) cv.cvCircle(self.buffer, pt, size, color, cv.CV_FILLED, cv.CV_AA)
def render_harris_points(image, filtered_coords): """This function renders points directly on an image with opencv""" pnts = [cv.cvPoint(int(i[1]), int(i[0])) for i in filtered_coords] for pnt in pnts: cv.cvCircle(image, pnt, 2, cv.CV_RGB(0, 200, 0), 3)
#This is the line that throws the error storage = cv.cvCreateMat(50, 1, cv.CV_32FC3) cv.cvSetZero(storage) #circles = cv.cvHoughCircles(grayimage, storage, cv.CV_HOUGH_GRADIENT, 2, grayimage.height/4, 150, 40, long(sys.argv[2]), long(sys.argv[3])) #circles = cv.cvHoughCircles(grayimage, storage, cv.CV_HOUGH_GRADIENT, 1, grayimage.height, 200, 40, long(sys.argv[2]), long(sys.argv[3])) circles = cv.cvHoughCircles(grayimage, storage, cv.CV_HOUGH_GRADIENT, 1, grayimage.width, 150, 40, long(sys.argv[2]), grayimage.width) print storage for i in storage: print i[0], i[1], i[2] center = cv.cvRound(i[0]), cv.cvRound(i[1]) radius = cv.cvRound(i[2]) cv.cvCircle(image, (center), radius, cv.CV_RGB(255, 0, 0), 1, cv.CV_AA, 0 ) cv.cvCircle(image, (center), 10, cv.CV_RGB(255, 0, 0), -1, cv.CV_AA, 0 ) cv.cvCircle(cannyedges, (center), radius, cv.CV_RGB(255, 255, 255), 1, cv.CV_AA, 0 ) # for c in range(0,3): #v = cv.cvRound( cv.cvmGet(storage, i, c) ) #if v != 0.0: #print v #cv.cvSmooth(grayimage, grayimage,cv.CV_MEDIAN, 3, 3) #Display image highgui.cvShowImage ('GrayScale', grayimage) highgui.cvShowImage ('Canny', cannyedges) highgui.cvShowImage ('Image Display Window', image)
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 if add_remove_pt: # we want to add a point points [1].append (cv.cvPointTo32f (pt)) # refine the corner locations points [1][-1] = cv.cvFindCornerSubPix ( grey, [points [1][-1]], cv.cvSize (win_size, win_size), cv.cvSize (-1, -1), cv.cvTermCriteria (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS,
def plot(image, point, radius=3, color=COL_GREEN): """Plot a point on an image""" cv.cvCircle(image, point, radius, color, -1)
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 if add_remove_pt: # we want to add a point points[1].append(cv.cvPointTo32f(pt)) # refine the corner locations points[1][-1] = cv.cvFindCornerSubPix( grey, [points[1][-1]], cv.cvSize(win_size, win_size), cv.cvSize(-1, -1), cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03))[0]
def render_harris_points(image, filtered_coords): """This function renders points directly on an image with opencv""" pnts = [cv.cvPoint(int(i[1]),int(i[0])) for i in filtered_coords] for pnt in pnts: cv.cvCircle(image, pnt , 2, cv.CV_RGB(0,200,0),3 )
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, [the_point.x, the_point.y], 3, cv.cvScalar(0, 255, 0, 0), -1, 8, 0) # set back the points we keep points[1] = new_points if add_remove_pt: # we want to add a point points[1].append(cv.cvPointTo32f(pt)) # refine the corner locations points[1][-1] = cv.cvFindCornerSubPix( grey, [points[1][-1]], cv.cvSize(win_size, win_size), cv.cvSize(-1, -1), cv.cvTermCriteria(cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 20, 0.03))[0]
def timerEvent(self, ev): # Fetch a frame from the video camera frame = highgui.cvQueryFrame(self.cap) img_orig = cv.cvCreateImage(cv.cvSize(frame.width, frame.height), cv.IPL_DEPTH_8U, frame.nChannels) if (frame.origin == cv.IPL_ORIGIN_TL): cv.cvCopy(frame, img_orig) else: cv.cvFlip(frame, img_orig, 0) # Create a grey frame to clarify data img_grey = cv.cvCreateImage(cv.cvSize(img_orig.width, img_orig.height), 8, 1) cv.cvCvtColor(img_orig, img_grey, cv.CV_BGR2GRAY) # Detect objects within the frame self.faces_storage = cv.cvCreateMemStorage(0) faces = self.detect_faces(img_grey) self.circles_storage = cv.cvCreateMemStorage(0) circles = self.detect_circles(img_grey) self.squares_storage = cv.cvCreateMemStorage(0) squares = self.detect_squares(img_grey, img_orig) self.lines_storage = cv.cvCreateMemStorage(0) lines = self.detect_lines(img_grey, img_orig) # Draw faces if faces: for face in faces: pt1, pt2 = self.face_points(face) cv.cvRectangle(img_orig, pt1, pt2, cv.CV_RGB(255, 0, 0), 3, 8, 0) # Draw lines if lines: for line in lines: cv.cvLine(img_orig, line[0], line[1], cv.CV_RGB(255, 255, 0), 3, 8) # Draw circles if circles: for circle in circles: cv.cvCircle( img_orig, cv.cvPoint(cv.cvRound(circle[0]), cv.cvRound(circle[1])), cv.cvRound(circle[2]), cv.CV_RGB(0, 0, 255), 3, 8, 0) # Draw squares if squares: i = 0 while i < squares.total: pt = [] # read 4 vertices pt.append(squares[i]) pt.append(squares[i + 1]) pt.append(squares[i + 2]) pt.append(squares[i + 3]) ## draw the square as a closed polyline cv.cvPolyLine(img_orig, [pt], 1, cv.CV_RGB(0, 255, 0), 3, cv.CV_AA, 0) i += 4 # Resize the image to display properly within the window # CV_INTER_NN - nearest-neigbor interpolation, # CV_INTER_LINEAR - bilinear interpolation (used by default) # CV_INTER_AREA - resampling using pixel area relation. (preferred for image decimation) # CV_INTER_CUBIC - bicubic interpolation. img_display = cv.cvCreateImage(cv.cvSize(self.width(), self.height()), 8, 3) cv.cvResize(img_orig, img_display, cv.CV_INTER_NN) img_pil = adaptors.Ipl2PIL(img_display) s = StringIO() img_pil.save(s, "PNG") s.seek(0) q_img = QImage() q_img.loadFromData(s.read()) bitBlt(self, 0, 0, q_img)
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
def timerEvent(self, ev): # Fetch a frame from the video camera frame = highgui.cvQueryFrame(self.cap) img_orig = cv.cvCreateImage(cv.cvSize(frame.width,frame.height),cv.IPL_DEPTH_8U, frame.nChannels) if (frame.origin == cv.IPL_ORIGIN_TL): cv.cvCopy(frame, img_orig) else: cv.cvFlip(frame, img_orig, 0) # Create a grey frame to clarify data img_grey = cv.cvCreateImage(cv.cvSize(img_orig.width,img_orig.height), 8, 1) cv.cvCvtColor(img_orig, img_grey, cv.CV_BGR2GRAY) # Detect objects within the frame self.faces_storage = cv.cvCreateMemStorage(0) faces = self.detect_faces(img_grey) self.circles_storage = cv.cvCreateMemStorage(0) circles = self.detect_circles(img_grey) self.squares_storage = cv.cvCreateMemStorage(0) squares = self.detect_squares(img_grey, img_orig) self.lines_storage = cv.cvCreateMemStorage(0) lines = self.detect_lines(img_grey, img_orig) # Draw faces if faces: for face in faces: pt1, pt2 = self.face_points(face) cv.cvRectangle(img_orig, pt1, pt2, cv.CV_RGB(255,0,0), 3, 8, 0) # Draw lines if lines: for line in lines: cv.cvLine(img_orig, line[0], line[1], cv.CV_RGB(255,255,0), 3, 8) # Draw circles if circles: for circle in circles: cv.cvCircle(img_orig, cv.cvPoint(cv.cvRound(circle[0]),cv.cvRound(circle[1])),cv.cvRound(circle[2]),cv.CV_RGB(0,0,255),3,8,0) # Draw squares if squares: i = 0 while i<squares.total: pt = [] # read 4 vertices pt.append(squares[i]) pt.append(squares[i+1]) pt.append(squares[i+2]) pt.append(squares[i+3]) ## draw the square as a closed polyline cv.cvPolyLine(img_orig, [pt], 1, cv.CV_RGB(0,255,0), 3, cv.CV_AA, 0) i += 4 # Resize the image to display properly within the window # CV_INTER_NN - nearest-neigbor interpolation, # CV_INTER_LINEAR - bilinear interpolation (used by default) # CV_INTER_AREA - resampling using pixel area relation. (preferred for image decimation) # CV_INTER_CUBIC - bicubic interpolation. img_display = cv.cvCreateImage(cv.cvSize(self.width(),self.height()), 8, 3) cv.cvResize(img_orig, img_display, cv.CV_INTER_NN) img_pil = adaptors.Ipl2PIL(img_display) s = StringIO() img_pil.save(s, "PNG") s.seek(0) q_img = QImage() q_img.loadFromData(s.read()) bitBlt(self, 0, 0, q_img)
def on_trackbar1(position): global pos1 global pos2 global pos3 global pos4 global pos5 global pos6 global pos7 global img global gray global edges print print position, pos2, pos3, pos4, pos5, pos6, pos7 temp = cv.cvCloneImage(img) gray = cv.cvCreateImage(cv.cvGetSize(temp), 8, 1) edges = cv.cvCreateImage(cv.cvGetSize(temp), 8, 1) dst = cv.cvCreateImage( cv.cvSize(256,256), 8, 3 ) src = cv.cvCloneImage(img) src2 = cv.cvCreateImage( cv.cvGetSize(src), 8, 3 ); cv.cvCvtColor(img, gray, cv.CV_BGR2GRAY) cv.cvCanny(gray, edges, position, pos2, 3) cv.cvSmooth(edges, edges, cv.CV_GAUSSIAN, 9, 9) storage = cv.cvCreateMat(50, 1, cv.CV_32FC3) cv.cvSetZero(storage) try: circles = cv.cvHoughCircles(gray, storage, cv.CV_HOUGH_GRADIENT, 1, float(pos3), float(pos2), float(pos4), long(pos5),long(pos6) ) #print storage for i in storage: print "Center: ", i[0], i[1], " Radius: ", i[2] center = cv.cvRound(i[0]), cv.cvRound(i[1]) radius = cv.cvRound(i[2]) cv.cvCircle(temp, (center), radius, cv.CV_RGB(255, 0, 0), 1, cv.CV_AA, 0 ) cv.cvCircle(edges, (center), radius, cv.CV_RGB(255, 255, 255), 1, cv.CV_AA, 0 ) if radius > 200: print "Circle found over 200 Radius" center_crop_topleft = (center[0]-(radius - pos7)), (center[1]-(radius - pos7)) center_crop_bottomright = (center[0]+(radius - pos7)), (center[1]+(radius - pos7)) print "crop top left: ", center_crop_topleft print "crop bottom right: ", center_crop_bottomright center_crop = cv.cvGetSubRect(src, (center_crop_topleft[0], center_crop_topleft[1] , (center_crop_bottomright[0] - center_crop_topleft[0]), (center_crop_bottomright[1] - center_crop_topleft[1]) )) #center_crop = cv.cvGetSubRect(src, (50, 50, radius/2, radius/2)) cvShowImage( "center_crop", center_crop ) print "center_crop created" #mark found circle's center with blue point and blue circle of pos 7 radius cv.cvCircle(temp ,(center), 2, cv.CV_RGB(0, 0, 255), 3, cv.CV_AA, 0 ) cv.cvCircle(temp ,(center), (radius - pos7), cv.CV_RGB(0, 0, 255), 3, cv.CV_AA, 0 ) #cvLogPolar(src, dst, (center), 48, CV_INTER_LINEAR +CV_WARP_FILL_OUTLIERS ) #this will draw a smaller cirle outlining the center circle #pos7 = int(pos7 /2.5) #cv.cvCircle(dst ,(img_size.width-pos7, 0), 2, cv.CV_RGB(0, 0, 255), 3, cv.CV_AA, 0 ) #cv.cvLine(dst, (img_size.width-pos7-1, 0), (img_size.width-pos7-1, img_size.height), cv.CV_RGB(0, 0, 255),1,8,0) #cvShowImage( "log-polar", dst ) #print radius, (radius-pos7) #cropped = cv.cvCreateImage( (pos7, img_size.height), 8, 3) #cropped2 = cv.cvCreateImage( (pos7, img_size.height), 8, 3) #coin_edge_img = cv.cvGetSubRect(dst, (img_size.width-pos7, 0, pos7 ,img_size.height )) #to create the center cropped part of coin #img_size = cvGetSize(scr) #cvCopy(coin_edge_img, cropped) #cvSaveImage("temp.png", cropped) #im = Image.open("temp.png").rotate(90) #print "pil image size = ", im.size[0], im.size[1] #im = im.resize((im.size[0]*2, im.size[1]*2)) #print "pil image size = ", im.size #im.show() #im.save("temp2.png") cropped2 = highgui.cvLoadImage("temp2.png") #cvShowImage( "cropped", cropped2) except: print "Exception:", sys.exc_info()[0] print position, pos2, pos3, pos4, pos5, pos6, pos7 pass highgui.cvShowImage("edges", edges) #cvShowImage( "log-polar", dst ) cvShowImage(wname, temp)