Esempio n. 1
0
            contours = cv.FindContours(marker_mask, storage, cv.CV_RETR_CCOMP,
                                       cv.CV_CHAIN_APPROX_SIMPLE)

            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)
# markers are differentiated by colour

# background marker, identified by colour (80, 80, 80)
cv.Circle(imgMarkers, (60, 60), 3, (80, 80, 80), -1)

# upper circle marker, identified by colour (160, 160, 160)
cv.Circle(imgMarkers, (180, 180), 3, (160, 160, 160), -1)

# lower circle marker, identified by colour (240, 240, 240)
cv.Circle(imgMarkers, (320, 320), 3, (240, 240, 240), -1)

# seed imgWatershed with the markers for the background, upper circle, and lower circle
cv.ConvertScale(imgMarkers, imgWatershed, 1)

# this is where the magic happens...
cv.Watershed(imgOriginal, imgWatershed)

cv.ShowImage("imgOriginal", imgOriginal)

cv.ShowImage("imgMarkers", imgMarkers)

# cv.IPL_DEPTH_32S images are divided by 256 on display so scale accordingly
cv.ConvertScale(imgWatershed, imgWatershed, 256)

cv.ShowImage("imgWatershed", imgWatershed)

Esc = 27

while True:
    keyPressed = cv.WaitKey(0) % 0x100
    if ((keyPressed == Esc) or (keyPressed == ord('q'))):