예제 #1
0
파일: color.py 프로젝트: wolfram2012/MOSSE
 def rescaleMax(self,value=255):
     ''' 
     Rescale the histogram such that the maximum equals the value.
     '''
     cv.NormalizeHist(self.hist,1)
     _,max_value,_,_ = cv.GetMinMaxHistValue(self.hist)
     if max_value == 0:
         max_value = 1.0
     cv.NormalizeHist(self.hist,255/max_value)
예제 #2
0
	def LoadHistogram(self, samples, channels, bins, ranges, factor):
		ch_numpy = [ samples[:, ch:(ch + 1)] for ch in channels ]
		ch_cvmat = map(cv.fromarray, ch_numpy)
		ch_image = map(cv.GetImage,  ch_cvmat)

		histogram = cv.CreateHist(bins, cv.CV_HIST_ARRAY, ranges, True)
		cv.CalcHist(ch_image, histogram, False)
		cv.NormalizeHist(histogram, factor)
		return histogram
예제 #3
0
def make_histogram(imagefile):
    col = cv.LoadImageM(imagefile)
    gray = cv.CreateImage(cv.GetSize(col), cv.IPL_DEPTH_8U, 1)
    cv.CvtColor(col, gray, cv.CV_RGB2GRAY)

    hist = cv.CreateHist([NUM_BINS], cv.CV_HIST_ARRAY, [[0, 255]], 1)
    cv.CalcHist([gray], hist)
    cv.NormalizeHist(hist, 1.0)
    return hist
예제 #4
0
파일: filters.py 프로젝트: tarora2/seawolf
def otsu_get_threshold(src):
    '''
    Find the optimal threshold value for a grey level image using Otsu's
    method.

    This is baised on Otsu's original paper published in IEEE Xplore: "A
    Threshold Selection Method from Gray-Level Histograms"

    '''

    if src.nChannels != 1:
        raise ValueError("Image must have one channel.")

    # Compute Histogram
    hist = cv.CreateHist([256], cv.CV_HIST_ARRAY, [[0, 255]], 1)
    cv.CalcHist([src], hist)

    # Convert to Probability Histogram
    cv.NormalizeHist(hist, 1)

    overall_moment = 0
    for t in xrange(256):
        overall_moment += t * cv.QueryHistValue_1D(hist, t)

    # Find the threshold t that gives the highest variance
    # Suffixes _b and _f mean background and foreground
    num_pixels = src.width * src.height
    weight_b = 0
    moment_b = 0
    highest_variance = 0
    best_threshold = 0
    for t in xrange(256):

        hist_value = cv.QueryHistValue_1D(hist, t)

        weight_b += hist_value
        weight_f = 1 - weight_b
        if weight_b == 0:
            continue
        if weight_f == 0:
            break

        moment_b += t * hist_value
        moment_f = overall_moment - moment_b

        mean_b = moment_b / weight_b
        mean_f = moment_f / weight_f

        variance_between = weight_b * weight_f * \
            (mean_b - mean_f) ** 2

        if variance_between >= highest_variance:
            highest_variance = variance_between
            best_threshold = t

    return best_threshold
예제 #5
0
    def update_histogram(self, face):
        (x, y, w, h) = face
        x2 = int(x + w * FACE_BORDER)
        y2 = int(y + h * FACE_BORDER)
        w2 = int(w - w * FACE_BORDER * 2)
        h2 = int(h - h * FACE_BORDER * 2)

        cv.SetImageROI(self.hue, (x2, y2, w2, h2))
        cv.SetImageROI(self.sat, (x2, y2, w2, h2))
        cv.CalcArrHist([self.hue, self.sat], self.hist, 1)
        cv.NormalizeHist(self.hist, 255)
        cv.ResetImageROI(self.hue)
        cv.ResetImageROI(self.sat)

        cv.Rectangle(self.visualize, (x, y), (x + w, y + h), (255, 0, 0))
        cv.Rectangle(self.visualize, (x2, y2), (x2 + w2, y2 + h2),
                     (128, 150, 0))
예제 #6
0
def compute_histogram(src, h_bins=30, s_bins=32):
    #create images
    hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    hplane = cv.CreateImage(cv.GetSize(src), 8, 1)
    splane = cv.CreateImage(cv.GetSize(src), 8, 1)
    vplane = cv.CreateImage(cv.GetSize(src), 8, 1)

    planes = [hplane, splane]
    cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
    cv.Split(hsv, hplane, splane, vplane, None)

    #compute histogram  (why not use v_plane?)
    hist = cv.CreateHist((h_bins, s_bins),
                         cv.CV_HIST_ARRAY,
                         ranges=((0, 180), (0, 255)),
                         uniform=True)
    cv.CalcHist(planes, hist)  #compute histogram
    cv.NormalizeHist(hist, 1.0)  #normalize hist

    return hist
예제 #7
0
def calcHistogram(src, quantization=16):
    # Convert to HSV
    #src = cv.fromarray(_src)
    luv = cv.CreateImage(cv.GetSize(src), 8, 3)
    cv.CvtColor(src, luv, cv.CV_RGB2Luv)

    # Extract the H and S planes
    size = cv.GetSize(src)
    L_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1)
    U_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1)
    V_plane = cv.CreateMat(size[1], size[0], cv.CV_8UC1)
    cv.Split(luv, L_plane, U_plane, V_plane, None)
    planes = [L_plane, U_plane, V_plane]
    #print np.asarray(L_plane),np.asarray(U_plane),np.asarray(V_plane)
    #Define number of bins
    L_bins = quantization
    U_bins = quantization
    V_bins = quantization

    #Define histogram size
    hist_size = [L_bins, U_bins, V_bins]

    #
    L_ranges = [0, 255]
    U_ranges = [0, 255]
    V_ranges = [0, 255]

    ranges = [L_ranges, U_ranges, V_ranges]

    #Create histogram
    hist = cv.CreateHist([L_bins, U_bins, V_bins], cv.CV_HIST_ARRAY, ranges, 1)

    #Calc histogram
    cv.CalcHist([cv.GetImage(i) for i in planes], hist)

    #Normalize histogram
    cv.NormalizeHist(hist, 1.0)

    #Return histogram
    return hist
예제 #8
0
파일: color.py 프로젝트: wolfram2012/MOSSE
 def rescaleSum(self,value=1.0):
     ''' 
     Rescale the histogram such that the maximum equals the value.
     '''
     cv.NormalizeHist(self.hist,value)
예제 #9
0
def main():
    BLACK_AND_WHITE = False
    THRESHOLD = 0.48
    BW_THRESHOLD = 0.4

    os.chdir(sys.argv[1])
    try:
        os.mkdir(OUTPUT_DIR_NAME)
    except:
        pass

    if len(sys.argv) > 2:
        if sys.argv[2] == "bw":
            BLACK_AND_WHITE = True
            THRESHOLD = BW_THRESHOLD
            print "##########"
            print " B/W MODE"
            print "##########"

    tree = et.parse("project.xml")
    movie = tree.getroot()
    file_path = movie.attrib["path"]
    cap = cv.CreateFileCapture(file_path)

    if DEBUG:
        cv.NamedWindow("win", cv.CV_WINDOW_AUTOSIZE)
        cv.MoveWindow("win", 200, 200)

    hist = None
    prev_hist = None
    prev_img = None

    pixel_count = None
    frame_counter = 0

    last_frame_black = False
    black_frame_start = -1

    t = time.time()

    while 1:
        img_orig = cv.QueryFrame(cap)

        if not img_orig:  # eof
            cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter - 1),
                         prev_img)
            """movie.set("frames", str(frame_counter))
			tree.write("project.xml")"""
            break

        img = cv.CreateImage(
            (int(img_orig.width / 4), int(img_orig.height / 4)),
            cv.IPL_DEPTH_8U, 3)
        cv.Resize(img_orig, img, cv.CV_INTER_AREA)

        if frame_counter == 0:  # erster frame
            cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (0), img)
            pixel_count = img.width * img.height
            prev_img = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
            cv.Zero(prev_img)

        if DEBUG and frame_counter % 2 == 1:
            cv.ShowImage("win", img)

        img_hsv = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
        cv.CvtColor(img, img_hsv, cv.CV_BGR2HSV)

        # #####################
        # METHOD #1: find the number of pixels that have (significantly) changed since the last frame
        diff = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3)
        cv.AbsDiff(img_hsv, prev_img, diff)
        cv.Threshold(diff, diff, 10, 255, cv.CV_THRESH_BINARY)
        d_color = 0
        for i in range(1, 4):
            cv.SetImageCOI(diff, i)
            d_color += float(cv.CountNonZero(diff)) / float(pixel_count)

        if not BLACK_AND_WHITE:
            d_color = float(d_color / 3.0)  # 0..1

        # #####################
        # METHOD #2: calculate the amount of change in the histograms
        h_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1)
        s_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1)
        v_plane = cv.CreateMat(img.height, img.width, cv.CV_8UC1)
        cv.Split(img_hsv, h_plane, s_plane, v_plane, None)
        planes = [h_plane, s_plane, v_plane]

        hist_size = [50, 50, 50]
        hist_range = [[0, 360], [0, 255], [0, 255]]
        if not hist:
            hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, hist_range, 1)
        cv.CalcHist([cv.GetImage(i) for i in planes], hist)
        cv.NormalizeHist(hist, 1.0)

        if not prev_hist:
            prev_hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, hist_range,
                                      1)
            # wieso gibt es kein cv.CopyHist()?!
            cv.CalcHist([cv.GetImage(i) for i in planes], prev_hist)
            cv.NormalizeHist(prev_hist, 1.0)
            continue

        d_hist = cv.CompareHist(prev_hist, hist, cv.CV_COMP_INTERSECT)

        # combine both methods to make a decision
        if ((0.4 * d_color + 0.6 * (1 - d_hist))) >= THRESHOLD:
            if DEBUG:
                if frame_counter % 2 == 0:
                    cv.ShowImage("win", img)
                winsound.PlaySound(soundfile,
                                   winsound.SND_FILENAME | winsound.SND_ASYNC)
            print "%.3f" % ((0.4 * d_color + 0.6 * (1 - d_hist))), "%.3f" % (
                d_color), "%.3f" % (1 - d_hist), frame_counter
            if DEBUG and DEBUG_INTERACTIVE:
                if win32api.MessageBox(0, "cut?", "",
                                       win32con.MB_YESNO) == 6:  #yes
                    cv.SaveImage(
                        OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter), img)
            else:
                cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (frame_counter),
                             img)

        cv.CalcHist([cv.GetImage(i) for i in planes], prev_hist)
        cv.NormalizeHist(prev_hist, 1.0)

        # #####################
        # METHOD #3: detect series of (almost) black frames as an indicator for "fade to black"
        average = cv.Avg(v_plane)[0]
        if average <= 0.6:
            if not last_frame_black:  # possible the start
                print "start", frame_counter
                black_frame_start = frame_counter
            last_frame_black = True
        else:
            if last_frame_black:  # end of a series of black frames
                cut_at = black_frame_start + int(
                    (frame_counter - black_frame_start) / 2)
                print "end", frame_counter, "cut at", cut_at
                img_black = cv.CreateImage(
                    (img_orig.width / 4, img_orig.height / 4), cv.IPL_DEPTH_8U,
                    3)
                cv.Set(img_black, cv.RGB(0, 255, 0))
                cv.SaveImage(OUTPUT_DIR_NAME + "\\%06d.png" % (cut_at),
                             img_black)
            last_frame_black = False

        cv.Copy(img_hsv, prev_img)
        frame_counter += 1

        if DEBUG:
            if cv.WaitKey(1) == 27:
                break

    if DEBUG:
        cv.DestroyWindow("win")

    print "%.2f min" % ((time.time() - t) / 60)
    #raw_input("- done -")
    return
예제 #10
0
    s_plane = cv.CreateImage(cv.GetSize(src), 8, 1)
    v_plane = cv.CreateImage(cv.GetSize(src), 8, 1)

    planes = [h_plane, s_plane]

    cv.CvtPixToPlane(hsv, h_plane, s_plane, v_plane, None)

    h_bins = 30
    s_bins = 32

    #	Build the histogram and compute its contents.

    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),
예제 #11
0
        #maskedPixels = (imageNP[ :, :, 0 ] >= 255) & (imageNP[ :, :, 1 ] <= 0) & (imageNP[ :, :, 2 ] >= 255)
        maskArray[maskedPixels == False] = 255

        r_plane = cv.CreateMat(image.height, image.width, cv.CV_8UC1)
        g_plane = cv.CreateMat(image.height, image.width, cv.CV_8UC1)
        b_plane = cv.CreateMat(image.height, image.width, cv.CV_8UC1)
        cv.Split(image, r_plane, g_plane, b_plane, None)

        planes = [r_plane, g_plane, b_plane]
        cv.CalcHist([cv.GetImage(i) for i in planes],
                    histogram,
                    accumulate=1,
                    mask=maskArray)

    # Normalise the histogram
    cv.NormalizeHist(histogram, 1.0)

    if objectName == "Sellotape":
        savedHistogram = histogram

    # Store the histogram
    learntHistograms[objectName] = histogram

# Now compare the test images to the learnt histograms
for objectName in testImages.keys():

    image = cv.LoadImageM(SEGMENTATION_DIR + "/" + testImages[objectName])

    # Create a histogram for the test image
    maskArray = np.zeros((image.height, image.width), dtype=np.uint8)
예제 #12
0
    def pipeline(self):
        presentation = []
        self.orig = self.source.grab_frame()
        cv.Resize(self.orig, self.small)
        cv.CvtColor(self.small, self.hsv, cv.CV_BGR2HSV)
        cv.Split(self.hsv, self.hue, self.sat, self.bw, None)
        cv.Copy(self.small, self.visualize)
        presentation.append((self.visualize, 'input'))
        face = self.find_face(self.small)
        if face:
            sub_face = self.face_region(face, FACE_BORDER)
            self.update_histogram(sub_face)
            self.draw_face(self.visualize, face, sub_face)

            hue_bg = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1)
            sat_bg = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1)
            (x, y, w, h) = face
            cv.Copy(self.hue, hue_bg)
            cv.Copy(self.sat, sat_bg)
            cv.Rectangle(hue_bg, (x, y), (x + w, y + h), 0, cv.CV_FILLED)
            cv.Rectangle(sat_bg, (x, y), (x + w, y + h), 0, cv.CV_FILLED)
            cv.CalcArrHist([self.hue, self.sat], self.bg_hist, 1)
            cv.NormalizeHist(self.bg_hist, 255)

        bp_bg = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1)
        cv.CalcArrBackProject([self.hue, self.sat], bp_bg, self.bg_hist)
        self.normalize(bp_bg)
        presentation.append((bp_bg, 'background bp'))

        bp = self.backproject()
        presentation.append((bp, 'forground bp'))
        self.normalize(bp)

        compare = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1)
        compare_th = cv.CreateImage(self.smallsize, cv.IPL_DEPTH_8U, 1)
        cv.Cmp(bp, bp_bg, compare, cv.CV_CMP_GT)
        #cv.AddS(bp_bg, 1, bp_bg)
        #cv.Div(bp, bp_bg, compare)
        self.normalize(compare)
        cv.Threshold(compare, compare_th, self.threshold_value, 255,
                     cv.CV_THRESH_BINARY)
        presentation.append((compare_th, 'compare'))

        th = self.threshold(bp)
        presentation.append((th, 'normal th'))
        morphed = self.morphology(th)

        # make dark copy of original
        cv.Copy(self.small, self.result)
        cv.ConvertScale(self.result, self.result, 0.2)
        cv.Copy(self.small, self.result, morphed)
        contours = self.find_contours(morphed)
        self.draw_contours(self.result, contours)
        limbs = self.find_limbs(contours)
        limbs = self.sort_limbs(limbs)
        self.draw_limbs(self.result, limbs)
        presentation.append((self.result, 'result'))
        self.make_sound(limbs)

        # combine and show the results
        combined = self.combine_images(presentation)
        if face:
            sub_face = self.face_region(face, FACE_BORDER)
            self.draw_face(self.result, face, sub_face)

        cv.ShowImage('Skin Detection', combined)

        if STORE:
            cv.WriteFrame(self.writer, self.combined)
예제 #13
0
파일: run.py 프로젝트: catree/sonic-gesture
faceRect = faces[0][0]
face_center = (faceRect[0] + faceRect[2]/2, faceRect[1] + faceRect[3]/2)
faceSubRect = sub_region(faceRect)
(x, y, w, h) = faceRect
cv.Rectangle(frameShow, (x,y), (x+w,y+h), cv.Scalar(0, 0, 255), 3)
(x, y, w, h) = faceSubRect
cv.Rectangle(frameShow, (x,y), (x+w,y+h), cv.Scalar(0, 255, 255), 3)

#calculate histogram
cv.SetImageROI(frameH, faceSubRect)
cv.SetImageROI(frameS, faceSubRect)
cv.CalcArrHist([frameH, frameS], hist, 0)

# turn this on or off:
#cv.NormalizeHist(hist, 1)
cv.NormalizeHist(hist, 5000)

cv.ResetImageROI(frameH)
cv.ResetImageROI(frameS)

#convert histogram to image
histImg = cv.GetMat(hist.bins, True)

#make backprojection
cv.CalcArrBackProject([frameH, frameS], frameBP, hist)

cv.Normalize(frameBP, frameBP, 0, 255, 32)
cv.Smooth( frameBP, frameBlur, param1=31);
cv.Threshold(frameBlur, frameTh, 30, 255, cv.CV_THRESH_BINARY);
cv.Threshold(frameBP, frameThNoBlur, 30, 255, cv.CV_THRESH_BINARY);