예제 #1
0
def main():
    img = cv.LoadImage("sample/image3114.png")
    h, s, v = get_hsv_planes(img)

    hist = get_gray_histogram(h, 180, 180)
    clear_hist_in_range(hist, *h2cv_values(50, 340))

    hist_img = get_hist_image(hist, 180, 500)
    h_mask = image_empty_clone(h)
    cv.CalcBackProject((h, ), h_mask, hist)

    show_images({"img": img, "h": h, "h_mask": h_mask, "hist_img": hist_img})
예제 #2
0
def hs_histogram(src, patch):
    # Convert to HSV
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
    hsv_patch= cv.CreateImage(cv.GetSize(patch), 8, 3)

    # Extract the H and S planes
    h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    h_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1)
    h_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1)
    s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    s_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1)
    s_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1)
    v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)

    cv.Split(hsv, h_plane, s_plane, v_plane, None)
    cv.Split(hsv, h_plane_img, s_plane_img, None, None)
    cv.Split(hsv_patch, h_plane_patch, s_plane_patch, None, None)
    #cv.Split(src, h_plane, s_plane, v_plane, None)
    planes = [h_plane_patch, s_plane_patch]#, s_plane, v_plane]

    h_bins = 30
    s_bins = 32
    v_bins = 30
    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]
    v_ranges = [0, 255]
    ranges = [h_ranges, s_ranges]#, s_ranges, v_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)

    back_proj_img = cv.CreateImage(cv.GetSize(src), 8, 1)
    cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, hist)
    
    # 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 back_proj_img, hist
예제 #3
0
파일: color.py 프로젝트: mdqyy/pyvision
def rgbBackProjectHist(im, fg_hist, bg_hist=None):
    '''
    Compute the hue saturation histogram of an image. (Based on OpenCV example code).
    
    @param im: the image
    @type im: pv.Image
    @param fg_hist: the histogram
    @type fg_hist: pv.Histogram
    @param bg_hist:
    @type bg_hist: pv.Histogram
    @return: an OpenCV histogram
    @rtype: pv.Image
    '''
    w, h = im.size
    bgr = im.asOpenCV()

    if bg_hist != None:
        # set the histogram size
        hist_size = [fg_hist.nbins1, fg_hist.nbins2, fg_hist.nbins3]

        # pixel value ranges
        b_ranges = [0, 255]
        g_ranges = [0, 255]
        r_ranges = [0, 255]
        ranges = [b_ranges, g_ranges, r_ranges]

        # Calculate the histogram
        prob_hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, ranges, 1)

        fg_hist.rescaleMax(255)
        bg_hist.rescaleMax(255)

        cv.CalcProbDensity(bg_hist.hist, fg_hist.hist, prob_hist, scale=255)

        fg_hist = pv.Histogram(prob_hist, pv.HIST_HS, fg_hist.nbins1,
                               fg_hist.nbins2, None)

    # Extract the H and S planes
    b_plane = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 1)
    g_plane = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 1)
    r_plane = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 1)
    cv.Split(bgr, b_plane, g_plane, r_plane, None)
    planes = [b_plane, g_plane, r_plane]

    output = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 1)

    # Normalize the histogram
    fg_hist.rescaleMax(255)

    cv.CalcBackProject(planes, output, fg_hist.hist)

    return pv.Image(output)
예제 #4
0
 def back_project_hs(self, img):
     self.check_for_hist()
     hsv = cv.CreateImage(cv.GetSize(img), 8, 3)
     scratch = cv.CreateImage(cv.GetSize(img), 8, 1)
     back_proj_img = cv.CreateImage(cv.GetSize(img), 8, 1)
     cv.CvtColor(img, hsv, cv.CV_BGR2HSV)
     h_plane_img = cv.CreateImage(cv.GetSize(img), 8, 1)
     s_plane_img = cv.CreateImage(cv.GetSize(img), 8, 1)
     cv.Split(hsv, h_plane_img, s_plane_img, None, None)            
     cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, self.hist)
     #cv.Threshold(back_proj_img, back_proj_img, 200, 255, cv.CV_THRESH_BINARY)
     #cv.MorphologyEx(back_proj_img, back_proj_img, None, None, cv.CV_MOP_OPEN, 1)
     #cv.MorphologyEx(back_proj_img, back_proj_img, None, None, cv.CV_MOP_CLOSE, 2)    
     return back_proj_img, self.hist
예제 #5
0
def back_project_hs(src, patch):
    # Convert to HSV
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
    hsv_patch= cv.CreateImage(cv.GetSize(patch), 8, 3)
    cv.CvtColor(patch, hsv_patch, cv.CV_BGR2HSV)

    # Extract the H and S planes
    h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    h_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1)
    h_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1)
    s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)
    s_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1)
    s_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1)
    v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1)

    cv.Split(hsv, h_plane, s_plane, v_plane, None)
    cv.Split(hsv, h_plane_img, s_plane_img, None, None)
    cv.Split(hsv_patch, h_plane_patch, s_plane_patch, None, None)
    #cv.Split(src, h_plane, s_plane, v_plane, None)
    planes = [h_plane_patch, s_plane_patch]#, s_plane, v_plane]
    # planes = [s_plane_patch]#, s_plane, v_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]
    s_ranges = [0, 255]
    # saturation varies from 0 (black-gray-white) to
    # 255 (pure spectrum color)
    ranges = [h_ranges, s_ranges]#, s_ranges, v_ranges]
    #ranges = [s_ranges]#, s_ranges, v_ranges]
    scale = 1
    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    #hist = cv.CreateHist([s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    cv.CalcHist([cv.GetImage(i) for i in planes], hist)
    
    (min_value, max_value, _, _) = cv.GetMinMaxHistValue(hist)

    #cv.NormalizeHist(hist, 20*250.0)
    print "min hist value is :", min_value
    print "max hist value is :", max_value

    back_proj_img = cv.CreateImage(cv.GetSize(src), 8, 1)

    #cv.NormalizeHist(hist, 2000)

    cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, hist)
    back_modified = cv.CreateImage(cv.GetSize(src), 8, 1)
    back_modified2 = cv.CreateImage(cv.GetSize(src), 8, 1)

    # cv.Dilate(back_proj_img, back_proj_img)
    # cv.Erode(back_proj_img, back_proj_img)
    #cv.Smooth(back_proj_img, back_modified)
    
    #cv.AdaptiveThreshold(back_proj_img, back_modified, 255, adaptive_method=cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C)
    #cv.Threshold(back_proj_img, back_modified, 250, 255, cv.CV_THRESH_BINARY)
    #cv.MorphologyEx(back_modified,back_modified2, None, None, cv.CV_MOP_CLOSE, 3)
    #cv.MorphologyEx(back_modified,back_modified2, None, None, cv.CV_MOP_CLOSE, 1)    
    # cv.MorphologyEx(back_proj_img,back_modified2, None, None, cv.CV_MOP_CLOSE, 1)
    #cv.MorphologyEx(back_modified2,back_modified2, None, None, cv.CV_MOP_OPEN, 2)    




    cv.MorphologyEx(back_proj_img,back_modified, None, None, cv.CV_MOP_OPEN, 1)
    cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_CLOSE, 2)    
    cv.Threshold(back_modified, back_modified, 250, 255, cv.CV_THRESH_BINARY)

    # cv.MorphologyEx(back_proj_img,back_modified2, None, None, cv.CV_MOP_CLOSE, 1)
    # cv.MorphologyEx(back_modified2,back_modified2, None, None, cv.CV_MOP_OPEN, 2)    




    #cv.FloodFill(back_modified, (320, 240), cv.Scalar(255), cv.Scalar(30), cv.Scalar(30), flags=8)

    # for i in xrange (10):
    #     cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_OPEN, 3)
    #     cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_CLOSE, 1)    

    
    #cv.SubRS(back_modified, 255, back_modified)

    # cv.CalcBackProject([s_plane_img], back_proj_img, hist)
    # cv.Scale(back_proj_img, back_proj_img, 30000)
    cv.ShowImage("back_projection", back_proj_img)
    cv.ShowImage("back_modified", back_modified)
    cv.ShowImage("back_modified2", back_modified2)

    cv.WaitKey(0)


    
    #return back_proj_img, hist
    return back_modified, hist