Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. %matplotlib inline
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from PIL import Image
  5. from scipy.ndimage import gaussian_filter
  6. from skimage import img_as_float
  7. from skimage.morphology import reconstruction
  8.  
  9. # Convert to float: Important for subtraction later which won't work with uint8
  10. im = Image.open('kabuto.jpg').convert('L')
  11. image = np.array(im)
  12. image = img_as_float(image)
  13. image = gaussian_filter(image, 1)
  14.  
  15. seed = np.copy(image)
  16. seed[1:-1, 1:-1] = image.min()
  17. mask = image
  18.  
  19. dilated = reconstruction(seed, mask, method='dilation')
  20.  
  21. fig, (ax0, ax1, ax2) = plt.subplots(nrows=1,
  22. ncols=3,
  23. figsize=(8, 2.5),
  24. sharex=True,
  25. sharey=True)
  26.  
  27. ax0.imshow(image, cmap='gray')
  28. ax0.set_title('original image')
  29. ax0.axis('off')
  30.  
  31. ax1.imshow(dilated, vmin=image.min(), vmax=image.max(), cmap='gray')
  32. ax1.set_title('dilated')
  33. ax1.axis('off')
  34.  
  35. ax2.imshow(image - dilated, cmap='gray')
  36. ax2.set_title('image - dilated')
  37. ax2.axis('off')
  38. fig.tight_layout()
  39. #plt.savefig('kabuto_dilated.jpg',dpi=150)
  40.  
  41. fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(8, 2.5))
  42. yslice = 147
  43.  
  44. ax0.plot(mask[yslice], 'm', label='mask')
  45. ax0.plot(seed[yslice], 'g', label='seed')
  46. ax0.plot(dilated[yslice], 'r', label='dilated')
  47. #
  48. ax0.set_title('image slice')
  49. ax0.set_xticks([])
  50. ax0.legend()
  51.  
  52. ax1.imshow(dilated, vmin=image.min(), vmax=image.max(), cmap='gray')
  53. ax1.axhline(yslice, color='r', alpha=0.4)
  54. ax1.set_title('dilated')
  55. ax1.axis('off')
  56.  
  57. ax2.imshow(image - dilated, cmap='gray')
  58. ax2.axhline(yslice, color='r', alpha=0.4)
  59. ax2.set_title('image - dilated')
  60. ax2.axis('off')
  61.  
  62. fig.tight_layout()
  63. #plt.savefig('kabuto_dilated_withline.jpg',dpi=150)
  64. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement