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)
Beispiel #2
0
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)

    # Two labels
    assert new_labels.max() == 1
Beispiel #3
0
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
Beispiel #4
0
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
Beispiel #5
0
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
Beispiel #6
0
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
Beispiel #7
0
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
Beispiel #8
0
    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)
"""
=====================================
Drawing Region Adjacency Graphs (RAGs)
======================================

This example constructs a Region Adjacency Graph (RAG) and draws it with
the `rag_draw` method.
"""
from skimage import graph, data, segmentation
from matplotlib import pyplot as plt, colors


img = data.coffee()
labels = segmentation.slic(img, compactness=30, n_segments=400)
g = graph.rag_mean_color(img, labels)

out = graph.draw_rag(labels, g, img)
plt.figure()
plt.title("RAG with all edges shown in green.")
plt.imshow(out)

# The color palette used was taken from
# http://www.colorcombos.com/color-schemes/2/ColorCombo2.html
cmap = colors.ListedColormap(['#6599FF', '#ff9900'])
out = graph.draw_rag(labels, g, img, node_color="#ffde00", colormap=cmap,
                     thresh=30, desaturate=True)
plt.figure()
plt.title("RAG with edge weights less than 30, color "
          "mapped between blue and orange.")
plt.imshow(out)
Beispiel #11
0
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()
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,connectivity=100)
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)