Пример #1
0
    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
Пример #2
0
def grey_histogram(img, nBins=64):
    """
    Returns a one dimension histogram for the given image
    The image is expected to have one channel, 8 bits depth
    nBins can be defined between 1 and 255 
    """
    hist_size = [nBins]
    # h_ranges = [0, 255]
    hist = cv2.createHist(hist_size, cv2.CV_HIST_ARRAY, [[0, 255]], 1)
    cv2.calcHist([img], hist)

    return hist