Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import matplotlib.patches as mpatches
  3. import numpy as np
  4. from skimage.filters import threshold_otsu
  5. from skimage.segmentation import clear_border
  6. from skimage.measure import label, regionprops
  7. from skimage.morphology import opening, closing, square
  8. from skimage.color import label2rgb
  9. from skimage.color import rgb2gray
  10.  
  11. image = rgb2gray(plt.imread('strongi.jpg'))[100:-100, 100:-100]
  12.  
  13. # apply threshold
  14. thresh = threshold_otsu(image)
  15. #closing
  16. bw = closing(image > thresh, square(3))
  17. #opening
  18. bww = opening(bw, square(3))
  19.  
  20. # remove artifacts connected to image border
  21. cleared = clear_border(bww)
  22.  
  23. # label image regions
  24. cmap = plt.get_cmap("tab20")
  25. cmaps=np.array([cmap(i) for i in range(cmap.N)])
  26. label_image = label(cleared)
  27. image_label_overlay = label2rgb(label_image,colors=cmaps, image=image)
  28.  
  29. fig, ax = plt.subplots(figsize=(8, 6))
  30. ax.imshow(image_label_overlay)
  31.  
  32. for region in regionprops(label_image):
  33. # take regions with large enough areas
  34. if region.area >= 100:
  35. # draw rectangle around segmented coins
  36. minr, minc, maxr, maxc = region.bbox
  37. rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
  38. fill=False, edgecolor='g', linewidth=3)
  39. ax.add_patch(rect)
  40.  
  41. ax.set_axis_off()
  42. plt.savefig('plot_label.jpg',dpi=100)
  43. plt.show()
  44.  
  45. properties=regionprops(label_image)
  46. area=np.array([prop.area for prop in properties])
  47. label=np.array([prop.label for prop in properties])
  48. centroid=np.array([prop.centroid for prop in properties])
  49.  
  50. area[area>100]
  51. #array([1457, 2554, 3155, 1579, 1315, 2929, 3919, 2849, 3943, 3706])
  52.  
  53. label[area>100]
  54. #array([ 4, 6, 7, 8, 9, 16, 19, 20, 24, 26])
  55.  
  56. centroid[area>100]
  57. """
  58. array([[ 77.65339739, 299.37062457],
  59. [132.5082224 , 149.93970243],
  60. [137.55847861, 250.30174326],
  61. [144.00379987, 78.81697277],
  62. [157.08745247, 316.88365019],
  63. [221.82963469, 184.11266644],
  64. [242.8532789 , 86.42817045],
  65. [242.07827308, 292.95998596],
  66. [299.91732184, 188.02967284],
  67. [323.8440367 , 273.36022666]])
  68. """
  69.  
  70. img_c = plt.imread('strongi.jpg')[100:-100, 100:-100]
  71. sabos=[]
  72. for region in regionprops(label_image):
  73. if region.area >= 100:
  74. minr, minc, maxr, maxc = region.bbox
  75. sabo = img_c[minr:maxr,minc:maxc,:]
  76. sabos.append(sabo)
  77.  
  78. from mpl_toolkits.axes_grid1 import ImageGrid
  79. fig = plt.figure(figsize=(8, 6))
  80. grid = ImageGrid(fig, 111,
  81. nrows_ncols=(2, 5),
  82. axes_pad=0.2,
  83. share_all=True,
  84. )
  85. for i in range(len(sabos)):
  86. grid[i].imshow(sabos[i])
  87. grid[i].axis('off')
  88. grid[i].set_title('label='+str(label[area>100][i])+'')
  89. plt.savefig('plot_label_cactus.jpg',dpi=123)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement