Esempio n. 1
0
def hs_histogram(src):
    # Convert to HSV
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)

    # Extract the H and S planes
    h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    cv.Split(hsv, h_plane, s_plane, None, None)
    planes = [h_plane, s_plane]

    h_bins = 30
    s_bins = 32
    hist_size = [h_bins, s_bins]
    # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
    h_ranges = [0, 180]
    # saturation varies from 0 (black-gray-white) to
    # 255 (pure spectrum color)
    s_ranges = [0, 255]
    ranges = [h_ranges, s_ranges]
    scale = 10
    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    cv.CalcHist([cv.GetImage(i) for i in planes], hist)
    (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)

    hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3)

    for h in range(h_bins):
        for s in range(s_bins):
            bin_val = cv.QueryHistValue_2D(hist, h, s)
            intensity = cv.Round(bin_val * 255 / max_value)
            cv.Rectangle(hist_img, (h * scale, s * scale),
                         ((h + 1) * scale - 1, (s + 1) * scale - 1),
                         cv.RGB(intensity, intensity, intensity), cv.CV_FILLED)
    return hist_img
Esempio n. 2
0
def createHist(img):
    #cv.CvtColor(img,img,cv.CV_BGR2HSV)
    b_plane = cv.CreateImage((img.width,img.height), 8, 1)
    g_plane = cv.CreateImage((img.width,img.height), 8, 1)
    r_plane = cv.CreateImage((img.width,img.height), 8, 1)
 
 
    cv.Split(img,b_plane,g_plane,r_plane,None)
    planes = [b_plane, g_plane, r_plane]
    
    bins = 4
    b_bins = bins
    g_bins = bins
    r_bins = bins
 
    hist_size = [b_bins,g_bins,r_bins]
    b_range = [0,255]
    g_range = [0,255]
    r_range = [0,255]
 
    ranges = [b_range,g_range,r_range]
    hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, ranges, 1)
    cv.CalcHist([cv.GetImage(i) for i in planes], hist)
    cv.NormalizeHist(hist,1)
    return hist
Esempio n. 3
0
    def run(self):
        self.backproject_mode = False
        self.hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)

        rospy.init_node('blob_tracker')
        image_topic = rospy.resolve_name("image")
        self.bbpub = rospy.Publisher("~blob", RegionOfInterest)
        self.bppub = rospy.Publisher("~backproject", Image)
        self.disp_hist = rospy.get_param("~display_histogram", True)
        rospy.Subscriber(image_topic, Image, self.detect_and_draw)
        rate = rospy.Rate(5)
        while not rospy.is_shutdown():
            # rospy.spin_once()
            if not self.pause:
                if not self.backproject_mode:
                    cv.ShowImage("CamShiftDemo", self.frame)
                else:
                    cv.ShowImage("CamShiftDemo", self.backproject)
                if self.disp_hist:
                    cv.ShowImage("Histogram",
                                 self.hue_histogram_as_image(self.hist))
            c = cv.WaitKey(7) & 0x0FF
            if c == 27:
                rospy.signal_shutdown("OpenCV said so")
            elif c == ord("p"):
                self.pause = not self.pause
            elif c == ord("b"):
                self.backproject_mode = not self.backproject_mode
Esempio n. 4
0
 def render_with_histogram(img):
     '''Just a utility to draw a grayscale histogram next to an image.'''
     _, _, width, height = cv.GetImageROI(img)
     
     canvas = cv.CreateImage((width + 200, max(height, 255)), cv.IPL_DEPTH_8U, 1)
     cv.Rectangle(canvas, (width, 0), (width + 200, height), (0), cv.CV_FILLED)
     
     cv.SetImageROI(canvas, (0, 0, width, height))
     cv.Copy(img, canvas)
     
     cv.SetImageROI(canvas, (width, 0, 200, canvas.height))
     
     hist = cv.CreateHist([255], cv.CV_HIST_ARRAY, [(0,255)], 1)
     cv.CalcHist([img], hist)
     
     values = [cv.QueryHistValue_1D(hist, n) for n in range(255)]
     max_value = max(values)
     
     for n, value in enumerate(values):
         cv.Rectangle(canvas,
             (0, n),
             (int((value / max_value) * 200), n + 1),
             (255), cv.CV_FILLED)
     
     cv.SetImageROI(canvas, (0, 0, canvas.width, canvas.height))
     return canvas
Esempio n. 5
0
    def run(self):
        hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
        backproject_mode = True

        while True:
            frame = cv.QueryFrame(self.capture)

            # Convert to HSV and keep the hue
            hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
            cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
            self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.Split(hsv, self.hue, None, None, None)

            # Compute back projection
            backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.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 = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
                (iters, (area, value, rect),
                 track_box) = cv.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 = cv.GetSubRect(frame, self.selection)  #Get specified area

                #Make the effect of background shadow when selecting a window
                save = cv.CloneMat(sub)
                cv.ConvertScale(frame, frame, 0.5)
                cv.Copy(save, sub)

                #Draw temporary rectangle
                x, y, w, h = self.selection
                cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

                #Take the same area but in hue image to calculate histogram
                sel = cv.GetSubRect(self.hue, self.selection)
                cv.CalcArrHist([sel], hist, 0)

                #Used to rescale the histogram with the max value (to draw it later on)
                (_, max_val, _, _) = cv.GetMinMaxHistValue(hist)
                if max_val != 0:
                    cv.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
                cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3,
                              cv.CV_AA, 0)

            cv.ShowImage("CamShiftDemo", frame)
            cv.ShowImage("Backprojection", backproject)
            cv.ShowImage("Histogram", self.hue_histogram_as_image(hist))

            c = cv.WaitKey(7) % 0x100
            if c == 27:
                break
    def run(self):
        hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
        backproject_mode = False
        while True:
            frame = 0
            frame = self.capture  #cv.QueryFrame( self.capture )

            # Convert to HSV and keep the hue
            hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
            cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
            self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.Split(hsv, self.hue, None, None, None)

            # Compute back projection
            backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)

            # Run the cam-shift
            cv.CalcArrBackProject([self.hue], backproject, hist)
            #             if self.track_window and is_rect_nonzero(self.track_window):
            #                 crit = ( cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
            #                 (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit)
            #                 self.track_window = rect

            # If mouse is pressed, highlight the current selected rectangle
            # and recompute the histogram

            if self.drag_start and is_rect_nonzero(self.selection):
                sub = cv.GetSubRect(frame, self.selection)
                save = cv.CloneMat(sub)
                #cv.ConvertScale(frame, frame, 0.5)
                cv.Copy(save, sub)
                x, y, w, h = self.selection
                cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

                sel = cv.GetSubRect(self.hue, self.selection)
                cv.CalcArrHist([sel], hist, 0)
                (_, max_val, _, _) = cv.GetMinMaxHistValue(hist)
                if max_val != 0:
                    cv.ConvertScale(hist.bins, hist.bins, 255. / max_val)


#             elif self.track_window and is_rect_nonzero(self.track_window):
#                 cv.EllipseBox( frame, track_box, cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 )

            if not backproject_mode:
                cv.ShowImage("SelectROI", frame)
            else:
                cv.ShowImage("SelectROI", backproject)
            cv.ShowImage("Histogram", self.hue_histogram_as_image(hist))

            c = cv.WaitKey(7) % 0x100
            if c == 27:
                f = open('newtree.yaml', "w")
                yaml.dump(self.selection, f)
                f.close()
                break
            elif c == ord("b"):
                backproject_mode = not backproject_mode
Esempio n. 7
0
 def __init__(self, src_image):
     self.src_image = src_image
     self.dst_image = cv.CloneMat(src_image)
     self.hist_image = cv.CreateImage((320, 200), 8, 1)
     self.hist = cv.CreateHist([hist_size], cv.CV_HIST_ARRAY, ranges, 1)
     self.brightness = 0
     self.contrast = 0
     cv.NamedWindow("image", 0)
     cv.NamedWindow("histogram", 0)
     cv.CreateTrackbar("brightness", "image", 100, 200,
                       self.update_brightness)
     cv.CreateTrackbar("contrast", "image", 100, 200, self.update_contrast)
     self.update_brightcont()
Esempio n. 8
0
def camshift(x, y, w, h, selection):
    print "Performing camshift with x:{} y:{} w:{} h:{}".format(x, y, w, h)
    print selection
    hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)

    while True:
        print "entered loop"
        #camshift termination criteria (10 iterations without movement of 1 pixel ends camshift)

        frame = cv.QueryFrame(cap)
        cv.Flip(frame, frame, 1)

        #print "switching to HSV"
        hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
        cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
        hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
        cv.Split(hsv, hue, None, None, None)

        #compute back projection
        # print "back projection"
        backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)
        cv.CalcArrBackProject([hue], backproject, hist)

        #run the camshift
        #print "camshift"
        print "Selection"
        #pdb.set_trace()
        print selection
        crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
        (iters, (area, value, rect),
         track_box) = cv.CamShift(backproject, selection, crit)
        print "rect"
        print rect
        if rect[0] > 0 and rect[1] > 0:
            selection = rect
        print "SelectionNew"
        print selection
        print "track_box"
        print track_box

        #draw the surrounding ellipse
        # print "ellipse"
        cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0)

        #draw image
        #print "drawing image"
        cv.ShowImage("CamShift", frame)
        if cv.WaitKey(1) & 0xFF == ord('q'):
            break
Esempio n. 9
0
def histogram(src):
    # Set ccd sampling region.
    #     cv.SetImageROI(src, (10, 10, 100, 100))

    # Convert to HSV
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)

    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
    s_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1)
    h_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1)

    cv.Split(hsv, h_plane, s_plane, None, None)
    planes = [h_plane, s_plane]

    h_bins = 28
    s_bins = 5
    hist_size = [h_bins, s_bins]
    # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
    h_ranges = [0, 180]
    # saturation varies from 0 (black-gray-white) to
    # 255 (pure spectrum color)
    s_ranges = [0, 255]
    ranges = [h_ranges, s_ranges]
    scale = 15

    # calculate histogram
    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    cv.CalcHist([cv.GetImage(i) for i in planes], hist)
    (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)

    # Reset cv sampling region to full CCD Area
    #     cv.ResetImageROI(src)

    # plot histogram data
    hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3)

    for h in range(h_bins):
        for s in range(s_bins):
            bin_val = cv.QueryHistValue_2D(hist, h, s)
            intensity = cv.Round(bin_val * 255 / max_value)
            cv.Rectangle(hist_img, (h * scale, s * scale),
                         ((h + 1) * scale - 1, (s + 1) * scale - 1),
                         cv.RGB(intensity, intensity, intensity), cv.CV_FILLED)
    return hist_img
Esempio n. 10
0
def hs_histogram(src, mask=None):
    '''Takes a cvMat and computes a hue-saturation histogram (value is dropped)'''
    # Convert to HSV
    # Allocate the 3 HSV 8 bit channels
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    # Convert from the default BGR representation to HSV
    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)

    # Extract the H and S planes
    h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    # Copy out omitting the values we don't want
    cv.Split(hsv, h_plane, s_plane, v_plane, None)
    planes = [h_plane, s_plane]

    if 0:
        pplane('H plane', h_plane, prefix='    ')
        pplane('S plane', s_plane, prefix='    ')

    h_bins = 8
    s_bins = 8
    #hist_size = [h_bins, s_bins]
    # hue varies from 0 (~0 deg red) to 180 (~360 deg red again
    # ??? My values give a hue of 0-255
    h_ranges = [0, 255]
    # saturation varies from 0 (black-gray-white) to
    # 255 (pure spectrum color)
    s_ranges = [0, 255]
    ranges = [h_ranges, s_ranges]
    # Allocate bins
    # note that we haven't put any data in yet
    #1: uniform
    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    # Convert the array planes back into images since thats what CalcHist needs?
    # Doc seems to indcate that cvMat will work as well
    # Doesn't this cross the hue and saturate values together?  That doesn't seem to make sense, like comparing red to blue
    # Guess its a 2D histogram so it deals with where they overlap
    cv.CalcHist([cv.GetImage(i) for i in planes], hist, 0, mask)
    cv.NormalizeHist(hist, 1.0)

    return hist
Esempio n. 11
0
    def run(self):
        self.disp_hist = True
        self.backproject_mode = False
        self.hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)

        rospy.init_node('blob_tracker')
        while not rospy.is_shutdown():
            # rospy.spin_once()
            if not self.pause:
                self.detect_and_draw()
                if not self.backproject_mode:
                    cv.ShowImage("CamShiftDemo", self.frame)
                else:
                    cv.ShowImage("CamShiftDemo", self.backproject)
                if self.disp_hist:
                    cv.ShowImage("Histogram",
                                 self.hue_histogram_as_image(self.hist))
            c = cv.WaitKey(7) & 0x0FF
            if c == 27:
                rospy.signal_shutdown("OpenCV said so")
            elif c == ord("p"):
                self.pause = not self.pause
            elif c == ord("b"):
                self.backproject_mode = not self.backproject_mode
Esempio n. 12
0
    def run(self):
        hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
        backproject_mode = False
        i = 1
        o_x = 0
        o_y = 0
        while True:
            frame = cv.QueryFrame(self.capture)

            # Convert to HSV and keep the hue
            hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
            cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
            self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.Split(hsv, self.hue, None, None, None)

            # Compute back projection
            backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)

            # Run the cam-shift
            cv.CalcArrBackProject([self.hue], backproject, hist)
            if self.track_window and is_rect_nonzero(self.track_window):
                crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
                (iters, (area, value, rect),
                 track_box) = cv.CamShift(backproject, self.track_window, crit)
                self.track_window = rect

            # If mouse is pressed, highlight the current selected rectangle
            # and recompute the histogram

            if self.drag_start and is_rect_nonzero(self.selection):
                sub = cv.GetSubRect(frame, self.selection)
                save = cv.CloneMat(sub)
                cv.ConvertScale(frame, frame, 0.5)
                cv.Copy(save, sub)
                x, y, w, h = self.selection
                cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

                sel = cv.GetSubRect(self.hue, self.selection)
                cv.CalcArrHist([sel], hist, 0)
                (_, max_val, _, _) = cv.GetMinMaxHistValue(hist)
                if max_val != 0:
                    cv.ConvertScale(hist.bins, hist.bins, 255. / max_val)
            elif self.track_window and is_rect_nonzero(self.track_window):
                cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3,
                              cv.CV_AA, 0)
                #print track_box
                trace_val = track_box[0]
                f_x = trace_val[0]
                f_y = trace_val[1]
                print 'value1', f_x
                print 'value2', f_y
                if i % 10 == 0:
                    o_x = f_x
                    o_y = f_y
                if (f_x != o_x):
                    a = (f_x - o_x) / float(10)
                    round(a)
                    cam.Azimuth(-a)
                if (f_y != o_y):
                    a = (f_y - o_y) / float(10)
                    round(a)
                    cam.Elevation(-a)
                ren1.ResetCameraClippingRange()
                renWin.Render()
                i += 1

            if not backproject_mode:
                cv.ShowImage("CamShiftDemo", frame)
            else:
                cv.ShowImage("CamShiftDemo", backproject)
            cv.ShowImage("Histogram", self.hue_histogram_as_image(hist))

            c = cv.WaitKey(7) % 0x100
            if c == 27:
                break
            elif c == ord("b"):
                backproject_mode = not backproject_mode
Esempio n. 13
0
def detectObject(filename):
    img=cv.LoadImage(filename)
    '''
    #get color histogram
    '''
   
#     im32f=np.zeros((img.shape[:2]),np.uint32)
    hist_range=[[0,256],[0,256],[0,256]]
    im32f=cv.CreateImage(cv.GetSize(img), cv2.IPL_DEPTH_32F, 3)
    cv.ConvertScale(img, im32f)
    
    
    hist=cv.CreateHist([32,32,32],cv.CV_HIST_ARRAY,hist_range,3)
    '''
    #create three histogram'''
    b=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1)
    g=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1)
    r=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1)
    
   
    '''
    #create image backproject 32f, 8u
    '''
    backproject32f=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_32F,1)
    backproject8u=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_8U,1)
    '''
    #create binary
    '''
    bw=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_8U,1)
    '''
    #create kernel image
    '''
    kernel=cv.CreateStructuringElementEx(3, 3, 1, 1, cv2.MORPH_ELLIPSE)
    cv.Split(im32f, b, g, r,None)

    planes=[b,g,r]
    cv.CalcHist(planes, hist)
    '''
    #find min and max histogram bin.
    '''
    minval=maxval=0.0
    min_idx=max_idx=0
    minval, maxval, min_idx, max_idx=cv.GetMinMaxHistValue(hist)
    '''
    # threshold histogram.  this sets the bin values that are below the threshold
    to zero
    '''
    cv.ThreshHist(hist, maxval/32.0)
    '''
    #backproject the thresholded histogram, backprojection should contian higher values for
    #background and lower values for the foreground
    '''
    cv.CalcBackProject(planes, backproject32f, hist)
    '''
    #convert to 8u type
    '''
    val_min=val_max=0.0
    idx_min=idx_max=0
    val_min,val_max,idx_min,idx_max=cv.MinMaxLoc(backproject32f)
    cv.ConvertScale(backproject32f, backproject8u,255.0/maxval)
    '''
    #threshold backprojected image. this gives us the background
    '''
    cv.Threshold(backproject8u, bw, 10, 255, cv2.THRESH_BINARY)
    '''
    #some morphology on background
    '''
    cv.Dilate(bw, bw,kernel,1)
    cv.MorphologyEx(bw, bw, None,kernel, cv2.MORPH_CLOSE, 2)
    '''
    #get the foreground
    '''
    cv.SubRS(bw,cv.Scalar(255,255,255),bw)
    cv.MorphologyEx(bw, bw, None, kernel,cv2.MORPH_OPEN,2)
    cv.Erode(bw, bw, kernel, 1)
    '''
    #find contours of foreground
    #Grabcut
    '''
    size=cv.GetSize(bw)
    color=np.asarray(img[:,:])
    fg=np.asarray(bw[:,:])
#     mask=cv.CreateMat(size[1], size[0], cv2.CV_8UC1)
    '''
    #Make anywhere black in the grey_image (output from MOG) as likely background
    #Make anywhere white in the grey_image (output from MOG) as definite foreground
    '''
    rect = (0,0,0,0)
   
    mat_mask=np.zeros((size[1],size[0]),dtype='uint8')
    mat_mask[:,:]=fg
    
    mat_mask[mat_mask[:,:] == 0] = 2
    mat_mask[mat_mask[:,:] == 255] = 1
    
    '''
    #Make containers 
    '''                               
    bgdModel = np.zeros((1, 13 * 5))
    fgdModel = np.zeros((1, 13 * 5))
    cv2.grabCut(color, mat_mask, rect, bgdModel, fgdModel,cv2.GC_INIT_WITH_MASK)
    '''
    #Multiple new mask by original image to get cut
    '''
    mask2 = np.where((mat_mask==0)|(mat_mask==2),0,1).astype('uint8')  
    gcfg=np.zeros((size[1],size[0]),np.uint8)
    gcfg=mask2
    
    img_cut = color*mask2[:,:,np.newaxis]

    contours, hierarchy=cv2.findContours(gcfg ,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    
    for cnt in contours:
        print cnt
        rect_box = cv2.minAreaRect(cnt)
        box = cv2.cv.BoxPoints(rect_box)
        box = np.int0(box)
        cv2.drawContours(color,[box], 0,(0,0,255),2)
    cv2.imshow('demo', color)
    cv2.waitKey(0)
Esempio n. 14
0
def compute_histogram(src, bins=255):
    hist = cv.CreateHist([255], cv.CV_HIST_ARRAY, ranges=[(0, 256)])
    cv.CalcHist([src], hist)  #compute histogram
    cv.NormalizeHist(hist, 1.0)  #normalize hist
    return hist
Esempio n. 15
0
    minV, maxV, minloc, maxloc = cv.MinMaxLoc(ar)  #Get the min and max value
    hpt = 0.9 * histsize
    for i in range(size):
        intensity = ar[
            i] * hpt / maxV  #Calculate the intensity to make enter in the image
        cv.Line(im, (i, size), (i, int(size - intensity)),
                cv.Scalar(255, 255, 255))  #Draw the line
        i += 1


#---- Gray image
orig = cv.LoadImage("meinv.jpg", cv.CV_8U)

histsize = 256  #Because we are working on grayscale pictures which values within 0-255

hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0, histsize]], 1)

cv.CalcHist([orig], hist)  #Calculate histogram for the given grayscale picture

histImg = cv.CreateMat(
    histsize, histsize,
    cv.CV_8U)  #Image that will contain the graph of the repartition of values
drawGraph(hist.bins, histImg, histsize)

cv.ShowImage("Original Image", orig)
cv.ShowImage("Original Histogram", histImg)
#---------------------

#---- Equalized image
imEq = cv.CloneImage(orig)
cv.EqualizeHist(imEq, imEq)  #Equlize the original image
Esempio n. 16
0
import cv2.cv as cv
import math
import time

while True:
    h = cv.CreateHist([40], cv.CV_HIST_ARRAY, [[0,255]], 1)
Esempio n. 17
0
 def HoG(self, tangent, magnitude, writing_method = ""):
     
     if writing_method != "":
         self.writing_method = writing_method
         
     if self.image_check(tangent) < 0 or self.image_check(magnitude) < 0: 
         return -1
     size = cv.GetSize(tangent)
     
     
     if self.writing_method == "cv":
         #Create histograms using tangent function. Magnitude is only used as mask 
         #ranges = [(-181,-157,-112,-67,-22,22,67,112,157,181)]
         ranges= [(0,360)]
         
                     
         #ranges = [[0,22,67,112,157,202,247,292,337,360]]
         hist = cv.CreateHist([8], cv.CV_HIST_ARRAY, ranges,1)
         cv.CalcHist([tangent],hist,accumulate=0,mask=magnitude)
         
     elif self.writing_method == "numpy":
         #DONT USE THIS: WRONG IMPLEMENTATION ATM
         #Using magnitude to give ranks/value to tangent picture. Then, the histogram is created
         tangent_asarray = numpy.asarray(tangent[:,:])
         magnitude_asarray = numpy.asarray(magnitude[:,:])
         
         tangent_result = numpy.multiply(tangent_asarray, numpy.divide(magnitude_asarray,255))
          
         normalizedTangent = cv.CreateImageHeader((tangent_result.shape[1], tangent_result.shape[0]), cv.IPL_DEPTH_32F, 1)
         cv.SetData(normalizedTangent, tangent_result.tostring(), tangent_result.dtype.itemsize * 1 * tangent_result.shape[1])
         #ranges = [(-181,-157,-112,-67,-22,22,67,112,157,181)]
         ranges= [(0,360)]
         
                     
         #ranges = [[0,22,67,112,157,202,247,292,337,360]]
         hist = cv.CreateHist([8], cv.CV_HIST_ARRAY, ranges,1)
         cv.CalcHist([normalizedTangent],hist,accumulate=0,mask=magnitude)            
        
     else:
         #Very slow approach. Use numpy case instead
         hist = numpy.zeros(8)
         for x in range(size[0]):
             for y in range(size[1]):
                 if magnitude[y,x] > 0:
                     if tangent[y,x] < -157.5 or tangent[y,x] > 157.5:
                         hist[0] += 1 *magnitude[y,x]/255
                     elif tangent[y,x] < -112.5 and tangent[y,x] > -157.5 :
                         hist[7] += 1 *magnitude[y,x]/255
                     elif tangent[y,x] < -67.5 and tangent[y,x] > -112.5 :
                         hist[6] += 1 *magnitude[y,x]/255
                     elif tangent[y,x] < -22.5 and tangent[y,x] > -67.5:
                         hist[5] += 1 *magnitude[y,x]/255
                     elif tangent[y,x] < 22.5 and tangent[y,x] > -22.5:
                         hist[4] += 1 *magnitude[y,x]/255
                     elif tangent[y,x] < 67.5 and tangent[y,x] > 22.5:
                         hist[3] += 1 *magnitude[y,x]/255
                     elif tangent[y,x] < 112.5 and tangent[y,x] > 67.5:
                         hist[2] += 1 *magnitude[y,x]/255
                     elif tangent[y,x] < 157.5 and tangent[y,x] > 112.5:
                         hist[1] += 1 *magnitude[y,x]/255
                         
         #sum = numpy.sum(hist)
          
         #return numpy.divide(hist,sum)      
     if self.visualize:
         pass #draw the histogram
     
     return hist
Esempio n. 18
0
planes = [h_plane, s_plane]

#s_plane = cv.Threshold(s_plane, s_plane, minSat, 255, cv.CV_THRESH_BINARY)

h_bins = 30
s_bins = 32
hist_size = [h_bins, s_bins]

# hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
h_ranges = [0, 180]
# saturation varies from 0 (black-gray-white) to
# 255 (pure spectrum color)
s_ranges = [0, 255]
ranges = [h_ranges, s_ranges]
scale = 10
hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
cv.CalcHist([cv.GetImage(i) for i in planes], hist)

#SetHistogramm ???
#Normalize

im2 = cv.LoadImage("../img/build.png")
hsv2 = cv.CreateImage(cv.GetSize(im2), 8, 3)
cv.CvtColor(im2, hsv2, cv.CV_BGR2HSV)
h_plane2 = cv.CreateImage(cv.GetSize(im2), 8, 1)
s_plane2 = cv.CreateImage(cv.GetSize(im2), 8, 1)
cv.Split(hsv2, h_plane2, s_plane2, None, None)

res = cv.CreateImage((im2.width, im2.height), 8, 3)
cv.CalcBackProject([h_plane2, s_plane2], res, hist)
Esempio n. 19
0
import cv2.cv as cv
import numpy as np
import cv2
import pdb

#needed for camshift
hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
backproject_mode = False

print "Press Q to quit."
cap = cv.CaptureFromCAM(0)


def is_rect_nonzero(r):
    (_, _, w, h) = r
    return (w > 0) and (h > 0)


def draw_detections(img, rects, thickness=1):
    #for x, y, w, h in rects:
    x = rects[0]
    y = rects[1]
    w = rects[2]
    h = rects[3]
    # the HOG detector returns slightly larger rectangles than the real obj$
    # so we slightly shrink the rectangles to get a nicer output.
    pad_w, pad_h = int(0.15 * w), int(0.05 * h)
    cv2.rectangle(img, (x + pad_w, y + pad_h), (x + w - pad_w, y + h - pad_h),
                  (0, 255, 0), thickness)

Esempio n. 20
0
    def run(self):
        hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
        backproject_mode = False
        print "hitting run section"
        x = 0
        while True:
            #print x
            #x = x + 1
            frame = cv.QueryFrame(self.capture)
            cv.Flip(frame, frame, 1)

            # Convert to HSV and keep the hue
            hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
            cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
            self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
            cv.Split(hsv, self.hue, None, None, None)

            # Compute back projection
            backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)

            # Run the cam-shift
            cv.CalcArrBackProject([self.hue], backproject, hist)
            if self.track_window and is_rect_nonzero(self.track_window):
                crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
                print self.track_window
                (iters, (area, value, rect),
                 track_box) = cv.CamShift(backproject, self.track_window, crit)
                self.track_window = rect
                print self.track_window
            try:
                #prints the center x and y value of the tracked ellipse
                coord = track_box[0]
                print "center = {}".format(coord)
                if (coord[0] < 320):
                    print "move right"
                # ser.write("R")
                elif (coord[0] == 320):
                    print "do nothing"
                else:
                    print "move left"
                # ser.write("L")
            except UnboundLocalError:
                print "track_box is None"

            # If mouse is pressed, highlight the current selected rectangle
            # and recompute the histogram

            if self.drag_start and is_rect_nonzero(self.selection):
                sub = cv.GetSubRect(frame, self.selection)
                save = cv.CloneMat(sub)
                cv.ConvertScale(frame, frame, 0.5)
                cv.Copy(save, sub)
                x, y, w, h = self.selection
                cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

                sel = cv.GetSubRect(self.hue, self.selection)
                cv.CalcArrHist([sel], hist, 0)
                (_, max_val, _, _) = cv.GetMinMaxHistValue(hist)
                if max_val != 0:
                    cv.ConvertScale(hist.bins, hist.bins, 255. / max_val)
            elif self.track_window and is_rect_nonzero(self.track_window):
                print track_box
                cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3,
                              cv.CV_AA, 0)

            if not backproject_mode:
                cv.ShowImage("CamShiftDemo", frame)
            else:
                cv.ShowImage("CamShiftDemo", backproject)
            cv.ShowImage("Histogram", self.hue_histogram_as_image(hist))

            c = cv.WaitKey(7) % 0x100
            if c == 27:
                break
            elif c == ord("b"):
                backproject_mode = not backproject_mode