def test_threshold_cut(): 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) new_labels = graph.cut_threshold(labels, rag, 10, in_place=False) # Two labels assert new_labels.max() == 1 new_labels = graph.cut_threshold(labels, rag, 10) # Two labels assert new_labels.max() == 1
def paint(img): """ Turn a standard image into a painting. :param numpy.ndarray img: An RGB image in numpy format. """ # scale the image img = cv2.convertScaleAbs(img) # perform SLIC segmentation regions_slic = slic(img, compactness=SLIC_COMPACTNESS, n_segments=N_SLIC_REGIONS) # cluster all SLIC regions rag = rag_mean_color(img, regions_slic) regions_rag = cut_threshold(regions_slic, rag, RAG_THRESHOLD) # final painting result = label2rgb(regions_rag, img, kind="avg") return result
f = plt.figure(figsize=(width, height)) plt.imshow(img) img = data.coffee() show_img(img) labels = segmentation.slic(img, compactness=30, n_segments=400) labels = labels + 1 # So that no labelled region is 0 and ignored by regionprops regions = regionprops(labels) label_rgb = color.label2rgb(labels, img, kind='avg') show_img(label_rgb) label_rgb = segmentation.mark_boundaries(label_rgb, labels, (0, 0, 0)) show_img(label_rgb) rag = graph.rag_mean_color(img, labels) for region in regions: rag.node[region['label']]['centroid'] = region['centroid'] edges_drawn_all = display_edges(label_rgb, rag, np.inf ) show_img(edges_drawn_all) edges_drawn_29 = display_edges(label_rgb, rag, 29 ) show_img(edges_drawn_29) final_labels = graph.cut_threshold(labels, rag, 29) final_label_rgb = color.label2rgb(final_labels, img, kind='avg') show_img(final_label_rgb)
import numpy as np from matplotlib import colors def show_image(img): width = img.shape[1] / 50.0 height = img.shape[0] * width/img.shape[1] f = plt.figure(figsize=(width, height)) plt.imshow(img) image = io.imread('C:\Users\sxun\Desktop\j.jpg') show_image(image) labels = segmentation.slic(image, compactness=30, n_segments=400) border_image = segmentation.mark_boundaries(image, labels, (0, 0, 0)) show_image(border_image) rag = graph.rag_mean_color(image, labels) out = graph.draw_rag(labels, rag, border_image) show_image(out) out_labels=graph.cut_threshold(labels,rag,29) out = graph.draw_rag(labels, rag, border_image) show_image(out) final_label_rgb = color.label2rgb(out_labels, image, kind='avg') show_image(final_label_rgb)
""" ================ RAG Thresholding ================ This example constructs a Region Adjacency Graph (RAG) and merges regions which are similar in color. We construct a RAG and define edges as the difference in mean color. We then join regions with similar mean color. """ 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) labels2 = graph.cut_threshold(labels1, g, 29) out2 = color.label2rgb(labels2, img, kind='avg') plt.figure() io.imshow(out1) plt.figure() io.imshow(out2) io.show()
""" ================ RAG Thresholding ================ This example constructs a Region Adjacency Graph (RAG) and merges regions which are similar in color. We construct a RAG and define edges as the difference in mean color. We then join regions with similar mean color. """ 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) labels2 = graph.cut_threshold(labels1, g, 30) out2 = color.label2rgb(labels2, img, kind='avg') plt.figure() io.imshow(out1) plt.figure() io.imshow(out2) io.show()