Ejemplo n.º 1
0
def check_simmilarity(path1, path2):
    src1 = cv.LoadImage(path1, cv.CV_LOAD_IMAGE_COLOR)
    src2 = cv.LoadImage(path2, cv.CV_LOAD_IMAGE_COLOR)
    hist1 = compute_histogram(src1)
    hist2 = compute_histogram(src2)
    sc = cv.CompareHist(hist1, hist2, cv.CV_COMP_BHATTACHARYYA)
    return sc
Ejemplo n.º 2
0
def main():
    interval = 1
    capture = cv.CaptureFromCAM(0)
    try:
        last_frame = None
        last_hist = None
        last_filename = None
        last_saved = False
        while True:
            frame = get_gray_frame(capture)
            filename = get_file_name()
            hist = get_frame_hist(frame)
            change = False
            if last_hist is not None:
                method = cv.CV_COMP_CORREL
                dist = cv.CompareHist(hist, last_hist, method)
                change = dist <= 0.995
                # print filename, last_filename, change, dist
                if change:
                    if not last_saved:
                        cv.SaveImage(last_filename, last_frame)
                    cv.SaveImage(filename, frame)
                    last_saved = True
                else:
                    last_saved = False
            last_hist = hist
            last_frame = frame
            last_filename = filename
            if change:
                interval = 0.4
            else:
                interval = 0.8
            time.sleep(interval)
    except KeyboardInterrupt:
        pass
Ejemplo n.º 3
0
 def distance(self, current, other):
     return cv.CompareHist(current, other, cv.CV_COMP_CHISQR)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
            #cv.Or(avg_noise, scratch, avg_noise)
            #


            #cv.Sub(back_proj_img2, avg_noise, back_proj_img2)
            #cv.Sub(scratch,, back_proj_img2)
            cv.ShowImage("final", scratch)
            #cv.Sub(scratch, avg_noise, scratch2)



            #cv.And(scratch, back_proj_img2, scratch2)
            #cv.SubRS(scratch2, 255, scratch)                
            #cv.ShowImage("final", back_proj_img)

            print cv.CompareHist(hist1, hist2, cv.CV_COMP_BHATTACHARYYA)

            #making a mask
            #mask = cv.CreateImage(cv.GetSize(back_proj_img2), 8, 1)

            #cv.SubRS(back_proj_img2, 255, back_proj_img2)
            #cv.SubRS(back_proj_img, 255, back_proj_img, mask=back_proj_img2)
            #cv.SubRS(back_proj_img, 255, back_proj_img)

            #cv.MorphologyEx(back_proj_img,back_proj_img, None, None, cv.CV_MOP_OPEN, 8)
            #cv.MorphologyEx(back_proj_img,back_proj_img, None, None, cv.CV_MOP_CLOSE, 8)
            #cv.ShowImage("back_projection", back_proj_img2)
            #cv.WaitKey(0)

            cv.Scale(back_proj_img, back_proj_img, 1/255.0)
            print "here's the sum :", cv.Sum(scratch2)
Ejemplo n.º 6
0
    planes = [r_plane, g_plane, b_plane]

    histogram = cv.CreateHist([256 / 8, 256 / 8, 256 / 8], cv.CV_HIST_ARRAY,
                              [(0, 255), (0, 255), (0, 255)], 1)
    cv.ClearHist(histogram)
    cv.CalcHist([cv.GetImage(i) for i in planes],
                histogram,
                accumulate=0,
                mask=maskArray)
    cv.NormalizeHist(histogram, 1.0)

    # Test it against each of the learnt histograms in turn
    print objectName
    for otherObjectName in learntHistograms.keys():
        print "    " + otherObjectName + ": " \
            + str( cv.CompareHist( histogram, learntHistograms[ otherObjectName ], cv.CV_COMP_INTERSECT ) )
        #learntHistograms[ otherObjectName ], cv.CV_COMP_INTERSECT ) )

    #if objectName == "Sellotape":
    #    savedHistogram = histogram

    #z = np.array( r_plane )
    #x = np.array( g_plane )
    #y = np.array( b_plane )
    #print z[ (z > 0) & (x > 0) & (y > 0) & (maskArray == 255) ].size

    #for i in range( 8 ):
    #for j in range( 8 ):
    #a = ""
    #for k in range( 8 ):
    #a += str( cv.QueryHistValue_3D( histogram, i, j, k )  )