This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixel.py
82 lines (68 loc) · 2.52 KB
/
pixel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import itertools
import os
import sys
import cv2
import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage as ndi
from skimage.io import imread
from sklearn.cluster import KMeans
from skimage.feature import local_binary_pattern
from skimage.filters import gabor_kernel
from skimage.filters import gabor_filter
# feature extraction methods
def lbp(image, n=3, method="uniform"):
return local_binary_pattern(image, n, 8 * n, method=method)
def gabor(image, freqs=[0.4], theta=[0, 30, 60, 90, 120, 150]):
#feats = []
#for f, t in itertools.product(0.1, 30):
# feats.append(gabor_filter(image, f, theta=t))
#return feats
f = gabor_filter(image, 0.1, 120)
return f[0] + np.sqrt(f[1] ** 2)
def imshow(*images):
# create the figure
fig = plt.figure(figsize=(10, 10))
for i in range(0, len(images)):
# display original image with locations of patchea
ax = fig.add_subplot(len(images), 1, i + 1)
ax.imshow(images[i], cmap=plt.cm.gray, interpolation="nearest", vmin=0, vmax=255)
# display the patches and plot
plt.show()
if __name__ == "__main__":
# set up input
path = os.path.abspath(os.path.join(os.getcwd(), sys.argv[1]))
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
print("Image dimensions: %d x %d" % image.shape)
# get texture measures
feat = lbp(image)
print("Feature dimensions: %d x %d" % feat.shape)
feat_r = feat.reshape((feat.shape[0] * feat.shape[1], 1))
print("Flattening feature array: %d x %d" % feat_r.shape)
# set up batch k-means
n_classes = 10
mbkm = KMeans(n_classes)
# cluster the local binary patterns with default settings (k = 8)
clus = mbkm.fit(feat_r)
labs = clus.labels_
print("Labels shape: %d" % labs.shape)
# reshape label array
labs = labs.reshape(image.shape[0], image.shape[1])
print("Reshaping label array: %d x %d" % labs.shape)
# neighbourhood-ify
#nbhd = 5
#avgs = np.zeros((labs.shape[0] / nbhd, labs.shape[1] / nbhd))
#print(avgs.shape)
#i, j = (0, 0)
#for x in range(0, labs.shape[0] - nbhd, nbhd):
# for y in range(0, labs.shape[1] - nbhd):
# if j >= labs.shape[1] / nbhd:
# j = 0
# patch = labs[x:x+nbhd,y:y+nbhd][0]
# mode = np.bincount(patch).argmax()
# avgs[i,j] = mode
# j += 1
# i += 1
# make texture classes more visible and display them
labs *= int(255 / n_classes) # exaggerate values so that the difference is obvious
imshow(image, labs)