-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_Combine_SLIC.py
182 lines (156 loc) · 6.06 KB
/
test_Combine_SLIC.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import cv2
import numpy as np
from sklearn.mixture import GaussianMixture as GMM
from sklearn.utils import shuffle
from pyheatmap.heatmap import HeatMap
import joblib
import argparse
def testGMM_RGB(testImgPath):
testImg = cv2.imread(testImgPath)
testImgRGB = cv2.imread(testImgPath)
testImgRGB = np.array(testImgRGB, dtype=np.float64)
testImgRGB[:,:,0] = testImgRGB[:,:,0] / 255
testImgRGB[:,:,1] = testImgRGB[:,:,1] / 255
testImgRGB[:,:,2] = testImgRGB[:,:,2] / 255
testImgRGB[:,:,2] *= 1.5
rows, cols, ch = testImg.shape
testImgHSV = np.reshape(testImgRGB, (rows * cols, ch))
return testImgHSV
def testGMM_HSV(testImgPath):
testImgRGB = cv2.imread(testImgPath)
testImgHSV = cv2.cvtColor(testImgRGB, cv2.COLOR_BGR2HSV)
testImgHSV = np.array(testImgHSV, dtype=np.float64)
testImgHSV[:,:,0] = (testImgHSV[:,:,0] + 90) % 180
testImgHSV[:,:,0] = testImgHSV[:,:,0] / 180
testImgHSV[:,:,1] = testImgHSV[:,:,1] / 255
testImgHSV[:,:,2] = testImgHSV[:,:,2] / 255
testImgHSV[:,:,0] *= 1.5
rows, cols, ch = testImgHSV.shape
testImgHSV = np.reshape(testImgHSV, (rows * cols, ch))
summation = np.sum(np.array(testImgRGB, dtype=np.float64), axis=2)
summation = np.reshape(summation,(rows * cols))
return testImgHSV, summation
def drawHeatMap(probability, rows, cols):
## probability: one dim long array (a col)
A = np.argwhere(np.ones((rows, cols)))
X = A[:,0]
Y = A[:,1]
data = []
N = len(X)
for i in range(N):
tmp = [int(Y[i]), int(X[i]), probability[i]]
data.append(tmp)
heat = HeatMap(data)
# Draw a heat map
heat.heatmap(save_as="./Output/HeatMap_GMM_Combine.png")
def scaling(probability):
grey = probability * 255
grey = np.array(grey, dtype=np.uint8)
return grey
def loadModel(modelPath, testImgPath, clusterNum):
## modelPath: .pkl path
## testImgPath: test image path
model = joblib.load(modelPath)
select = []
for i in range(clusterNum):
if model.weights_[i] > 0.122:
select.append(i)
rows, cols, ch = cv2.imread(testImgPath).shape
testImgHSV, summation = testGMM_HSV(testImgPath)
testImgRGB = testGMM_RGB(testImgPath)
testImg = np.hstack((testImgHSV, testImgRGB))
probabilityCombine = model.predict_proba(testImg)
probabilityCombine = probabilityCombine[:, select]
probability = np.max(probabilityCombine, axis=1)
isBlackOrWhite = np.bitwise_or(summation==0, summation==765)
probabilityCombine[isBlackOrWhite] = 0
drawHeatMap(probability, rows, cols)
# Scale probability from 0-1 to 0-255
grey = np.reshape(scaling(np.copy(probability)), (rows, cols))
probability2D = np.reshape(probability, (rows, cols))
print("Probability prediction completed!")
return grey, probability2D
def classify(imgPath, npy, ret=255):
img = cv2.imread(imgPath)
# initialize the parameters for SLIC
# region_size refers to the size of every piece of superpixel segmentation, ruler refers to the smooth factor
slic = cv2.ximgproc.createSuperpixelSLIC(img, region_size=5, ruler=20.0)
slic.iterate(10)
maskSlic = slic.getLabelContourMask()
cv2.imwrite('./Output/SLIC_Black_HSV.png', maskSlic)
# display image (could be commented out)
cv2.imshow('maskSlic', maskSlic)
cv2.waitKey(0)
cv2.destroyAllWindows()
maskInvSlic = cv2.bitwise_not(maskSlic)
cv2.imwrite('./Output/SLIC_White_HSV.png', maskInvSlic)
# display image (could be commented out)
cv2.imshow('maskInvSlic', maskInvSlic)
cv2.waitKey(0)
cv2.destroyAllWindows()
imgSlic = cv2.bitwise_and(img, img, mask=maskInvSlic)
cv2.imwrite('./Output/SLIC_HSV.png', imgSlic)
# display image (could be commented out)
cv2.imshow("imgSlic", imgSlic)
cv2.waitKey(0)
cv2.destroyAllWindows()
probValue = npy
length = np.size(slic.getLabels(), axis=0)
width = np.size(slic.getLabels(), axis=1)
labelArray = slic.getLabels()
labelNum = np.max(labelArray)
score = []
for i in range(labelNum):
superScore = []
for j in range(length):
for k in range(width):
if (labelArray[j][k] == i + 1):
superScore.append(probValue[j][k])
if len(superScore) != 0:
meanScore = np.array(superScore).mean()
score.append(meanScore)
else:
score.append(0)
binScore = []
for i in range(len(score)):
if (score[i] < ret / 255.0):
binScore.append(0)
else:
binScore.append(1)
finalImg = np.zeros_like(img)
binaryImg = np.zeros_like(img)
for i in range(length):
for j in range(width):
bScore = binScore[labelArray[i][j] - 1]
if (bScore == 0):
finalImg[i][j] = img[i][j]
binaryImg[i][j][0] = 255
binaryImg[i][j][1] = 255
binaryImg[i][j][2] = 255
else:
finalImg[i][j][0] = 0
finalImg[i][j][1] = 0
finalImg[i][j][2] = 255
binaryImg[i][j][0] = 0
binaryImg[i][j][1] = 0
binaryImg[i][j][2] = 255
return finalImg, binaryImg
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# pass in the path of the test image
parser.add_argument('--test_img_path', type=str, default=None)
args = parser.parse_args()
grey, prob = loadModel('./model/GMMmodel_Combine.pkl', args.test_img_path, 7)
ret, th = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
finalImg, binaryImg = classify(args.test_img_path, prob, ret)
print("Successfully generated test output!")
cv2.imwrite('./Output/Final_Img_Combine.png', finalImg)
# display image (could be commented out)
cv2.imshow('Final_Img_Combine', finalImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('./Output/Binary_Img_Combine.png', binaryImg)
# display image (could be commented out)
cv2.imshow('Binary_Img_Combine', binaryImg)
cv2.waitKey(0)
cv2.destroyAllWindows()