def run(self): hist = cv2.createHist([180], cv2.CV_HIST_ARRAY, [(0,180)], 1 ) backproject_mode = True while True: frame = cv2.QueryFrame( self.capture ) # Convert to HSV and keep the hue hsv = cv2.createImage(cv2.GetSize(frame), 8, 3) cv2.cvtColor(frame, hsv, cv2.CV_BGR2HSV) self.hue = cv2.createImage(cv2.GetSize(frame), 8, 1) cv2.split(hsv, self.hue, None, None, None) # Compute back projection backproject = cv2.createImage(cv2.GetSize(frame), 8, 1) cv2.calcArrBackProject( [self.hue], backproject, hist ) # Run the cam-shift (if the a window is set and != 0) if self.track_window and is_rect_nonzero(self.track_window): crit = ( cv2.CV_TERMCRIT_EPS | cv2.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv2.camShift(backproject, self.track_window, crit) #Call the camshift !! self.track_window = rect #Put the current rectangle as the tracked area # If mouse is pressed, highlight the current selected rectangle and recompute histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv2.getSubRect(frame, self.selection) #Get specified area #Make the effect of background shadow when selecting a window save = cv2.cloneMat(sub) cv2.convertScale(frame, frame, 0.5) cv2.copy(save, sub) #Draw temporary rectangle x,y,w,h = self.selection cv2.rectangle(frame, (x,y), (x+w,y+h), (255,255,255)) #Take the same area but in hue image to calculate histogram sel = cv2.getSubRect(self.hue, self.selection ) cv2.calcArrHist( [sel], hist, 0) #Used to rescale the histogram with the max value (to draw it later on) (_, max_val, _, _) = cv2.getMinMaxHistValue( hist) if max_val != 0: cv2.convertScale(hist.bins, hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): #If window set draw an elipseBox cv2.ellipseBox( frame, track_box, cv2.CV_RGB(255,0,0), 3, cv2.CV_AA, 0 ) cv2.showImage( "CamShiftDemo", frame ) cv2.showImage( "Backprojection", backproject) cv2.showImage( "Histogram", self.hue_histogram_as_image(hist)) c = cv2.waitKey(7) % 0x100 if c == 27: break
def set_frame(self,frame): #This function sets the picture to a desired frame size #self - object of this class #frame - captured frame jpegPIL = Image.fromstring("RGB",(640,480),frame,"jpeg","RGB","raw") cv_im = cv2.createImage((640,480),cv.IPL_DEPTH_8U,3) cv2.setData(cv_im,jpegPIL.tostring()) cv2.showImage(self.name,cv_im)
def DoCanny(img, lowThresh, highThresh, aperature): """ from chapter 2 of the book "Learning OpenCV: Computer Vision with the OpenCV Library", ISBN-10: 0596516134 also found on http://www.beechtreetech.com/dev/opencv-exercises-in-python.aspx -> example 2.6 """ gray = cv2.createImage(cvSize(cvGetSize(img).width, cvGetSize(img).height), IPL_DEPTH_8U, 1) cv2.cvtColor(img,gray,cv2.CV_BGR2GRAY) if (gray.nChannels != 1): return False out = cv2.Canny(gray, lowThresh, highThresh, aperature) return out
def hue_histogram_as_image(self, hist): """ Returns a nice representation of a hue histogram """ histimg_hsv = cv2.createImage( (320,200), 8, 3) mybins = cv2.cloneMatND(hist.bins) #Contain all values cv2.log(mybins, mybins) #Calculate logarithm of all values (so there are all above 0) (_, hi, _, _) = cv2.MinMaxLoc(mybins) cv2.convertScale(mybins, mybins, 255. / hi) #Rescale all element to get the highest at 255 w,h = cv2.getSize(histimg_hsv) hdims = cv2.getDims(mybins)[0] for x in range(w): xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image val = int(mybins[int(hdims * x / w)] * h / 255) cv2.rectangle( histimg_hsv, (x, 0), (x, h-val), (xh,255,64), -1) cv2.rectangle( histimg_hsv, (x, h-val), (x, h), (xh,255,255), -1) histimg = cv2.createImage( (320,200), 8, 3) #Convert image from hsv to RGB cv2.cvtColor(histimg_hsv, histimg, cv2.CV_HSV2BGR) return histimg
def run(self): while True: img = self.capture.read() #blur the source image to reduce color noise cv2.medianBlur(img, 5) cv2.smooth(img, img, cv.CV_BLUR, 3) #convert the image to hsv(Hue, Saturation, Value) so its #easier to determine the color to track(hue) hsv_img = cv2.createImage(cv2.getSize(img), 8, 3) cv2.cvtColor(img, hsv_img, CV_BGR2HSV) #limit all pixels that don't match our criteria, in this case we are #looking for purple but if you want you can adjust the first value in #both turples which is the hue range(120,140). OpenCV uses 0-180 as #a hue range for the HSV color model thresholded_img = cv.CreateImage(cv.GetSize(hsv_img), 8, 1) cv.InRangeS(hsv_img, (120, 80, 80), (140, 255, 255), thresholded_img) #determine the objects moments and check that the area is large #enough to be our object moments = cv.Moments(thresholded_img, 0) area = cv.GetCentralMoment(moments, 0, 0) #there can be noise in the video so ignore objects with small areas if (area > 100000): #determine the x and y coordinates of the center of the object #we are tracking by dividing the 1, 0 and 0, 1 moments by the area x = cv.GetSpatialMoment(moments, 1, 0) / area y = cv.GetSpatialMoment(moments, 0, 1) / area #print 'x: ' + str(x) + ' y: ' + str(y) + ' area: ' + str(area) #create an overlay to mark the center of the tracked object overlay = cv.CreateImage(cv.GetSize(img), 8, 3) cv.Circle(overlay, (x, y), 2, (255, 255, 255), 20) cv.Add(img, overlay, img) #add the thresholded image back to the img so we can see what was #left after it was applied cv.Merge(thresholded_img, None, None, None, img) #display the image cv.ShowImage(color_tracker_window, img) if cv.WaitKey(10) == 27: break
def hue_histogram_as_image(self, hist): """ Returns a nice representation of a hue histogram """ histimg_hsv = cv2.createImage((320, 200), 8, 3) mybins = cv2.cloneMatND(hist.bins) cv2.log(mybins, mybins) (_, hi, _, _) = cv2.minMaxLoc(mybins) cv2.convertScale(mybins, mybins, 255. / hi) w,h = cv2.getSize(histimg_hsv) hdims = cv2.getDims(mybins)[0] for x in range(w): xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image val = int(mybins[int(hdims * x / w)] * h / 255) cv2.rectangle(histimg_hsv, (x, 0), (x, h-val), (xh,255,64), -1) cv2.rectangle(histimg_hsv, (x, h-val), (x, h), (xh,255,255), -1) histimg = cv2.cvtColor(histimg_hsv, cv2.COLOR_HSV2BGR) return histimg