예제 #1
0
                    yield contour
                    contour = contour.h_next()

            cv.Zero(markers)
            comp_count = 0
            for c in contour_iterator(contours):
                cv.DrawContours(markers,
                                c,
                                cv.ScalarAll(comp_count + 1),
                                cv.ScalarAll(comp_count + 1),
                                -1,
                                -1,
                                8)
                comp_count += 1

            cv.Watershed(img0, markers)

            cv.Set(wshed, cv.ScalarAll(255))

            # paint the watershed image
            color_tab = [(cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50) for i in range(comp_count)]
            for j in range(markers.height):
                for i in range(markers.width):
                    idx = markers[j, i]
                    if idx != -1:
                        wshed[j, i] = color_tab[int(idx - 1)]

            cv.AddWeighted(wshed, 0.5, img_gray, 0.5, 0, wshed)
            cv.ShowImage("watershed transform", wshed)
    cv.DestroyAllWindows()
예제 #2
0
            def contour_iterator(contour):
                while contour:
                    yield contour
                    contour = contour.h_next()

            cv.Zero(markers)
            comp_count = 0
            for c in contour_iterator(contours):
                cv.DrawContours(markers, c, cv.ScalarAll(comp_count + 1),
                                cv.ScalarAll(comp_count + 1), -1, -1, 8)
                comp_count += 1

            cv.Watershed(img0, markers)

            cv.Set(wshed, cv.ScalarAll(255))

            # paint the watershed image
            color_tab = [
                (cv.RandInt(rng) % 180 + 50, cv.RandInt(rng) % 180 + 50,
                 cv.RandInt(rng) % 180 + 50) for i in range(comp_count)
            ]
            for j in range(markers.height):
                for i in range(markers.width):
                    idx = markers[j, i]
                    if idx != -1:
                        wshed[j, i] = color_tab[int(idx - 1)]

            cv.AddWeighted(wshed, 0.5, img_gray, 0.5, 0, wshed)
            cv.ShowImage("watershed transform", wshed)
예제 #3
0
파일: kmeans.py 프로젝트: r4mp/cinelerra
        cv.CV_RGB(255, 255, 0)
    ]
    img = cv.CreateImage((500, 500), 8, 3)
    rng = cv.RNG(-1)

    cv.NamedWindow("clusters", 1)

    while True:
        cluster_count = randint(2, MAX_CLUSTERS)
        sample_count = randint(1, 1000)
        points = cv.CreateMat(sample_count, 1, cv.CV_32FC2)
        clusters = cv.CreateMat(sample_count, 1, cv.CV_32SC1)

        # generate random sample from multigaussian distribution
        for k in range(cluster_count):
            center = (cv.RandInt(rng) % img.width,
                      cv.RandInt(rng) % img.height)
            first = k * sample_count / cluster_count
            last = sample_count
            if k != cluster_count:
                last = (k + 1) * sample_count / cluster_count

            point_chunk = cv.GetRows(points, first, last)

            cv.RandArr(rng, point_chunk, cv.CV_RAND_NORMAL,
                       cv.Scalar(center[0], center[1], 0, 0),
                       cv.Scalar(img.width * 0.1, img.height * 0.1, 0, 0))

        # shuffle samples
        cv.RandShuffle(points, rng)