def findcontours(iplimage, threshold=100): srcimage = opencv.cvCloneImage(iplimage) # create the storage area and bw image grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1) opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY) #threshold opencv.cvThreshold(grayscale, grayscale, threshold, 255, opencv.CV_THRESH_BINARY) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # find the contours nb_contours, contours = opencv.cvFindContours (grayscale, storage) # comment this out if you do not want approximation contours = opencv.cvApproxPoly (contours, opencv.sizeof_CvContour, storage, opencv.CV_POLY_APPROX_DP, 3, 1) # next line is for ctypes-opencv #contours = opencv.cvApproxPoly (contours, opencv.sizeof(opencv.CvContour), storage, opencv.CV_POLY_APPROX_DP, 3, 1) conts = [] for cont in contours.hrange(): points=[] for pt in cont: points.append((pt.x,pt.y)) conts.append(points) opencv.cvReleaseMemStorage(storage) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(grayscale) return (nb_contours, conts)
def findcontours(iplimage, threshold=100): srcimage = opencv.cvCloneImage(iplimage) # create the storage area and bw image grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1) opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY) # threshold opencv.cvThreshold(grayscale, grayscale, threshold, 255, opencv.CV_THRESH_BINARY) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # find the contours nb_contours, contours = opencv.cvFindContours(grayscale, storage) # comment this out if you do not want approximation contours = opencv.cvApproxPoly(contours, opencv.sizeof_CvContour, storage, opencv.CV_POLY_APPROX_DP, 3, 1) # next line is for ctypes-opencv # contours = opencv.cvApproxPoly (contours, opencv.sizeof(opencv.CvContour), storage, opencv.CV_POLY_APPROX_DP, 3, 1) conts = [] for cont in contours.hrange(): points = [] for pt in cont: points.append((pt.x, pt.y)) conts.append(points) opencv.cvReleaseMemStorage(storage) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(grayscale) return (nb_contours, conts)
if time.time() > timer + 0.5: switchMode(MODE_DETECT_OBSTACLES2) elif mode == MODE_DETECT_OBSTACLES3: screen.fill(white) queryWebcam() if time.time() > timer + 0.5: action = "" switchMode(MODE_READY) elif mode == MODE_DETECT_OBSTACLES2: before = time.time() screen.fill(white) im = queryWebcam() opencv.cvCvtColor (im, igray, opencv.CV_BGR2GRAY) opencv.cvSmooth(igray, igray, opencv.CV_GAUSSIAN, 3, 3) opencv.cvAdaptiveThreshold(igray, iwhite, 255, opencv.CV_ADAPTIVE_THRESH_GAUSSIAN_C) num, contours = opencv.cvFindContours (iwhite, stor, opencv.sizeof_CvContour, opencv.CV_RETR_LIST) opencv.cvCvtColor(iwhite, im, opencv.CV_GRAY2BGR) staticImage = im retrieveObstacles(contours) switchMode(MODE_DETECT_OBSTACLES3) action = "Detection took: %.2f" % (time.time()-before) elif mode == MODE_READY: screen.fill(lgray) im = queryWebcam() opencv.cvCvtColor(im, igray, opencv.CV_BGR2GRAY) opencv.cvSmooth(igray, igray, opencv.CV_GAUSSIAN, 5, 5) circles = opencv.cvHoughCircles(igray, stor, opencv.CV_HOUGH_GRADIENT, 2, height/4, 200, 150)