edge-detect / app.py
Omnibus's picture
Update app.py
3f09e59
"""
==================================================
Comparing edge-based and region-based segmentation
==================================================
In this example, we will see how to segment objects from a background. We use
the ``coins`` image from ``skimage.data``, which shows several coins outlined
against a darker background.
"""
import numpy as np
#import matplotlib.pyplot as plt
from skimage import data
from skimage.exposure import histogram
from skimage.feature import canny
from scipy import ndimage as ndi
from skimage import morphology
from skimage.filters import sobel
from skimage import segmentation
from skimage.color import label2rgb
coins = "test_im.jpg"
def process1(coins=coins):
hist, hist_centers = histogram(coins)
return hist
def process2(coins=coins):
######################################################################
# Edge-based segmentation
# =======================
#
# Next, we try to delineate the contours of the coins using edge-based
# segmentation. To do this, we first get the edges of features using the
# Canny edge-detector.
edges = canny(coins)
######################################################################
# These contours are then filled using mathematical morphology.
fill_coins = ndi.binary_fill_holes(edges)
######################################################################
# Small spurious objects are easily removed by setting a minimum size for
# valid objects.
coins_cleaned = morphology.remove_small_objects(fill_coins, 21)
return coins_cleaned
def process3(coins=coins):
######################################################################
# However, this method is not very robust, since contours that are not
# perfectly closed are not filled correctly, as is the case for one unfilled
# coin above.
#
# Region-based segmentation
# =========================
#
# We therefore try a region-based method using the watershed transform.
# First, we find an elevation map using the Sobel gradient of the image.
elevation_map = sobel(coins)
######################################################################
# Next we find markers of the background and the coins based on the extreme
# parts of the histogram of gray values.
markers = np.zeros_like(coins)
markers[coins < 30] = 1
markers[coins > 150] = 2
######################################################################
# Finally, we use the watershed transform to fill regions of the elevation
# map starting from the markers determined above:
segmentation_coins = segmentation.watershed(elevation_map, markers)
return segmentation_coins
def process4(segmentation_coins):
######################################################################
# This last method works even better, and the coins can be segmented and
# labeled individually.
segmentation_coins = ndi.binary_fill_holes(segmentation_coins - 1)
labeled_coins, _ = ndi.label(segmentation_coins)
image_label_overlay = label2rgb(labeled_coins, image=coins, bg_label=0)
with gr.Blocks() as app:
with gr.Row():
btn1=gr.Button("1")
btn2=gr.Button("2")
btn3=gr.Button("3")
btn4=gr.Button("4")
with gr.Row():
output1=gr.Image()
output2=gr.Image()
with gr.Row():
output3=gr.Image()
output4=gr.Image()
btn1.click(process1,None,output1)
btn2.click(process2,None,output2)
btn3.click(process3,None,output3)
btn4.click(process4,None,output4)
app.launch()