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 read(self): frame=self.input.read() if self.enabled: cv_rs = [None]*4 cv_thresh = [0]*4 cv_max = [255]*4 for i in self.channels : cv_rs[i] = cv.cvCreateImage(cv.cvSize(frame.width,frame.height),frame.depth,1) cv_thresh[i] = self.thresholds[i] cv_max[i] = self.max_thresholds[i] # extract the color channel cv.cvSplit(frame,cv_rs[0],cv_rs[1],cv_rs[2],cv_rs[3]) #self.debug_print(cv_rs) for i in self.channels : cv.cvThreshold(cv_rs[i],cv_rs[i],cv_thresh[i],cv_max[i],self.type) #cv_thresh = cv.cvCreateImage(cv.cvSize(frame.width,frame.height),frame.depth,3) cv.cvZero(frame) cv.cvMerge(cv_rs[0],cv_rs[1],cv_rs[2],cv_rs[3],frame) #frame = cv_thresh return frame
def depthmatch(x,y,leftimage,rightimage,roi=80,buf=50,baseline=2.7,focal_length=80): """depthmatch function x,y : (int) pixel position of target in left image leftimage, rightimage : (IplImage) stereo images roi: (int) region of interest around x,y to use in matching buf: (int) buffer outside of a straight horizontal search for a match """ #print "Match",x,y info = cv.cvGetSize(leftimage) width = info.width height = info.height centerx = width/2 centery = height/2 (y1,x1,y2,x2) = (y-roi,x-roi,y+roi,x+roi) if y1<0: y1 = 0 if x1<0: x1 = 0 if y2>height: y2 = height if x2>width: x2 = width # copy subregion roi x roi template_rect = cv.cvRect(x1,y1,(x2-x1),(y2-y1)) template = cv.cvGetSubRect(leftimage, template_rect) #(y3,x3,y4,x4) = (y-roi-buf,x-roi-buf,y+roi+buf,width) # +/- 20 pixels in vertical direction, -20 to the right edge (y3,x3,y4,x4) = (y-roi-buf,0,y+roi+buf,x+roi+buf) # +/- buf pixels in vertical direction, +buf to the left edge if x3<0: x3 = 0 if y3<0: y3 = 0 if x4>=width: x4 = width-1 if y4>height: y4 = height #cv.cvSetImageROI(rightimage, (y3,x3,y4,x4)) rightsub_rect = cv.cvRect(x3,y3,(x4-x3),(y4-y3)) rightsub = cv.cvGetSubRect(rightimage, rightsub_rect) # result matrix should be (W - w + 1) x (H - h + 1) where WxH are template dimensions, wxh are rightsub dimensions W = x4-x3 H = y4-y3 w = x2-x1 h = y2-y1 resy = (y4-y3)-(y2-y1)+1 resx = (x4-x3)-(x2-x1)+1 resultmat = cv.cvCreateImage((resx, resy), 32, 1) cv.cvZero(resultmat) # match template image in a subportion of rightimage cv.cvMatchTemplate(rightsub, template, resultmat, cv.CV_TM_SQDIFF) min_val, max_val, min_point, max_point = cv.cvMinMaxLoc(resultmat) cv.cvNormalize(resultmat, resultmat, 1, 0, cv.CV_MINMAX) depth = plane2point(x-centerx, y-centery, x3+min_point.x+roi-centerx, y3+min_point.y+roi-centery, baseline, focal_length) #print "Found match at", min_point.x+x3, min_point.y+y3 return (depth, (x,y), (x3+min_point.x+roi, y3+min_point.y+roi))
def getBackground(frameWidht, frameHeight): cvNamedWindow("Background") text = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 3) frame = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 3) background = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 3) font = cvInitFont(CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0, 0.0, 2) pt1 = cvPoint(50, 100) pt2 = cvPoint(50, 150) center = cvPoint(frameWidth/2, frameHeight/2) cvPutText(text, "Press enter, run away and wait", pt1, font, CV_RGB(150, 100, 150)) cvPutText(text, str(delayS) + " seconds to capture background", pt2, font, CV_RGB(150, 100, 150)) cvShowImage("Background", text) key = -1 while key == -1: key = cvWaitKey(10) like = False while not like: for i in range(delayS): cvZero(text) cvPutText(text, str(delayS-i), center, font, CV_RGB(150, 100, 150)) cvShowImage("Background", text) cvWaitKey(1000) csut = camStartUpTime while (csut): # Stats capturing frames in order to give time to the cam to auto-adjust colors if not cvGrabFrame(CAM): print "Could not grab a frame" exit cvWaitKey(10) csut -= 1 frame = cvQueryFrame(CAM) cvCopy(frame, background) cvCopy(frame, text) cvPutText(text, "Is correct? [y/n]", center, font, CV_RGB(150, 100, 150)) cvShowImage("Background", text) key = -1 while key != 'n' and key != 'y': key = cvWaitKey(10) if key == 'y': like = True return background cvDestroyWindow("Background")
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 depthmatch(x,y,leftimage,rightimage,roi=20,buf=10,debug=False): __doc__ = """depthmatch function x,y : (int) pixel position of target in left image leftimage, rightimage : (IplImage) stereo images roi: (int) region of interest around x,y to use in matching buf: (int) buffer outside of a straight horizontal search for a match """ info = cv.cvGetSize(leftimage) width = info.width height = info.height (y1,x1,y2,x2) = (y-roi,x-roi,y+roi,x+roi) #template = cv.cvCreateImage((roi*2,roi*2), 8, 3) if y1<0: y1 = 0 if x1<0: x1 = 0 if y2>height: y2 = height if x2>width: x2 = width #cv.cvSetZero(template) # copy subregion roi x roi template_rect = cv.cvRect(x1,y1,(x2-x1),(y2-y1)) template = cv.cvGetSubRect(leftimage, template_rect) (y3,x3,y4,x4) = (y-roi-buf,x-roi-buf,y+roi+buf,width) # +/- 20 pixels in vertical direction, -20 to the right edge if x3<0: x3 = 0 if y3<0: y3 = 0 if x4>=width: x4 = width-1 if y4>height: y4 = height #cv.cvSetImageROI(rightimage, (y3,x3,y4,x4)) rightsub_rect = cv.cvRect(x3,y3,(x4-x3),(y4-y3)) rightsub = cv.cvGetSubRect(rightimage, rightsub_rect) # result matrix should be (W - w + 1) x (H - h + 1) where WxH are template dimensions, wxh are rightsub dimensions W = x4-x3 H = y4-y3 w = x2-x1 h = y2-y1 resy = (y4-y3)-(y2-y1)+1 resx = (x4-x3)-(x2-x1)+1 resultmat = cv.cvCreateImage((resx, resy), 32, 1) cv.cvZero(resultmat) # match template image in a subportion of rightimage cv.cvMatchTemplate(rightsub, template, resultmat, cv.CV_TM_SQDIFF) min_val, max_val, min_point, max_point = cv.cvMinMaxLoc(resultmat) cv.cvNormalize(resultmat, resultmat, 1, 0, cv.CV_MINMAX) depth = stereo.depth(x, x3+min_point.x, max_pixels=width/2) if debug: print "Input image: %ix%i, target: (%i,%i)" % (width,height,x,y) print "Template box: (%i,%i) to (%i,%i)" % (x1, y1, x2, y2) print "Search area: (%i,%i) to (%i,%i)" % (x3, y3, x4, y4) print "%ix%i, %ix%i" % (W,H,w,h) print "Result matrix %ix%i" % (resx, resy) print "stereo.depth(%i,%i,max_pixels=%i)" % (x, min_point.x+x3,width/2) if depth[0]: print "Depth: ", depth[0], "(cm)" #cv.cvRectangle(rightimage, cv.cvPoint(x1,y1), cv.cvPoint(x2,y2), (255,0,0)) cv.cvRectangle(rightimage, cv.cvPoint(min_point.x+x3,min_point.y+y3), cv.cvPoint(min_point.x+x3+roi*2,min_point.y+y3+roi*2), (0,255,0)) cv.cvRectangle(rightimage, cv.cvPoint(x3,y3), cv.cvPoint(x4,y4), (0,0,255)) cv.cvRectangle(leftimage, cv.cvPoint(x1,y1), cv.cvPoint(x2,y2), (255,0,0)) #cv.cvRectangle(leftimage, cv.cvPoint(min_point.x+x3,min_point.y+y3), cv.cvPoint(min_point.x+x3+roi*2,min_point.y+y3+roi*2), (0,255,0)) cv.cvRectangle(leftimage, cv.cvPoint(x3,y3), cv.cvPoint(x4,y4), (0,0,255)) if depth[0]: cv.cvPutText(leftimage, "%5f(cm)" % depth[0], (x1,y1), font, (255,255,255)) highgui.cvShowImage("depthmatch - template", template) highgui.cvShowImage("depthmatch - match", resultmat) highgui.cvShowImage("depthmatch - right", rightimage) highgui.cvShowImage("depthmatch - left", leftimage)
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 );
if (track_box.size.width > 10 or track_box.size.height > 10): rotate = ( (image.width/2.0) - track_box.center.x) / (image.width/2.0) translate = ((image.height/2.0) - track_box.center.y) / (image.height/2.0) #print "rotate =", rotate, "translate =", translate if go: move(translate, rotate) if select_object and selection.width > 0 and selection.height > 0: subimg = cv.cvGetSubRect(image, selection) cv.cvXorS( subimage, cv.cvScalarAll(255), subimage, 0 ) highgui.cvShowImage( "VisualJoystick", image ) c = highgui.cvWaitKey(10) if c == '\x1b': break elif c == 'b': backproject_mode ^= 1 elif c == 'c': track_object = 0 cv.cvZero( histimg ) if go: stop() if go: stop()
def compute_saliency(image): global thresh global scale saliency_scale = int(math.pow(2,scale)); bw_im1 = cv.cvCreateImage(cv.cvGetSize(image), cv.IPL_DEPTH_8U,1) cv.cvCvtColor(image, bw_im1, cv.CV_BGR2GRAY) bw_im = cv.cvCreateImage(cv.cvSize(saliency_scale,saliency_scale), cv.IPL_DEPTH_8U,1) cv.cvResize(bw_im1, bw_im) highgui.cvShowImage("BW", bw_im) realInput = cv.cvCreateImage( cv.cvGetSize(bw_im), cv.IPL_DEPTH_32F, 1); imaginaryInput = cv.cvCreateImage( cv.cvGetSize(bw_im), cv.IPL_DEPTH_32F, 1); complexInput = cv.cvCreateImage( cv.cvGetSize(bw_im), cv.IPL_DEPTH_32F, 2); cv.cvScale(bw_im, realInput, 1.0, 0.0); cv.cvZero(imaginaryInput); cv.cvMerge(realInput, imaginaryInput, None, None, complexInput); dft_M = saliency_scale #cv.cvGetOptimalDFTSize( bw_im.height - 1 ); dft_N = saliency_scale #cv.cvGetOptimalDFTSize( bw_im.width - 1 ); dft_A = cv.cvCreateMat( dft_M, dft_N, cv.CV_32FC2 ); image_Re = cv.cvCreateImage( cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1); image_Im = cv.cvCreateImage( cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1); # copy A to dft_A and pad dft_A with zeros tmp = cv.cvGetSubRect( dft_A, cv.cvRect(0,0, bw_im.width, bw_im.height)); cv.cvCopy( complexInput, tmp, None ); if(dft_A.width > bw_im.width): tmp = cv.cvGetSubRect( dft_A, cv.cvRect(bw_im.width,0, dft_N - bw_im.width, bw_im.height)); cv.cvZero( tmp ); cv.cvDFT( dft_A, dft_A, cv.CV_DXT_FORWARD, complexInput.height ); cv.cvSplit( dft_A, image_Re, image_Im, None, None ); # Compute the phase angle image_Mag = cv.cvCreateImage(cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1); image_Phase = cv.cvCreateImage(cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1); #compute the phase of the spectrum cv.cvCartToPolar(image_Re, image_Im, image_Mag, image_Phase, 0) log_mag = cv.cvCreateImage(cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1); cv.cvLog(image_Mag, log_mag) #Box filter the magnitude, then take the difference image_Mag_Filt = cv.cvCreateImage(cv.cvSize(dft_N, dft_M), cv.IPL_DEPTH_32F, 1); filt = cv.cvCreateMat(3,3, cv.CV_32FC1); cv.cvSet(filt,cv.cvScalarAll(-1.0/9.0)) cv.cvFilter2D(log_mag, image_Mag_Filt, filt, cv.cvPoint(-1,-1)) cv.cvAdd(log_mag, image_Mag_Filt, log_mag, None) cv.cvExp(log_mag, log_mag) cv.cvPolarToCart(log_mag, image_Phase, image_Re, image_Im,0); cv.cvMerge(image_Re, image_Im, None, None, dft_A) cv.cvDFT( dft_A, dft_A, cv.CV_DXT_INVERSE, complexInput.height) tmp = cv.cvGetSubRect( dft_A, cv.cvRect(0,0, bw_im.width, bw_im.height)); cv.cvCopy( tmp, complexInput, None ); cv.cvSplit(complexInput, realInput, imaginaryInput, None, None) min, max = cv.cvMinMaxLoc(realInput); #cv.cvScale(realInput, realInput, 1.0/(max-min), 1.0*(-min)/(max-min)); cv.cvSmooth(realInput, realInput); threshold = thresh/100.0*cv.cvAvg(realInput)[0] cv.cvThreshold(realInput, realInput, threshold, 1.0, cv.CV_THRESH_BINARY) tmp_img = cv.cvCreateImage(cv.cvGetSize(bw_im1),cv.IPL_DEPTH_32F, 1) cv.cvResize(realInput,tmp_img) cv.cvScale(tmp_img, bw_im1, 255,0) return bw_im1
rotate = ((image.width / 2.0) - track_box.center.x) / (image.width / 2.0) translate = ((image.height / 2.0) - track_box.center.y) / (image.height / 2.0) #print "rotate =", rotate, "translate =", translate if go: move(translate, rotate) if select_object and selection.width > 0 and selection.height > 0: subimg = cv.cvGetSubRect(image, selection) cv.cvXorS(subimage, cv.cvScalarAll(255), subimage, 0) highgui.cvShowImage("VisualJoystick", image) c = highgui.cvWaitKey(10) if c == '\x1b': break elif c == 'b': backproject_mode ^= 1 elif c == 'c': track_object = 0 cv.cvZero(histimg) if go: stop() if go: stop()
rgb[sector_data[sector][2]] = p return cv.cvScalar(rgb[2], rgb[1], rgb[0], 0) img_h = cv.cvCreateImage(size, 8, 1) img_s = cv.cvCreateImage(size, 8, 1) img_v = cv.cvCreateImage(size, 8, 1) thresh_mask = cv.cvCreateImage(size, 8, 1) hist_hue_img = cv.cvCreateImage((int(h_bins * scalewidth), scaleheight), 8, 3) hist_val_img = cv.cvCreateImage((int(v_bins * scalewidth), scaleheight), 8, 3) output_mask = cv.cvCreateImage(size, 8, 1) while True: img = highgui.cvQueryFrame(cap) cv.cvZero(img_h) cv.cvZero(img_s) cv.cvZero(img_v) cv.cvZero(thresh_mask) highgui.cvShowImage("Input", img) # 5x5 Gaussian Blur cv.cvSmooth(img, img, cv.CV_GAUSSIAN, 5, 5) # convert to HSV cv.cvCvtColor(img, img, cv.CV_BGR2HSV) # threshold bad values cv.cvInRangeS(img, hsv_min, hsv_max, thresh_mask) cv.cvAnd(thresh_mask, mask_bw, thresh_mask) # Hue(0,180), Saturation(0,255), Value(0,255) cv.cvSplit(img, img_h, img_s, img_v, 0)