Abhisek92

Compare_ImageSegmentation.py

Mar 23rd, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import gdal
  4.  
  5. #from skimage.data import astronaut
  6. from skimage.color import rgb2gray
  7. from skimage.filters import sobel
  8. from skimage.segmentation import felzenszwalb, slic, quickshift, watershed
  9. from skimage.segmentation import mark_boundaries
  10. from skimage.util import img_as_float
  11.  
  12. image = (np.array(gdal.Open('study_area.tif').ReadAsArray())).astype(float)
  13. image = image.transpose(1, 2, 0)
  14. img = img_as_float(image[::2, ::2])
  15.  
  16. segments_fz = felzenszwalb(img, scale=100, sigma=0.5, min_size=50)
  17. segments_slic = slic(img, n_segments=250, compactness=10, sigma=1)
  18. segments_quick = quickshift(img, kernel_size=3, max_dist=6, ratio=0.5)
  19. gradient = sobel(rgb2gray(img))
  20. segments_watershed = watershed(gradient, markers=250, compactness=0.001)
  21.  
  22. print("Felzenszwalb number of segments: {}".format(len(np.unique(segments_fz))))
  23. print('SLIC number of segments: {}'.format(len(np.unique(segments_slic))))
  24. print('Quickshift number of segments: {}'.format(len(np.unique(segments_quick))))
  25.  
  26. fig, ax = plt.subplots(2, 2, figsize=(10, 10), sharex=True, sharey=True)
  27.  
  28. ax[0, 0].imshow(mark_boundaries(img, segments_fz))
  29. ax[0, 0].set_title("Felzenszwalbs's method")
  30. ax[0, 1].imshow(mark_boundaries(img, segments_slic))
  31. ax[0, 1].set_title('SLIC')
  32. ax[1, 0].imshow(mark_boundaries(img, segments_quick))
  33. ax[1, 0].set_title('Quickshift')
  34. ax[1, 1].imshow(mark_boundaries(img, segments_watershed))
  35. ax[1, 1].set_title('Compact watershed')
  36.  
  37. for a in ax.ravel():
  38.     a.set_axis_off()
  39.  
  40. plt.tight_layout()
  41. plt.show()
Add Comment
Please, Sign In to add comment