Ejemplo 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 plane
    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 to 180
    h_ranges = [0, 180]
    # saturation varies from 0 to 255
    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
Ejemplo n.º 2
0
def calcEM(hist1, hist2, h_bins, s_bins):
    #Define number of rows
    numRows = h_bins * s_bins

    sig1 = cv.CreateMat(numRows, 3, cv.CV_32FC1)
    sig2 = cv.CreateMat(numRows, 3, cv.CV_32FC1)

    for h in range(h_bins):
        for s in range(s_bins):
            bin_val = cv.QueryHistValue_2D(hist1, h, s)
            cv.Set2D(sig1, h * s_bins + s, 0, cv.Scalar(bin_val))
            cv.Set2D(sig1, h * s_bins + s, 1, cv.Scalar(h))
            cv.Set2D(sig1, h * s_bins + s, 2, cv.Scalar(s))

            bin_val = cv.QueryHistValue_2D(hist2, h, s)
            cv.Set2D(sig2, h * s_bins + s, 0, cv.Scalar(bin_val))
            cv.Set2D(sig2, h * s_bins + s, 1, cv.Scalar(h))
            cv.Set2D(sig2, h * s_bins + s, 2, cv.Scalar(s))

    #This is the important line were the OpenCV EM algorithm is called
    return cv.CalcEMD2(sig1, sig2, cv.CV_DIST_L2)
Ejemplo n.º 3
0
 def asMatrix(self):
     if self.nbins2 == None:
         result = np.zeros([self.nbins1])
         for i in range(self.nbins1):
             result[i] = cv.QueryHistValue_1D(self.hist,i)
         return result
     elif self.nbins3 == None:
         result = np.zeros([self.nbins1,self.nbins2])
         for i in range(self.nbins1):
             for j in range(self.nbins2):
                 result[i,j] = cv.QueryHistValue_2D(self.hist,i,j)
         return result
     else:
         result = np.zeros([self.nbins1,self.nbins2,self.nbins3])
         for i in range(self.nbins1):
             for j in range(self.nbins2):
                 for k in range(self.nbins3):
                     result[i,j,k] = cv.QueryHistValue_3D(self.hist,i,j,k)
         return result
Ejemplo n.º 4
0
def get_2d_hist_img(x_bins=30, y_bins=32, scale=10, hist=None, img=None):
    if not hist:
        if img:
            hist = get_hs_2d_hist(img, x_bins, y_bins)
        else:
            raise Exception("Histogram or image should be given")

    (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)

    hist_img = cv.CreateImage((x_bins * scale, y_bins * scale), 8, 3)

    for h in range(x_bins):
        for s in range(y_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(255 - intensity, 255 - intensity,
                       255 - intensity), cv.CV_FILLED)
    return hist_img
Ejemplo n.º 5
0
                hueImg = cv.CreateMat(frameSmallHSV.height,
                                      frameSmallHSV.width, cv.CV_8UC1)
                satImg = cv.CreateMat(frameSmallHSV.height,
                                      frameSmallHSV.width, cv.CV_8UC1)
                cv.Split(frameSmallHSV, hueImg, satImg, None, None)

                binSum = face.height * face.width
                maxProbInt = 0
                for x in range(0, frameSmallHSV.height):
                    for y in range(0, frameSmallHSV.width):
                        hue = int(hueImg[x, y] / (180 / hBins))
                        if hue == hBins:
                            hue = hBins - 1
                        sat = int(satImg[x, y] / (256 / sBins))

                        binVal = cv.QueryHistValue_2D(myHist, hue, sat)
                        binProb = binVal / binSum
                        probIntensity = int(binProb * 255 /
                                            (maxValue / binSum))
                        skinProbImg[x, y] = probIntensity

                        if probIntensity > maxProbInt:
                            maxProbInt = probIntensity
                cv.ShowImage("skinProb",
                             skinProbImg)  #Original skin probability image

                #threshold
                cv.InRangeS(skinProbImg, 100, 255, skinProbImg)
                cv.ShowImage(
                    "skinProbThresholded1", skinProbImg
                )  #Original skin probability image after thresholding
Ejemplo n.º 6
0
    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, [(0, 180),
                                                              (0, 255)], 1)
    cv.CalcHist(planes, hist, 0, None)
    cv.NormalizeHist(hist, 1.0)

    #	Create an image to use to visualize our histogram.

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

    max_value = 0
    (minvalue, maxvalue, minidx, maxidx) = cv.GetMinMaxHistValue(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 / maxvalue)
            cv.Rectangle(hist_img, (h * scale, s * scale),
                         ((h + 1) * scale - 1, (s + 1) * scale - 1),
                         (intensity, intensity, intensity), cv.CV_FILLED)
            print intensity

    cv.ShowImage("src", src)
    cv.ShowImage("hsv", hist_img)
    cv.WaitKey(0)

else:
    print "usage : python example_7_1.py <image>"