Пример #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 dilate(ary, N, iterations):
    kernel = np.zeros((N, N), dtype=np.uint8)
    kernel[(N - 1) / 2, :] = 1
    dilated_image = cv2.dilate(ary / 255, kernel, iterations=iterations)

    kernel = np.zeros((N, N), dtype=np.uint8)
    kernel[:, (N - 1) / 2] = 1
    dilated_image = cv2.dilate(dilated_image, kernel, iterations=iterations)
    dilated_image = cv2.convertScale(dilated_image)
    return dilated_image
Пример #3
0
 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
Пример #4
0
    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
Пример #5
0
def float_version(img):
    return cv2.convertScale(img, 1 / 255.0)
Пример #6
0
while True:
    _, color_image = capture.read()
    color_image = cv2.GaussianBlur(color_image, (21, 21), 0)

    ########################
    if not difference:  #For the first time put values in difference, temp and moving_average
        difference = color_image.copy()
        temp = color_image.copy()
        cv2.convertScaleAbs(color_image, moving_average, 1.0, 0.0)
    else:
        cv2.runningAvg(color_image, moving_average, 0.020,
                       None)  #Compute the average

    # Convert the scale of the moving average.
    cv2.convertScale(moving_average, temp, 1.0, 0.0)

    # Minus the current frame from the moving average.
    cv2.absDiff(color_image, temp, difference)

    #Convert the image so that it can be thresholded
    cv2.cvtColor(difference, grey_image, cv2.COLOR_RGB2GRAY)
    cv2.threshold(grey_image, grey_image, 70, 255, cv2.THRESH_BINARY)

    cv2.dilate(grey_image, grey_image, None, 18)  #to get object blobs
    cv2.erode(grey_image, grey_image, None, 10)

    # Find contours
    storage = cv2.createMemStorage(0)
    contours = cv2.findContours(grey_image, storage, cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)