def __findcurve(self, img): storage = cv.cvCreateMemStorage(0) nb_contours, cont = cv.cvFindContours(img, storage, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint(0, 0)) cidx = int(random.random() * len(color)) if (self.drawcontour): cv.cvDrawContours(self.drawimg, cont, _white, _white, 1, 1, cv.CV_AA, cv.cvPoint(0, 0)) idx = 0 for c in cont.hrange(): PointArray = cv.cvCreateMat(1, c.total, cv.CV_32SC2) PointArray2D32f = cv.cvCreateMat(1, c.total, cv.CV_32FC2) cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)) fpoints = [] for i in range(c.total): kp = myPoint() kp.x = cv.cvGet2D(PointArray, 0, i)[0] kp.y = cv.cvGet2D(PointArray, 0, i)[1] kp.index = idx idx += 1 fpoints.append(kp) self.allcurve.append(fpoints) self.curvelength = idx
def on_trackbar (position): # create the image for putting in it the founded contours contours_image = cv.cvCreateImage (cv.cvSize (_SIZE, _SIZE), 8, 3) # compute the real level of display, given the current position levels = position - 3 # initialisation _contours = contours if levels <= 0: # zero or negative value # => get to the nearest face to make it look more funny _contours = contours.h_next.h_next.h_next # first, clear the image where we will draw contours cv.cvSetZero (contours_image) # draw contours in red and green cv.cvDrawContours (contours_image, _contours, _red, _green, levels, 3, cv.CV_AA, cv.cvPoint (0, 0)) # finally, show the image highgui.cvShowImage ("contours", contours_image)
def on_trackbar(position): # create the image for putting in it the founded contours contours_image = cv.cvCreateImage(cv.cvSize(_SIZE, _SIZE), 8, 3) # compute the real level of display, given the current position levels = position - 3 # initialisation _contours = contours if levels <= 0: # zero or negative value # => get to the nearest face to make it look more funny _contours = contours.h_next.h_next.h_next # first, clear the image where we will draw contours cv.cvSetZero(contours_image) # draw contours in red and green cv.cvDrawContours(contours_image, _contours, _red, _green, levels, 3, cv.CV_AA, cv.cvPoint(0, 0)) # finally, show the image highgui.cvShowImage("contours", contours_image)
def __findcurve(self, img): storage = cv.cvCreateMemStorage(0) nb_contours, cont = cv.cvFindContours (img, storage, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint (0,0)) cidx = int(random.random() * len(color)) if (self.drawcontour): cv.cvDrawContours (self.drawimg, cont, _white, _white, 1, 1, cv.CV_AA, cv.cvPoint (0, 0)) idx = 0 for c in cont.hrange(): PointArray = cv.cvCreateMat(1, c.total, cv.CV_32SC2) PointArray2D32f= cv.cvCreateMat( 1, c.total , cv.CV_32FC2) cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)) fpoints = [] for i in range(c.total): kp = myPoint() kp.x = cv.cvGet2D(PointArray,0, i)[0] kp.y = cv.cvGet2D(PointArray,0, i)[1] kp.index = idx idx += 1 fpoints.append(kp) self.allcurve.append(fpoints) self.curvelength = idx
def __findContour(self, filename): #find the contour of images, and save all points in self.vKeyPoints self.img = highgui.cvLoadImage (filename) self.grayimg = cv.cvCreateImage(cv.cvSize(self.img.width, self.img.height), 8,1) self.drawimg = cv.cvCreateImage(cv.cvSize(self.img.width, self.img.height), 8,3) cv.cvCvtColor (self.img, self.grayimg, cv.CV_BGR2GRAY) cv.cvSmooth(self.grayimg, self.grayimg, cv.CV_BLUR, 9) cv.cvSmooth(self.grayimg, self.grayimg, cv.CV_BLUR, 9) cv.cvSmooth(self.grayimg, self.grayimg, cv.CV_BLUR, 9) cv.cvThreshold( self.grayimg, self.grayimg, self.threshold, self.threshold +100, cv.CV_THRESH_BINARY ) cv.cvZero(self.drawimg) storage = cv.cvCreateMemStorage(0) nb_contours, cont = cv.cvFindContours (self.grayimg, storage, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint (0,0)) cv.cvDrawContours (self.drawimg, cont, cv.cvScalar(255,255,255,0), cv.cvScalar(255,255,255,0), 1, 1, cv.CV_AA, cv.cvPoint (0, 0)) self.allcurve = [] idx = 0 for c in cont.hrange(): PointArray = cv.cvCreateMat(1, c.total , cv.CV_32SC2) PointArray2D32f= cv.cvCreateMat( 1, c.total , cv.CV_32FC2) cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)) fpoints = [] for i in range(c.total): kp = myPoint() kp.x = cv.cvGet2D(PointArray,0, i)[0] kp.y = cv.cvGet2D(PointArray,0, i)[1] kp.index = idx idx += 1 fpoints.append(kp) self.allcurve.append(fpoints) self.curvelength = idx
def printContour(contour, position, color, imagen): # print " a contour " + str(contour.total) + " area: " + str(cvContourArea(contour)) + " perimeter: " + str(cvContourPerimeter(contour)) # compute the real level of display, given the current position levels = position - 3 # draw contours in red and green cv.cvDrawContours(imagen, contour, color, _green, levels, CONTOUR_THICKNESS, cv.CV_AA, cv.cvPoint(0, 0))
def printContour(contour, position, color, imagen): #print " a contour " + str(contour.total) + " area: " + str(cvContourArea(contour)) + " perimeter: " + str(cvContourPerimeter(contour)) # compute the real level of display, given the current position levels = position - 3 # draw contours in red and green cv.cvDrawContours(imagen, contour, color, _green, levels, CONTOUR_THICKNESS, cv.CV_AA, cv.cvPoint(0, 0))
def process_image(slider_pos): """ Define trackbar callback functon. This function find contours, draw it and approximate it by ellipses. """ stor = cv.cvCreateMemStorage(0) # Threshold the source image. This needful for cv.cvFindContours(). cv.cvThreshold(image03, image02, slider_pos, 255, cv.CV_THRESH_BINARY) # Find all contours. nb_contours, cont = cv.cvFindContours(image02, stor, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint(0, 0)) # Clear images. IPL use. cv.cvZero(image02) cv.cvZero(image04) # This cycle draw all contours and approximate it by ellipses. for c in cont.hrange(): count = c.total # This is number point in contour # Number point must be more than or equal to 6 (for cv.cvFitEllipse_32f). if (count < 6): continue # Alloc memory for contour point set. PointArray = cv.cvCreateMat(1, count, cv.CV_32SC2) PointArray2D32f = cv.cvCreateMat(1, count, cv.CV_32FC2) # Get contour point set. cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)) # Convert CvPoint set to CvBox2D32f set. cv.cvConvert(PointArray, PointArray2D32f) box = cv.CvBox2D() # Fits ellipse to current contour. box = cv.cvFitEllipse2(PointArray2D32f) # Draw current contour. cv.cvDrawContours(image04, c, cv.CV_RGB(255, 255, 255), cv.CV_RGB(255, 255, 255), 0, 1, 8, cv.cvPoint(0, 0)) # Convert ellipse data from float to integer representation. center = cv.CvPoint() size = cv.CvSize() center.x = cv.cvRound(box.center.x) center.y = cv.cvRound(box.center.y) size.width = cv.cvRound(box.size.width * 0.5) size.height = cv.cvRound(box.size.height * 0.5) box.angle = -box.angle # Draw ellipse. cv.cvEllipse(image04, center, size, box.angle, 0, 360, cv.CV_RGB(0, 0, 255), 1, cv.CV_AA, 0) # Show image. HighGUI use. highgui.cvShowImage("Result", image04)
def process_image( slider_pos ): """ Define trackbar callback functon. This function find contours, draw it and approximate it by ellipses. """ stor = cv.cvCreateMemStorage(0); # Threshold the source image. This needful for cv.cvFindContours(). cv.cvThreshold( image03, image02, slider_pos, 255, cv.CV_THRESH_BINARY ); # Find all contours. nb_contours, cont = cv.cvFindContours (image02, stor, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint (0,0)) # Clear images. IPL use. cv.cvZero(image02); cv.cvZero(image04); # This cycle draw all contours and approximate it by ellipses. for c in cont.hrange(): count = c.total; # This is number point in contour # Number point must be more than or equal to 6 (for cv.cvFitEllipse_32f). if( count < 6 ): continue; # Alloc memory for contour point set. PointArray = cv.cvCreateMat(1, count, cv.CV_32SC2) PointArray2D32f= cv.cvCreateMat( 1, count, cv.CV_32FC2) # Get contour point set. cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX)); # Convert CvPoint set to CvBox2D32f set. cv.cvConvert( PointArray, PointArray2D32f ) box = cv.CvBox2D() # Fits ellipse to current contour. box = cv.cvFitEllipse2(PointArray2D32f); # Draw current contour. cv.cvDrawContours(image04, c, cv.CV_RGB(255,255,255), cv.CV_RGB(255,255,255),0,1,8,cv.cvPoint(0,0)); # Convert ellipse data from float to integer representation. center = cv.CvPoint() size = cv.CvSize() center.x = cv.cvRound(box.center.x); center.y = cv.cvRound(box.center.y); size.width = cv.cvRound(box.size.width*0.5); size.height = cv.cvRound(box.size.height*0.5); box.angle = -box.angle; # Draw ellipse. cv.cvEllipse(image04, center, size, box.angle, 0, 360, cv.CV_RGB(0,0,255), 1, cv.CV_AA, 0); # Show image. HighGUI use. highgui.cvShowImage( "Result", image04 );
while True: turn = -1 # Captura o frame, binariza, procura por contornos e checa com os padroes # Capture frame, binarize, find contours and check with known patterns frame = hg.cvQueryFrame(capture) cv.cvCvtColor(frame, frame_mod,cv.CV_RGB2GRAY) cv.cvThreshold(frame_mod,frame_mod,120,254,cv.CV_THRESH_BINARY) hg.cvShowImage("Binarizado", frame_mod) source_c = cv.cvFindContours(frame_mod, storage2)[1] if source_c: for contour in source_c.hrange(): seq = cv.cvApproxPoly(contour,cv.sizeof_CvContour,storage2,cv.CV_POLY_APPROX_DP,2) for i in range(0,4): res = cv.cvMatchShapes(target_c[i], contour, cv.CV_CONTOURS_MATCH_I3) if res > 0 and res < limiar[i] and cv.cvContourArea(seq) > 1000: cv.cvDrawContours(frame,seq,color[i],color[i], 100,4) #print padroes[i] + ": " + str(res) turn = i # move o NXT # move NXT # esquerda # left if turn == 0: left.update(-25,170) right.update(25,170) # direita # right elif turn == 1: left.update(25,170) right.update(-25,170) # tras