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()
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)
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)