def normcut_segmentations(img): #labels1 = segmentation.slic(img, compactness=3, n_segments=50) labels1 = segmentation.slic(img,compactness=3,n_segments=20) out1 = color.label2rgb(labels1, img)#, kind='avg') #return labels1 g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g) out2 = color.label2rgb(labels2, img,image_alpha=0.2)#, kind='avg') return (labels1,labels2)
def test_cut_normalized(): img = np.zeros((100, 100, 3), dtype='uint8') img[:50, :50] = 255, 255, 255 img[:50, 50:] = 254, 254, 254 img[50:, :50] = 2, 2, 2 img[50:, 50:] = 1, 1, 1 labels = np.zeros((100, 100), dtype='uint8') labels[:50, :50] = 0 labels[:50, 50:] = 1 labels[50:, :50] = 2 labels[50:, 50:] = 3 rag = graph.rag_mean_color(img, labels, mode='similarity') new_labels = graph.cut_normalized(labels, rag, in_place=False) new_labels, _, _ = segmentation.relabel_sequential(new_labels) # Two labels assert new_labels.max() == 1 new_labels = graph.cut_normalized(labels, rag) new_labels, _, _ = segmentation.relabel_sequential(new_labels) assert new_labels.max() == 1
Normalized Cut ============== This example constructs a Region Adjacency Graph (RAG) and recursively performs a Normalized Cut on it. References ---------- .. [1] Shi, J.; Malik, J., "Normalized cuts and image segmentation", Pattern Analysis and Machine Intelligence, IEEE Transactions on, vol. 22, no. 8, pp. 888-905, August 2000. """ from skimage import graph, data, io, segmentation, color from matplotlib import pyplot as plt img = data.coffee() labels1 = segmentation.slic(img, compactness=30, n_segments=400) out1 = color.label2rgb(labels1, img, kind='avg') g = graph.rag_mean_color(img, labels1, mode='similarity') labels2 = graph.cut_normalized(labels1, g) out2 = color.label2rgb(labels2, img, kind='avg') plt.figure() io.imshow(out1) plt.figure() io.imshow(out2) io.show()