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)
def getData(): frame = highgui.cvQueryFrame(capture) if frame is None: return None cv.cvSplit(frame, b_img, g_img, r_img, None) cv.cvInRangeS(r_img, 150, 255, r_img) cv.cvInRangeS(g_img, 0, 100, g_img) cv.cvInRangeS(b_img, 0, 100, b_img) cv.cvAnd(r_img, g_img, laser_img) cv.cvAnd(laser_img, b_img, laser_img) cv.cvErode(laser_img,laser_img) #,0,2) cv.cvDilate(laser_img,laser_img) c_count, contours = cv.cvFindContours (laser_img, storage, cv.sizeof_CvContour, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE, cv.cvPoint (0,0)) if c_count: return returnEllipses(contours) else: return None
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 _get_frame_in_range(self,color,tol=5,frame=None) : if frame is None : frame = self._get_cv_frame() cv_thresh = cv.cvCreateImage(cv.cvSize(frame.width,frame.height),frame.depth,1) cv.cvInRangeS(self._get_color_frame('r'),color[0]-tol,color[0]+tol,cv_thresh) cv.cvInRangeS(self._get_color_frame('g'),color[1]-tol,color[1]+tol,cv_thresh) cv.cvInRangeS(self._get_color_frame('b'),color[2]-tol,color[2]+tol,cv_thresh) return cv_thresh
# do forever # 1. capture the current image frame = highgui.cvQueryFrame(capture) if frame is None: # no image captured... end the processing break # mirror the captured image #cv.cvFlip (frame, None, 1) # compute the hsv version of the image cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV) # compute which pixels are in the wanted range cv.cvInRangeS(hsv, hsv_min, hsv_max, mask) # extract the hue from the hsv array cv.cvSplit(hsv, hue, None, None, None) # select the rectangle of interest in the hue/mask arrays hue_roi = cv.cvGetSubRect(hue, selection) mask_roi = cv.cvGetSubRect(mask, selection) # it's time to compute the histogram cv.cvCalcHist(hue_roi, hist, 0, mask_roi) # extract the min and max value of the histogram min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue(hist) # compute the scale factor
def main(): print "OpenCV version: %s (%d, %d, %d)" % (cv.CV_VERSION, cv.CV_MAJOR_VERSION, cv.CV_MINOR_VERSION, cv.CV_SUBMINOR_VERSION) # create windows create_and_position_window('Thresholded_HSV_Image', 10, 10) create_and_position_window('RGB_VideoFrame', 10+cam_width, 10) create_and_position_window('Hue', 10, 10+cam_height) create_and_position_window('Saturation', 210, 10+cam_height) create_and_position_window('Value', 410, 10+cam_height) create_and_position_window('LaserPointer', 0,0) capture = setup_camera_capture() # create images for the different channels h_img = cv.cvCreateImage (cv.cvSize (cam_width,cam_height), 8, 1) s_img = cv.cvCreateImage (cv.cvSize (cam_width,cam_height), 8, 1) v_img = cv.cvCreateImage (cv.cvSize (cam_width,cam_height), 8, 1) laser_img = cv.cvCreateImage (cv.cvSize (cam_width,cam_height), 8, 1) cv.cvSetZero(h_img) cv.cvSetZero(s_img) cv.cvSetZero(v_img) cv.cvSetZero(laser_img) while True: # 1. capture the current image frame = highgui.cvQueryFrame (capture) if frame is None: # no image captured... end the processing break hsv_image = cv.cvCloneImage(frame) # temporary copy of the frame cv.cvCvtColor(frame, hsv_image, cv.CV_BGR2HSV) # convert to HSV # split the video frame into color channels cv.cvSplit(hsv_image, h_img, s_img, v_img, None) # Threshold ranges of HSV components. cv.cvInRangeS(h_img, hmin, hmax, h_img) cv.cvInRangeS(s_img, smin, smax, s_img) cv.cvInRangeS(v_img, vmin, vmax, v_img) # Perform an AND on HSV components to identify the laser! cv.cvAnd(h_img, v_img, laser_img) # This actually Worked OK for me without using Saturation. #cv.cvAnd(laser_img, s_img,laser_img) # Merge the HSV components back together. cv.cvMerge(h_img, s_img, v_img, None, hsv_image) #----------------------------------------------------- # NOTE: default color space in OpenCV is BGR!! # we can now display the images highgui.cvShowImage ('Thresholded_HSV_Image', hsv_image) highgui.cvShowImage ('RGB_VideoFrame', frame) highgui.cvShowImage ('Hue', h_img) highgui.cvShowImage ('Saturation', s_img) highgui.cvShowImage ('Value', v_img) highgui.cvShowImage('LaserPointer', laser_img) # handle events k = highgui.cvWaitKey (10) if k == '\x1b' or k == 'q': # user has press the ESC key, so exit break
def main(args): global capture global hmax, hmin global stats, startTime highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Red Hue', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Green Hue', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Value', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Red Laser', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Green Laser', highgui.CV_WINDOW_AUTOSIZE) highgui.cvMoveWindow('Camera', 0, 10) highgui.cvMoveWindow('Value', 10, 420) highgui.cvMoveWindow('Red Laser', 360, 10) highgui.cvMoveWindow('Green Laser', 360, 360) highgui.cvMoveWindow('Red Hue',700, 10 ) highgui.cvMoveWindow('Green Hue',700, 420) highgui.cvCreateTrackbar("Brightness Trackbar","Camera",0,255, change_brightness); highgui.cvCreateTrackbar("vmin Trackbar","Value",vmin,255, change_vmin); highgui.cvCreateTrackbar("vmax Trackbar","Value",vmax,255, change_vmax); highgui.cvCreateTrackbar("red hmin Trackbar","Red Hue",red_hmin,180, change_red_hmin); highgui.cvCreateTrackbar("red hmax Trackbar","Red Hue",red_hmax,180, change_red_hmax); highgui.cvCreateTrackbar("green hmin Trackbar","Green Hue",green_hmin,180, change_green_hmin); highgui.cvCreateTrackbar("green hmax Trackbar","Green Hue",green_hmax,180, change_green_hmax); print "grabbing camera" capture = highgui.cvCreateCameraCapture(0) print "found camera" highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_WIDTH, iwidth) highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_HEIGHT, iheight) frame = highgui.cvQueryFrame(capture) frameSize = cv.cvGetSize(frame) hsv = cv.cvCreateImage(frameSize,8,3) mask = cv.cvCreateImage(frameSize,8,1) red_hue = cv.cvCreateImage(frameSize,8,1) green_hue = cv.cvCreateImage(frameSize,8,1) saturation = cv.cvCreateImage(frameSize,8,1) value = cv.cvCreateImage(frameSize,8,1) red_laser = cv.cvCreateImage(frameSize,8,1) green_laser = cv.cvCreateImage(frameSize,8,1) turret = FuzzyController(frameSize.width,frameSize.height,True) while 1: frame = highgui.cvQueryFrame(capture) cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV) cv.cvSplit(hsv,red_hue,saturation,value,None) cv.cvSplit(hsv,green_hue,saturation,value,None) cv.cvInRangeS(red_hue, cv.cvScalar(red_hmin), cv.cvScalar(red_hmax), red_hue) cv.cvInRangeS(green_hue, cv.cvScalar(green_hmin), cv.cvScalar(green_hmax), green_hue) cv.cvInRangeS(value, cv.cvScalar(vmin), cv.cvScalar(vmax), value) cv.cvAnd(red_hue, value, red_laser) cv.cvAnd(green_hue, value, green_laser) green_cenX,green_cenY = averageWhitePoints(green_laser) draw_target(frame, green_cenX, green_cenY, "GREEN") red_cenX, red_cenY = averageWhitePoints(red_laser) draw_target(frame, red_cenX, red_cenY, "RED") if(green_cenX >= 0 and green_cenY >= 0):# and move_count <= 0): turret.update(green_cenX,green_cenY) highgui.cvShowImage('Camera',frame) highgui.cvShowImage('Red Hue', red_hue) highgui.cvShowImage('Green Hue', green_hue) highgui.cvShowImage('Value',value) highgui.cvShowImage('Red Laser',red_laser) highgui.cvShowImage('Green Laser',green_laser) if stats: printRunningStats((green_cenX, green_cenY), (red_cenX, red_cenY)) k = highgui.cvWaitKey(10) if k == '\x1b' or k == 'q': sys.exit() if k == 'p': if stats: printTotalStats() stats = False else: startTime = time() stats = True
# do forever # 1. capture the current image frame = highgui.cvQueryFrame (capture) if frame is None: # no image captured... end the processing break # mirror the captured image cv.cvFlip (frame, None, 1) # compute the hsv version of the image cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV) # compute which pixels are in the wanted range cv.cvInRangeS (hsv, hsv_min, hsv_max, mask) # extract the hue from the hsv array cv.cvSplit (hsv, hue, None, None, None) # select the rectangle of interest in the hue/mask arrays hue_roi = cv.cvGetSubRect (hue, selection) mask_roi = cv.cvGetSubRect (mask, selection) # it's time to compute the histogram cv.cvCalcHist (hue_roi, hist, 0, mask_roi) # extract the min and max value of the histogram #min_val, max_val = cv.cvGetMinMaxHistValue (hist, None, None) min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue (hist)
def main(args): global capture global hmax, hmin highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Hue', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Satuation', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Value', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Laser', highgui.CV_WINDOW_AUTOSIZE) highgui.cvMoveWindow('Camera', 0, 10) highgui.cvMoveWindow('Hue', 0, 350) highgui.cvMoveWindow('Satuation', 360, 10) highgui.cvMoveWindow('Value', 360, 350) highgui.cvMoveWindow('Laser', 700, 40) highgui.cvCreateTrackbar("Brightness Trackbar","Camera",0,255, change_brightness); highgui.cvCreateTrackbar("hmin Trackbar","Hue",hmin,180, change_hmin); highgui.cvCreateTrackbar("hmax Trackbar","Hue",hmax,180, change_hmax); highgui.cvCreateTrackbar("smin Trackbar","Satuation",smin,255, change_smin); highgui.cvCreateTrackbar("smax Trackbar","Satuation",smax,255, change_smax); highgui.cvCreateTrackbar("vmin Trackbar","Value",vmin,255, change_vmin); highgui.cvCreateTrackbar("vmax Trackbar","Value",vmax,255, change_vmax); print "grabbing camera" capture = highgui.cvCreateCameraCapture(0) print "found camera" highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_WIDTH, 320) highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_HEIGHT, 240) frame = highgui.cvQueryFrame(capture) frameSize = cv.cvGetSize(frame) hsv = cv.cvCreateImage(frameSize,8,3) mask = cv.cvCreateImage(frameSize,8,1) hue = cv.cvCreateImage(frameSize,8,1) satuation = cv.cvCreateImage(frameSize,8,1) value = cv.cvCreateImage(frameSize,8,1) laser = cv.cvCreateImage(frameSize,8,1) turret = FuzzyController(frameSize.width,frameSize.height,True) move_count = 0 while 1: frame = highgui.cvQueryFrame(capture) cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV) #cv.cvInRangeS(hsv,hsv_min,hsv_max,mask) cv.cvSplit(hsv,hue,satuation,value,None) cv.cvInRangeS(hue,cv.cvScalar(hmin),cv.cvScalar(hmax),hue) cv.cvInRangeS(satuation,cv.cvScalar(smin),cv.cvScalar(smax),satuation) cv.cvInRangeS(value,cv.cvScalar(vmin),cv.cvScalar(vmax),value) #cv.cvInRangeS(hue,0,180,hue) cv.cvAnd(hue, value, laser) #cv.cvAnd(laser, value, laser) cenX,cenY = averageWhitePoints(laser) #print cenX,cenY draw_target(frame,cenX,cenY) if(cenX != 0 and cenY != 0):# and move_count <= 0): turret.update(cenX,cenY,False) """ turret.reset() move_count = 3 if(cenX < 100): turret.left(20) elif(cenX > 200): turret.right(20) if(cenY < 80): turret.up(40) elif(cenY > 170): print "DOWN please.." turret.down(40) print cenY """ #move_count -= 1 #draw_target(frame,200,1) highgui.cvShowImage('Camera',frame) highgui.cvShowImage('Hue',hue) highgui.cvShowImage('Satuation',satuation) highgui.cvShowImage('Value',value) highgui.cvShowImage('Laser',laser) k = highgui.cvWaitKey(10) if k == 'q': sys.exit()
def main(args): global capture global hmax, hmin highgui.cvNamedWindow('Hue', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Saturation', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Value', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Laser', highgui.CV_WINDOW_AUTOSIZE) highgui.cvMoveWindow('Camera', 0, 10) highgui.cvMoveWindow('Hue', 0, 350) highgui.cvMoveWindow('Saturation', 360, 10) highgui.cvMoveWindow('Value', 360, 350) highgui.cvMoveWindow('Laser', 700, 40) highgui.cvCreateTrackbar("Brightness Trackbar","Camera",0,255, change_brightness); highgui.cvCreateTrackbar("hmin Trackbar","Hue",hmin,180, change_hmin); highgui.cvCreateTrackbar("hmax Trackbar","Hue",hmax,180, change_hmax); highgui.cvCreateTrackbar("smin Trackbar","Saturation",smin,255, change_smin); highgui.cvCreateTrackbar("smax Trackbar","Saturation",smax,255, change_smax); highgui.cvCreateTrackbar("vmin Trackbar","Value",vmin,255, change_vmin); highgui.cvCreateTrackbar("vmax Trackbar","Value",vmax,255, change_vmax); print "grabbing camera" capture = highgui.cvCreateCameraCapture(0) print "found camera" highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_WIDTH, iwidth) highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_HEIGHT, iheight) frame = highgui.cvQueryFrame(capture) frameSize = cv.cvGetSize(frame) hsv = cv.cvCreateImage(frameSize,8,3) mask = cv.cvCreateImage(frameSize,8,1) hue = cv.cvCreateImage(frameSize,8,1) saturation = cv.cvCreateImage(frameSize,8,1) value = cv.cvCreateImage(frameSize,8,1) laser = cv.cvCreateImage(frameSize,8,1) while 1: frame = highgui.cvQueryFrame(capture) cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV) #cv.cvInRangeS(hsv,hsv_min,hsv_max,mask) cv.cvSplit(hsv,hue,saturation,value,None) #print hmin, hmax cv.cvInRangeS(hue,cv.cvScalar(hmin),cv.cvScalar(hmax),hue) cv.cvInRangeS(saturation,cv.cvScalar(smin),cv.cvScalar(smax),saturation) cv.cvInRangeS(value,cv.cvScalar(vmin),cv.cvScalar(vmax),value) #cv.cvInRangeS(hue,cv.cvScalar(0),cv.cvScalar(180),hue) cv.cvAnd(hue, value, laser) #cv.cvAnd(laser, value, laser) # stupid filter #removeErrantPoints(laser) cenX,cenY = averageWhitePoints(laser) px = iwidth/2 - cenX dis = 57.18832855 / ( px - 5.702350176) + .05753797721 print cenX,px,dis draw_target(frame,cenX,cenY) #draw_target(frame,200,1) highgui.cvShowImage('Hue',hue) highgui.cvShowImage('Camera',frame) highgui.cvShowImage('Saturation',saturation) highgui.cvShowImage('Value',value) highgui.cvShowImage('Laser',laser) highgui.cvWaitKey(10)
def main(args): global capture global hmax, hmin highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Hue', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Satuation', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Value', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Laser', highgui.CV_WINDOW_AUTOSIZE) highgui.cvMoveWindow('Camera', 0, 10) highgui.cvMoveWindow('Hue', 0, 350) highgui.cvMoveWindow('Satuation', 360, 10) highgui.cvMoveWindow('Value', 360, 350) highgui.cvMoveWindow('Laser', 700, 40) highgui.cvCreateTrackbar("Brightness Trackbar","Camera",0,255, change_brightness); highgui.cvCreateTrackbar("hmin Trackbar","Hue",hmin,180, change_hmin); highgui.cvCreateTrackbar("hmax Trackbar","Hue",hmax,180, change_hmax); highgui.cvCreateTrackbar("smin Trackbar","Satuation",smin,255, change_smin); highgui.cvCreateTrackbar("smax Trackbar","Satuation",smax,255, change_smax); highgui.cvCreateTrackbar("vmin Trackbar","Value",vmin,255, change_vmin); highgui.cvCreateTrackbar("vmax Trackbar","Value",vmax,255, change_vmax); print "grabbing camera" capture = highgui.cvCreateCameraCapture(0) print "found camera" highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_WIDTH, 320) highgui.cvSetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_HEIGHT, 240) frame = highgui.cvQueryFrame(capture) frameSize = cv.cvGetSize(frame) hsv = cv.cvCreateImage(frameSize,8,3) mask = cv.cvCreateImage(frameSize,8,1) hue = cv.cvCreateImage(frameSize,8,1) satuation = cv.cvCreateImage(frameSize,8,1) value = cv.cvCreateImage(frameSize,8,1) laser = cv.cvCreateImage(frameSize,8,1) while 1: frame = highgui.cvQueryFrame(capture) cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV) #cv.cvInRangeS(hsv,hsv_min,hsv_max,mask) cv.cvSplit(hsv,hue,satuation,value,None) cv.cvInRangeS(hue,hmin,hmax,hue) cv.cvInRangeS(satuation,smin,smax,satuation) cv.cvInRangeS(value,vmin,vmax,value) #cv.cvInRangeS(hue,0,180,hue) cv.cvAnd(hue, value, laser) #cv.cvAnd(laser, value, laser) cenX,cenY = averageWhitePoints(laser) #print cenX,cenY draw_target(frame,cenX,cenY) #draw_target(frame,200,1) highgui.cvShowImage('Camera',frame) highgui.cvShowImage('Hue',hue) highgui.cvShowImage('Satuation',satuation) highgui.cvShowImage('Value',value) highgui.cvShowImage('Laser',laser) k = highgui.cvWaitKey(10) if k == " ": highgui.cvDestroyAllWindows() highgui.cvReleaseCapture (capture) sys.exit()
# do forever # 1. capture the current image frame = highgui.cvQueryFrame (capture) if frame is None: # no image captured... end the processing break # mirror the captured image #cv.cvFlip (frame, None, 1) # compute the hsv version of the image cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV) # compute which pixels are in the wanted range cv.cvInRangeS (hsv, hsv_min, hsv_max, mask) # extract the hue from the hsv array cv.cvSplit (hsv, hue, None, None, None) # select the rectangle of interest in the hue/mask arrays hue_roi = cv.cvGetSubRect (hue, selection) mask_roi = cv.cvGetSubRect (mask, selection) # it's time to compute the histogram cv.cvCalcHist (hue_roi, hist, 0, mask_roi) # extract the min and max value of the histogram min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue (hist) # compute the scale factor
cv.cvCvtColor(image, hsv, cv.CV_BGR2HSV) cv.cvLine(image, cv.cvPoint(0, image.height / 2), cv.cvPoint(image.width, image.height / 2), cv.CV_RGB(0, 255, 0), 2, 8, 0) cv.cvLine(image, cv.cvPoint(image.width / 2, 0), cv.cvPoint(image.width / 2, image.height), cv.CV_RGB(0, 255, 0), 2, 8, 0) if track_object: _vmin = vmin _vmax = vmax cv.cvInRangeS(hsv, cv.cvScalar(0, smin, min(_vmin, _vmax), 0), cv.cvScalar(180, 256, max(_vmin, _vmax), 0), mask) cv.cvSplit(hsv, hue, None, None, None) if track_object < 0: max_val = 0.0 subhue = cv.cvGetSubRect(hue, selection) submask = cv.cvGetSubRect(mask, selection) cv.cvCalcHist(subhue, hist, 0, submask) # extract the min and max value of the histogram min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue( hist) if (max_val): cv.cvConvertScale(hist.bins, hist.bins, 255.0 / max_val, 0)
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) # calculate histogram cv.cvCalcHist(img_h, h_hue, 0, thresh_mask) cv.cvCalcHist(img_s, h_sat, 0, thresh_mask) cv.cvCalcHist(img_v, h_val, 0, thresh_mask) # Don't normalize, use total mask pixels to calculate relative importance # cv.cvNormalizeHist(h_hue, 180) # cv.cvNormalizeHist(h_sat, 255) # cv.cvNormalizeHist(h_val, 255) # minv,maxv,minp,maxp = cv.cvMinMaxLoc(img_h)
def main(args): global capture global hmax, hmin highgui.cvNamedWindow('Camera', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Hue', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Satuation', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Value', highgui.CV_WINDOW_AUTOSIZE) highgui.cvNamedWindow('Laser', highgui.CV_WINDOW_AUTOSIZE) highgui.cvMoveWindow('Camera', 0, 10) highgui.cvMoveWindow('Hue', 0, 350) highgui.cvMoveWindow('Satuation', 360, 10) highgui.cvMoveWindow('Value', 360, 350) highgui.cvMoveWindow('Laser', 700, 40) highgui.cvCreateTrackbar("Brightness Trackbar", "Camera", 0, 255, change_brightness) highgui.cvCreateTrackbar("hmin Trackbar", "Hue", hmin, 180, change_hmin) highgui.cvCreateTrackbar("hmax Trackbar", "Hue", hmax, 180, change_hmax) highgui.cvCreateTrackbar("smin Trackbar", "Satuation", smin, 255, change_smin) highgui.cvCreateTrackbar("smax Trackbar", "Satuation", smax, 255, change_smax) highgui.cvCreateTrackbar("vmin Trackbar", "Value", vmin, 255, change_vmin) highgui.cvCreateTrackbar("vmax Trackbar", "Value", vmax, 255, change_vmax) print "grabbing camera" capture = highgui.cvCreateCameraCapture(0) print "found camera" highgui.cvSetCaptureProperty(capture, highgui.CV_CAP_PROP_FRAME_WIDTH, 320) highgui.cvSetCaptureProperty(capture, highgui.CV_CAP_PROP_FRAME_HEIGHT, 240) frame = highgui.cvQueryFrame(capture) frameSize = cv.cvGetSize(frame) hsv = cv.cvCreateImage(frameSize, 8, 3) mask = cv.cvCreateImage(frameSize, 8, 1) hue = cv.cvCreateImage(frameSize, 8, 1) satuation = cv.cvCreateImage(frameSize, 8, 1) value = cv.cvCreateImage(frameSize, 8, 1) laser = cv.cvCreateImage(frameSize, 8, 1) while 1: frame = highgui.cvQueryFrame(capture) cv.cvCvtColor(frame, hsv, cv.CV_BGR2HSV) #cv.cvInRangeS(hsv,hsv_min,hsv_max,mask) cv.cvSplit(hsv, hue, satuation, value, None) cv.cvInRangeS(hue, hmin, hmax, hue) cv.cvInRangeS(satuation, smin, smax, satuation) cv.cvInRangeS(value, vmin, vmax, value) #cv.cvInRangeS(hue,0,180,hue) cv.cvAnd(hue, value, laser) #cv.cvAnd(laser, value, laser) cenX, cenY = averageWhitePoints(laser) #print cenX,cenY draw_target(frame, cenX, cenY) #draw_target(frame,200,1) highgui.cvShowImage('Camera', frame) highgui.cvShowImage('Hue', hue) highgui.cvShowImage('Satuation', satuation) highgui.cvShowImage('Value', value) highgui.cvShowImage('Laser', laser) k = highgui.cvWaitKey(10) if k == " ": highgui.cvDestroyAllWindows() highgui.cvReleaseCapture(capture) sys.exit()
# do forever # 1. capture the current image frame = highgui.cvQueryFrame (capture) if frame is None: # no image captured... end the processing break # mirror the captured image cv.cvFlip (frame, None, 1) # compute the hsv version of the image cv.cvCvtColor (frame, hsv, cv.CV_BGR2HSV) # compute which pixels are in the wanted range cv.cvInRangeS (hsv, hsv_min, hsv_max, mask) # extract the hue from the hsv array cv.cvSplit (hsv, hue, None, None, None) # select the rectangle of interest in the hue/mask arrays hue_roi = cv.cvGetSubRect (hue, selection) mask_roi = cv.cvGetSubRect (mask, selection) # it's time to compute the histogram cv.cvCalcHist (hue_roi, hist, 0, mask_roi) # extract the min and max value of the histogram min_val, max_val = cv.cvGetMinMaxHistValue (hist, None, None) # compute the scale factor
cv.cvFlip (frame, image, 1) cv.cvCvtColor( image, hsv, cv.CV_BGR2HSV) cv.cvLine(image, cv.cvPoint(0, image.height/2), cv.cvPoint(image.width, image.height/2), cv.CV_RGB(0,255,0), 2, 8, 0 ) cv.cvLine(image, cv.cvPoint(image.width/2, 0), cv.cvPoint(image.width/2, image.height), cv.CV_RGB(0,255,0), 2, 8, 0 ) if track_object: _vmin = vmin _vmax = vmax cv.cvInRangeS( hsv, cv.cvScalar( 0, smin,min(_vmin,_vmax),0), cv.cvScalar(180, 256, max(_vmin,_vmax),0), mask ); cv.cvSplit( hsv, hue, None, None, None) if track_object < 0: max_val = 0.0 subhue = cv.cvGetSubRect(hue, selection) submask = cv.cvGetSubRect(mask, selection) cv.cvCalcHist( subhue, hist, 0, submask ) # extract the min and max value of the histogram min_val, max_val, min_idx, max_idx = cv.cvGetMinMaxHistValue (hist) if (max_val): cv.cvConvertScale( hist.bins, hist.bins, 255.0 / max_val, 0)