Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- from skimage import io, color, feature
- from skimage.filters import rank
- from skimage.feature import greycomatrix, greycoprops
- from skimage import data
- import skimage
- import cv2
- from PIL import Image
- PATCH_SIZE = 21
- #metoda0
- # levels = 256
- # rgbImg = io.imread("lis.jpg")
- # image = cv2.cvtColor(rgbImg, cv2.COLOR_BGR2GRAY)
- #metoda1
- # levels = 256
- # gray = Image.open("lis.jpg").convert('L').convert("P", palette=Image.ADAPTIVE, colors=3).save('gray.png')
- # image = io.imread("gray.png")
- #metoda2
- original_path = 'lis.jpg'
- levels = 32 #2,4,8,16,32,64,128,256
- palette = []
- stepsize = 768 // levels
- for i in range(levels):
- for j in range(stepsize):
- palette.append(i)
- #print(palette)
- assert len(palette) == 768
- original = Image.open(original_path)
- converted = Image.new('P', original.size)
- converted.putpalette(palette)
- converted.paste(original, (0, 0))
- converted.save('gray.png')
- image = io.imread("gray.png")
- # select some patches from grassy areas of the image
- grass_locations = [(474, 291), (440, 433), (466, 18), (462, 236)]
- grass_patches = []
- for loc in grass_locations:
- grass_patches.append(image[loc[0]:loc[0] + PATCH_SIZE,
- loc[1]:loc[1] + PATCH_SIZE])
- # select some patches from sky areas of the image
- sky_locations = [(54, 48), (21, 233), (90, 380), (195, 330)]
- sky_patches = []
- for loc in sky_locations:
- sky_patches.append(image[loc[0]:loc[0] + PATCH_SIZE,
- loc[1]:loc[1] + PATCH_SIZE])
- # compute some GLCM properties each patch
- xs = []
- ys = []
- for patch in (grass_patches + sky_patches):
- glcm = greycomatrix(patch, [5], [0], levels+1, symmetric=True, normed=True)
- xs.append(greycoprops(glcm, 'dissimilarity')[0, 0])
- ys.append(greycoprops(glcm, 'correlation')[0, 0])
- # create the figure
- fig = plt.figure(figsize=(8, 8))
- # display original image with locations of patches
- ax = fig.add_subplot(3, 2, 1)
- ax.imshow(image, cmap=plt.cm.gray, interpolation='nearest',
- vmin=0, vmax=255)
- for (y, x) in grass_locations:
- ax.plot(x + PATCH_SIZE / 2, y + PATCH_SIZE / 2, 'gs')
- for (y, x) in sky_locations:
- ax.plot(x + PATCH_SIZE / 2, y + PATCH_SIZE / 2, 'bs')
- ax.set_xlabel('Original Image')
- ax.set_xticks([])
- ax.set_yticks([])
- ax.axis('image')
- # for each patch, plot (dissimilarity, correlation)
- ax = fig.add_subplot(3, 2, 2)
- ax.plot(xs[:len(grass_patches)], ys[:len(grass_patches)], 'go')
- ax.plot(xs[len(grass_patches):], ys[len(grass_patches):], 'bo')
- ax.set_xlabel('GLCM Dissimilarity')
- ax.set_ylabel('GLCM Correlation')
- ax.legend()
- # display the image patches
- for i, patch in enumerate(grass_patches):
- ax = fig.add_subplot(3, len(grass_patches), len(grass_patches) * 1 + i + 1)
- ax.imshow(patch, cmap=plt.cm.gray, interpolation='nearest',
- vmin=0, vmax=255)
- for i, patch in enumerate(sky_patches):
- ax = fig.add_subplot(3, len(sky_patches), len(sky_patches) * 2 + i + 1)
- ax.imshow(patch, cmap=plt.cm.gray, interpolation='nearest',
- vmin=0, vmax=255)
- # display the patches and plot
- # fig.suptitle('Grey level co-occurrence matrix features', fontsize=14)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement