def pixelInRange(src, rmin, rmax, floor, roof, dst): if rmax > rmin: # normal case cvInRangeS(src, rmin, rmax, dst) else: # considering range as a cycle dst0 = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 1) dst1 = cvCreateImage(cvSize(frameWidth, frameHeight), IPL_DEPTH_8U, 1) cvInRangeS(src, floor, rmax, dst0) cvInRangeS(src, rmin, roof, dst1) cvOr(dst0, dst1, dst)
def main(): # ctrl+c to end global h,s,v,h2,v2,s2,d,e highgui.cvNamedWindow("Camera 1", 1) highgui.cvNamedWindow("Orig", 1) highgui.cvCreateTrackbar("H", "Camera 1", h, 256, tb_h) highgui.cvCreateTrackbar("S", "Camera 1", s, 256, tb_s) highgui.cvCreateTrackbar("V", "Camera 1", v, 256, tb_v) highgui.cvCreateTrackbar("H2", "Camera 1", h2, 256, tb_h2) highgui.cvCreateTrackbar("S2", "Camera 1", s2, 256, tb_s2) highgui.cvCreateTrackbar("V2", "Camera 1", v2, 256, tb_v2) highgui.cvCreateTrackbar("Dilate", "Camera 1", d, 30, tb_d) highgui.cvCreateTrackbar("Erode", "Camera 1", e, 30, tb_e) cap = highgui.cvCreateCameraCapture(1) highgui.cvSetCaptureProperty(cap, highgui.CV_CAP_PROP_FRAME_WIDTH, IMGW) highgui.cvSetCaptureProperty(cap, highgui.CV_CAP_PROP_FRAME_HEIGHT, IMGH) c = 0 t1 = tdraw = time.clock() t = 1 font = cv.cvInitFont(cv.CV_FONT_HERSHEY_PLAIN, 1, 1) while c != 0x27: image = highgui.cvQueryFrame(cap) if not image: print "capture failed" break thresh = cv.cvCreateImage(cv.cvSize(IMGW,IMGH),8,1) cv.cvSetZero(thresh) cv.cvCvtColor(image,image,cv.CV_RGB2HSV) cv.cvInRangeS(image, (h,s,v,0), (h2,s2,v2,0), thresh) result = cv.cvCreateImage(cv.cvSize(IMGW,IMGH),8,3) cv.cvSetZero(result) cv.cvOr(image,image,result,thresh) for i in range(1,e): cv.cvErode(result,result) for i in range(1,d): cv.cvDilate(result,result) # floodfill objects back in, allowing threshold differences outwards t2 = time.clock() if t2 > tdraw+0.3: t = t2-t1 tdraw=t2 cv.cvPutText(result, "FPS: " + str(1 / (t)), (0,25), font, (255,255,255)) t1 = t2 highgui.cvShowImage("Orig", image) highgui.cvShowImage("Camera 1", result) c = highgui.cvWaitKey(10)
# dynamic floodfill import queue q = queue.Queue(0) q.put({"x":tx,"y":ty}) thresh[ty][tx] = 255 while True: # move left and right until boundary is hit n = q.pop() l = n.x - 1 r = n.x + 1 y = n.y while True: if h-20 < img[y][l][0] < h+20: thresh[y][l] = 255 # else: break l = l - 1 #while True: #cv.cvInRangeS(img, (h-10,s-40,v-20,0), (h+10,s+40,v+20,0), thresh) result = cv.cvCreateImage(cv.cvSize(IMGW,IMGH),8,3) cv.cvSetZero(result) cv.cvOr(img,img,result,thresh) if showoriginal: highgui.cvShowImage("Blob", imgorig) else: highgui.cvShowImage("Blob", result) c = highgui.cvWaitKey(10)