Skip to content

Instantly share code, notes, and snippets.

@sabopy
Created September 24, 2020 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sabopy/aac4c47f0adcd5a6ec5b9d0621c632df to your computer and use it in GitHub Desktop.
Save sabopy/aac4c47f0adcd5a6ec5b9d0621c632df to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
from skimage.filters import threshold_sauvola
from skimage.color import rgb2gray
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
from skimage.morphology import opening,square
from skimage.measure import label, regionprops
#load image
image = rgb2gray(plt.imread('sisioumaru_seeds.jpg'))
#binarize
window_size = 25
thresh_sauvola = threshold_sauvola(image, window_size=window_size)
binary_sauvola = image < thresh_sauvola
fig = plt.figure(figsize=(8, 4),dpi=200)
grid = ImageGrid(fig, 111,
nrows_ncols=(1, 2),
axes_pad=0.3,
share_all=True,
)
grid[0].set_title('Original')
grid[0].imshow(image, cmap=plt.cm.gray)
grid[1].set_title('Sauvola Threshold')
grid[1].imshow(binary_sauvola, cmap=plt.cm.gray)
for gr in grid:
gr.axis("off")
plt.savefig('seed_binarize.png',dpi=100)
plt.show()
#opening
opening_img = opening(binary_sauvola, square(5))
fig = plt.figure(figsize=(8, 4),dpi=200)
grid = ImageGrid(fig, 111,
nrows_ncols=(1, 2),
axes_pad=0.3,
share_all=True,
)
grid[0].set_title('Original')
grid[0].imshow(image, cmap=plt.cm.gray)
grid[1].set_title('Opening')
grid[1].imshow(opening_img, cmap=plt.cm.gray)
for gr in grid:
gr.axis("off")
plt.savefig('seed_opening.png',dpi=100)
plt.show()
label_image = label(opening_img)
label_image
"""
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]])
"""
#0をnanに変換
label_imagee = label_image.astype(float)
label_imagee[label_imagee==0] = np.nan
fig = plt.figure(figsize=(8, 4),dpi=200)
grid = ImageGrid(fig, 111,
nrows_ncols=(1, 2),
axes_pad=0.3,
share_all=True,
)
grid[0].set_title('Original')
grid[0].imshow(image, cmap=plt.cm.gray)
grid[1].set_title('Label image')
grid[1].imshow(label_imagee,cmap="turbo")
for gr in grid:
gr.axis("off")
plt.savefig('seed_labels.png',dpi=400)
plt.show()
#areaを調べる
properties=regionprops(label_image)
area=np.array([prop.area for prop in properties])
area
"""
array([276, 286, 250, 271, 256, 279, 229, 308, 246, 262, 107, 292, 304,
487, 197, 297, 202, 551, 303, 258, 550, 261, 294, 254, 557, 603,
251, 820, 303, 144, 279, 783, 304, 285, 292, 310, 277, 332, 250,
318, 265, 315, 279, 277, 285, 290, 228, 48, 264, 287, 298, 279,
172, 178, 250, 254, 300, 258, 142, 288, 421, 257, 276, 165, 273,
249, 652, 573, 258, 148, 180, 260, 266, 146, 249, 240, 257, 267,
242, 154, 285, 295, 431, 260, 250, 189, 564, 317, 169, 425, 280,
468, 290, 282, 255, 277, 153, 268, 306, 256, 287, 189, 283, 201,
512, 132, 280, 273, 265, 273, 258, 70, 567, 256, 136, 381, 280,
776, 342, 146, 303, 55, 578, 275, 183, 302, 309, 89, 172, 258,
284, 271, 260, 304, 189, 575, 259, 230, 137, 290, 138, 25, 292,
402, 778, 453, 256, 256, 286, 525, 285, 239, 697, 548, 253, 316,
298, 260, 285, 236, 233, 268, 245, 293, 295, 227, 281, 542, 285,
273, 276, 308, 273, 163, 233, 223, 785, 268, 157, 310, 268, 306,
274, 101, 283, 114, 124, 664, 132, 165, 233, 284, 818, 259, 175,
331, 318, 313, 434, 267, 584, 297, 272, 265, 336, 284, 259, 247,
605, 343, 272, 295, 333, 267, 576, 118, 276, 375, 549, 246, 280,
307, 536, 62, 307, 161, 277, 269, 244, 201, 283, 156, 309, 310,
594, 262, 260, 260, 240, 297, 264, 262, 174, 251, 264, 257, 261,
277, 219, 276, 272, 290, 545, 268, 302, 260, 304, 266, 263])
"""
centroid=np.array([prop.centroid for prop in properties])
centroid =centroid[area>100]
len(centroid)
#253
fig,ax = plt.subplots(dpi=400)
ax.imshow(label_imagee,cmap="turbo")
[ax.text(np.array(centroid)[i,1]+5,np.array(centroid)[i,0]-5,i, fontsize=4) for i in range(len(centroid))]
ax.axis("off")
plt.savefig('seed_labelnum.png',dpi=400)
plt.show()
#version
import matplotlib
print(matplotlib.__version__)
3.3.2
print(np.__version__)
1.19.1
import skimage
print(skimage.__version__)
0.17.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment