Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def hsBackProjectHist(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
    hsv = im.asHSV()

    h_ranges = [0, 180]
    s_ranges = [0, 255]
    ranges = h_ranges + s_ranges

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

        # pixel value ranges
        h_ranges = [0, 180]
        s_ranges = [0, 255]
        ranges = h_ranges + s_ranges

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

        #fg_hist =
        #cv2.calcProbDensity(bg_hist.hist, fg_hist.hist, prob_hist, scale=255)

        fg_hist = pv.Histogram(fg_hist.hist / bg_hist.hist, pv.HIST_HS,
                               fg_hist.nbins1, fg_hist.nbins2, None)

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

    # Normalize the histogram
    fg_hist.rescaleMax(255)

    #cv.CalcBackProject(planes,output,fg_hist.hist)
    #calcBackProject(images, channels, hist, ranges, scale[, dst]) -> dst
    output = cv2.calcBackProject([hsv], [0, 1], fg_hist.hist, ranges, 1.0)
    return pv.Image(output)
Ejemplo n.º 3
0
def RGBHist(im,r_bins=8,g_bins=8,b_bins=8,mask=None,normalize=True):
    '''
    Compute the rgb saturation histogram of an image. (Based on OpenCV example code).
    
    @param im: the image
    @type im: pv.Image
    @param r_bins: the number of bins for hue.
    @type r_bins: int
    @param g_bins: the number of bins for hue.
    @type g_bins: int
    @param b_bins: the number of bins for hue.
    @type b_bins: int
    @param mask: an image containing a mask
    @type mask: cv.Image or np.array(dtype=np.bool)
    @return: an OpenCV histogram
    @rtype: pv.Histogram
    '''
    w,h = im.size
    bgr = im.asOpenCV()

    # 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]

    # set the histogram size
    hist_size = [b_bins, g_bins, r_bins]
    
    # 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    
    hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, ranges, 1)
    if mask != None:
        mask = mask.asOpenCVBW()
    cv.CalcHist(planes, hist, mask=mask)
    
    return pv.Histogram(hist,HIST_RGB,b_bins,g_bins,r_bins)
Ejemplo n.º 4
0
def HSHist(im,h_bins=32,s_bins=32,mask=None,normalize=True):
    '''
    Compute the hue saturation histogram of an image. (Based on OpenCV example code).
    
    @param im: the image
    @type im: pv.Image
    @param h_bins: the number of bins for hue.
    @type h_bins: int
    @param s_bins: the number of bins for saturation.
    @type s_bins: int
    @param mask: an image containing a mask
    @type mask: cv.Image or np.array(dtype=np.bool)
    @return: an OpenCV histogram
    @rtype: pv.Histogram
    '''
    w,h = im.size
    hsv = im.asHSV()

    # Extract the H and S planes
    h_plane = cv.CreateImage((w,h), cv.IPL_DEPTH_8U, 1)
    s_plane = cv.CreateImage((w,h), cv.IPL_DEPTH_8U, 1)
    cv.Split(hsv, h_plane, s_plane, None, None)
    planes = [h_plane, s_plane]

    # set the histogram size
    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]

    # Calculate the histogram    
    hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, ranges, 1)
    if mask != None:
        mask = mask.asOpenCVBW()
    cv.CalcHist(planes, hist,mask=mask)
    
    return pv.Histogram(hist,HIST_HS,h_bins,s_bins,None)
Ejemplo n.º 5
0
def HSHist(im, h_bins=32, s_bins=32, mask=None, normalize=True):
    '''
    Compute the hue saturation histogram of an image. (Based on OpenCV example code).
    
    @param im: the image
    @type im: pv.Image
    @param h_bins: the number of bins for hue.
    @type h_bins: int
    @param s_bins: the number of bins for saturation.
    @type s_bins: int
    @param mask: an image containing a mask
    @type mask: cv.Image or np.array(dtype=np.bool)
    @return: an OpenCV histogram
    @rtype: pv.Histogram
    '''
    w, h = im.size
    hsv = im.asHSV()

    # set the histogram size
    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

    channels = (0, 1)

    # Calculate the histogram
    if mask != None:
        mask = mask.asOpenCV2BW()

    hist = cv2.calcHist([hsv], channels, None, hist_size, ranges)

    return pv.Histogram(hist, HIST_HS, h_bins, s_bins, None)