-
-
Save anuragphadke/7eb0e4398ef005632c6d to your computer and use it in GitHub Desktop.
image segmentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import matplotlib.patches as mpatches | |
from skimage import data | |
from skimage.filters import threshold_otsu | |
from skimage.segmentation import clear_border | |
from skimage.measure import label | |
from skimage.morphology import closing, square | |
from skimage.measure import regionprops | |
from skimage.color import label2rgb | |
from skimage.io import imread | |
f = '/Users/aphadke/Desktop/c.png' | |
image = imread(f) | |
# apply threshold | |
thresh = threshold_otsu(image) | |
bw = closing(image > thresh, square(3)) | |
# remove artifacts connected to image border | |
cleared = bw.copy() | |
clear_border(cleared) | |
# label image regions | |
label_image = label(cleared) | |
borders = np.logical_xor(bw, cleared) | |
label_image[borders] = -1 | |
image_label_overlay = label2rgb(label_image, image=image) | |
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) | |
ax.imshow(image_label_overlay) | |
for region in regionprops(label_image): | |
# skip small images | |
if region.area < 100: | |
continue | |
# draw rectangle around segmented coins | |
minr, minc, maxr, maxc = region.bbox | |
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr, | |
fill=False, edgecolor='red', linewidth=2) | |
ax.add_patch(rect) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment