Guest User

Untitled

a guest
Dec 11th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #!/Users/CiderBones/anaconda3/bin/python
  2.  
  3. # Testing out scikit-image with a scanned fluorescent maize ear
  4.  
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. from skimage import io
  8. from skimage.filters import sobel
  9. from skimage.color import rgb2gray
  10.  
  11. # Importing the image as a numpy array
  12. maize = io.imread('/Users/CiderBones/Desktop/Laboratory/computer_vision/scikit-image/X402x498-2m1.png')
  13.  
  14. # Testing to see if the image imported properly; it looks like it did.
  15. print(type(maize))
  16. print(maize.shape)
  17.  
  18. # Converting the image to grayscale
  19. maize_grayscale = rgb2gray(maize)
  20.  
  21. # Something's wrong with the histogram: after converting it to grayscale
  22. # everything's at 0.
  23. hist = np.histogram(maize_grayscale, bins=np.arange(0, 256))
  24.  
  25. # Puts the image into some kind of cache that can be show with plt.show()
  26. # io.imshow(maize_grayscale)
  27.  
  28. fig, axes = plt.subplots(1, 2, figsize=(8, 3))
  29. axes[0].imshow(maize_grayscale, cmap=plt.cm.gray, interpolation='nearest')
  30. axes[0].axis('off')
  31. axes[1].plot(hist[1][:-1], hist[0], lw=2)
  32. axes[1].set_title('histogram of gray values')
  33.  
  34. # Shows both the original image and the histogram of gray values. Not sure how to
  35. # remove an images once it's in the cache from imshow(). For some reason this is
  36. # showing both the maize ear and the maize ear plus the histogram. Actually I think
  37. # I see in line 29 where it's adding both to the same line.
  38. plt.show()
  39.  
  40.  
  41. # Doing some region-based segmentation
  42.  
  43. elevation_map = sobel(maize_grayscale)
  44. fig, ax = plt.subplots(figsize=(4, 3))
  45. ax.imshow(elevation_map, cmap=plt.cm.gray, interpolation='nearest')
  46. ax.set_title('elevation map')
  47. ax.axis('off')
  48.  
  49. plt.show()
  50.  
  51.  
  52. # Finding markers of the background and the kernels based on extreme parts of the
  53. # histogram of gray values. Not sure what that means. Not working, maybe tied to
  54. # the histogram not working?
  55. markers = np.zeros_like(maize_grayscale)
  56. markers[maize_grayscale < 30] = 1
  57. markers[maize_grayscale > 150] = 2
  58.  
  59. fig, ax = plt.subplots(figsize=(4, 3))
  60. ax.imshow(markers, cmap=plt.cm.nipy_spectral, interpolation='nearest')
  61. ax.set_title('markers')
  62. ax.axis('off')
  63.  
  64. plt.show()
Add Comment
Please, Sign In to add comment