Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from skimage import io, color, feature
  3. from skimage.filters import rank
  4. from skimage.feature import greycomatrix, greycoprops
  5. from skimage import data
  6. import skimage
  7. import cv2
  8. from PIL import Image
  9.  
  10. PATCH_SIZE = 21
  11.  
  12.  
  13. #metoda0
  14. # levels = 256
  15. # rgbImg = io.imread("lis.jpg")
  16. # image = cv2.cvtColor(rgbImg, cv2.COLOR_BGR2GRAY)
  17.  
  18.  
  19. #metoda1
  20. # levels = 256
  21. # gray = Image.open("lis.jpg").convert('L').convert("P", palette=Image.ADAPTIVE, colors=3).save('gray.png')
  22. # image = io.imread("gray.png")
  23.  
  24.  
  25. #metoda2
  26. original_path = 'lis.jpg'
  27. levels = 32 #2,4,8,16,32,64,128,256
  28.  
  29. palette = []
  30. stepsize = 768 // levels
  31. for i in range(levels):
  32. for j in range(stepsize):
  33. palette.append(i)
  34.  
  35. #print(palette)
  36. assert len(palette) == 768
  37.  
  38. original = Image.open(original_path)
  39. converted = Image.new('P', original.size)
  40. converted.putpalette(palette)
  41. converted.paste(original, (0, 0))
  42. converted.save('gray.png')
  43. image = io.imread("gray.png")
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. # select some patches from grassy areas of the image
  51. grass_locations = [(474, 291), (440, 433), (466, 18), (462, 236)]
  52. grass_patches = []
  53. for loc in grass_locations:
  54. grass_patches.append(image[loc[0]:loc[0] + PATCH_SIZE,
  55. loc[1]:loc[1] + PATCH_SIZE])
  56.  
  57. # select some patches from sky areas of the image
  58. sky_locations = [(54, 48), (21, 233), (90, 380), (195, 330)]
  59. sky_patches = []
  60. for loc in sky_locations:
  61. sky_patches.append(image[loc[0]:loc[0] + PATCH_SIZE,
  62. loc[1]:loc[1] + PATCH_SIZE])
  63.  
  64. # compute some GLCM properties each patch
  65. xs = []
  66. ys = []
  67. for patch in (grass_patches + sky_patches):
  68. glcm = greycomatrix(patch, [5], [0], levels+1, symmetric=True, normed=True)
  69. xs.append(greycoprops(glcm, 'dissimilarity')[0, 0])
  70. ys.append(greycoprops(glcm, 'correlation')[0, 0])
  71.  
  72.  
  73. # create the figure
  74. fig = plt.figure(figsize=(8, 8))
  75. # display original image with locations of patches
  76. ax = fig.add_subplot(3, 2, 1)
  77. ax.imshow(image, cmap=plt.cm.gray, interpolation='nearest',
  78. vmin=0, vmax=255)
  79. for (y, x) in grass_locations:
  80. ax.plot(x + PATCH_SIZE / 2, y + PATCH_SIZE / 2, 'gs')
  81. for (y, x) in sky_locations:
  82. ax.plot(x + PATCH_SIZE / 2, y + PATCH_SIZE / 2, 'bs')
  83. ax.set_xlabel('Original Image')
  84. ax.set_xticks([])
  85. ax.set_yticks([])
  86. ax.axis('image')
  87.  
  88. # for each patch, plot (dissimilarity, correlation)
  89. ax = fig.add_subplot(3, 2, 2)
  90. ax.plot(xs[:len(grass_patches)], ys[:len(grass_patches)], 'go')
  91. ax.plot(xs[len(grass_patches):], ys[len(grass_patches):], 'bo')
  92. ax.set_xlabel('GLCM Dissimilarity')
  93. ax.set_ylabel('GLCM Correlation')
  94. ax.legend()
  95.  
  96. # display the image patches
  97. for i, patch in enumerate(grass_patches):
  98. ax = fig.add_subplot(3, len(grass_patches), len(grass_patches) * 1 + i + 1)
  99. ax.imshow(patch, cmap=plt.cm.gray, interpolation='nearest',
  100. vmin=0, vmax=255)
  101.  
  102. for i, patch in enumerate(sky_patches):
  103. ax = fig.add_subplot(3, len(sky_patches), len(sky_patches) * 2 + i + 1)
  104. ax.imshow(patch, cmap=plt.cm.gray, interpolation='nearest',
  105. vmin=0, vmax=255)
  106.  
  107. # display the patches and plot
  108. # fig.suptitle('Grey level co-occurrence matrix features', fontsize=14)
  109. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement