Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %matplotlib inline
- import numpy as np
- import matplotlib.pyplot as plt
- from PIL import Image
- from scipy.ndimage import gaussian_filter
- from skimage import img_as_float
- from skimage.morphology import reconstruction
- # Convert to float: Important for subtraction later which won't work with uint8
- im = Image.open('kabuto.jpg').convert('L')
- image = np.array(im)
- image = img_as_float(image)
- image = gaussian_filter(image, 1)
- seed = np.copy(image)
- seed[1:-1, 1:-1] = image.min()
- mask = image
- dilated = reconstruction(seed, mask, method='dilation')
- fig, (ax0, ax1, ax2) = plt.subplots(nrows=1,
- ncols=3,
- figsize=(8, 2.5),
- sharex=True,
- sharey=True)
- ax0.imshow(image, cmap='gray')
- ax0.set_title('original image')
- ax0.axis('off')
- ax1.imshow(dilated, vmin=image.min(), vmax=image.max(), cmap='gray')
- ax1.set_title('dilated')
- ax1.axis('off')
- ax2.imshow(image - dilated, cmap='gray')
- ax2.set_title('image - dilated')
- ax2.axis('off')
- fig.tight_layout()
- #plt.savefig('kabuto_dilated.jpg',dpi=150)
- fig, (ax0, ax1, ax2) = plt.subplots(nrows=1, ncols=3, figsize=(8, 2.5))
- yslice = 147
- ax0.plot(mask[yslice], 'm', label='mask')
- ax0.plot(seed[yslice], 'g', label='seed')
- ax0.plot(dilated[yslice], 'r', label='dilated')
- #
- ax0.set_title('image slice')
- ax0.set_xticks([])
- ax0.legend()
- ax1.imshow(dilated, vmin=image.min(), vmax=image.max(), cmap='gray')
- ax1.axhline(yslice, color='r', alpha=0.4)
- ax1.set_title('dilated')
- ax1.axis('off')
- ax2.imshow(image - dilated, cmap='gray')
- ax2.axhline(yslice, color='r', alpha=0.4)
- ax2.set_title('image - dilated')
- ax2.axis('off')
- fig.tight_layout()
- #plt.savefig('kabuto_dilated_withline.jpg',dpi=150)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement