-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolor_cluster_cv.py
executable file
·559 lines (373 loc) · 16.8 KB
/
color_cluster_cv.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
'''
Name: trait_extract_parallel.py
Version: 1.0
Summary: Extract plant traits (leaf area, width, height, ) by paralell processing
Author: suxing liu
Author-email: [email protected]
Created: 2019-09-29
USAGE:
python3 color_cluster_cv.py -p ~/plant-image-analysis/test/ -ft jpg
'''
# import the necessary packages
import numpy as np
import cv2
import os
import argparse
import glob
import utils
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.cluster import MiniBatchKMeans
from collections import Counter
from skimage.color import rgb2lab, deltaE_cie76
import seaborn as sns
import imutils
from scipy.interpolate import interp1d
from mpl_toolkits.mplot3d import Axes3D
'''
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
'''
MBFACTOR = float(1<<20)
# generate foloder to store the output results
def mkdir(path):
# import module
import os
# remove space at the beginning
path=path.strip()
# remove slash at the end
path=path.rstrip("\\")
# path exist? # True # False
isExists=os.path.exists(path)
# process
if not isExists:
# construct the path and folder
#print path + ' folder constructed!'
# make dir
os.makedirs(path)
return True
else:
# if exists, return
#print path+' path exists!'
return False
def color_cluster_seg(image, args_colorspace, args_channels, args_num_clusters):
# Change image color space, if necessary.
colorSpace = args_colorspace.lower()
if colorSpace == 'hsv':
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
elif colorSpace == 'ycrcb' or colorSpace == 'ycc':
image = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
elif colorSpace == 'lab':
image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
else:
colorSpace = 'bgr' # set for file naming purposes
# Keep only the selected channels for K-means clustering.
if args_channels != 'all':
channels = cv2.split(image)
channelIndices = []
for char in args_channels:
channelIndices.append(int(char))
image = image[:,:,channelIndices]
if len(image.shape) == 2:
image.reshape(image.shape[0], image.shape[1], 1)
(width, height, n_channel) = image.shape
#print("image shape: \n")
#print(width, height, n_channel)
# Flatten the 2D image array into an MxN feature vector, where M is the number of pixels and N is the dimension (number of channels).
reshaped = image.reshape(image.shape[0] * image.shape[1], image.shape[2])
# Perform K-means clustering.
if args_num_clusters < 2:
print('Warning: num-clusters < 2 invalid. Using num-clusters = 2')
#define number of cluster
numClusters = max(2, args_num_clusters)
# clustering method
kmeans = KMeans(n_clusters = numClusters, n_init = 40, max_iter = 500).fit(reshaped)
# get lables
pred_label = kmeans.labels_
# Reshape result back into a 2D array, where each element represents the corresponding pixel's cluster index (0 to K - 1).
clustering = np.reshape(np.array(pred_label, dtype=np.uint8), (image.shape[0], image.shape[1]))
# Sort the cluster labels in order of the frequency with which they occur.
sortedLabels = sorted([n for n in range(numClusters)],key = lambda x: -np.sum(clustering == x))
# Initialize K-means grayscale image; set pixel colors based on clustering.
kmeansImage = np.zeros(image.shape[:2], dtype=np.uint8)
for i, label in enumerate(sortedLabels):
kmeansImage[clustering == label] = int(255 / (numClusters - 1)) * i
ret, thresh = cv2.threshold(kmeansImage,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
#return thresh
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(thresh, connectivity=8)
sizes = stats[1:, -1]
nb_components = nb_components - 1
min_size = 150
img_thresh = np.zeros([width, height], dtype=np.uint8)
#for every component in the image, you keep it only if it's above min_size
for i in range(0, nb_components):
if sizes[i] >= min_size:
img_thresh[output == i + 1] = 255
#from skimage import img_as_ubyte
#img_thresh = img_as_ubyte(img_thresh)
#print("img_thresh.dtype")
#print(img_thresh.dtype)
return img_thresh
def RGB2HEX(color):
return "#{:02x}{:02x}{:02x}".format(int(color[0]), int(color[1]), int(color[2]))
def color_quantization(image, mask, save_path, num_clusters):
#grab image width and height
(h, w) = image.shape[:2]
#change the color storage order
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
#apply the mask to get the segmentation of plant
masked_image_BGR = cv2.bitwise_and(image, image, mask = mask)
#define result path for labeled images
result_img_path = save_path + 'masked.png'
cv2.imwrite(result_img_path, masked_image_BGR)
# convert the image from the RGB color space to the L*a*b*
# color space -- since we will be clustering using k-means
# which is based on the euclidean distance, we'll use the
# L*a*b* color space where the euclidean distance implies
# perceptual meaning
#masked_image = cv2.cvtColor(masked_image_BGR, cv2.COLOR_BGR2LAB)
masked_image = cv2.cvtColor(masked_image_BGR, cv2.COLOR_BGR2RGB)
#reshape the image to be a list of pixels
pixels = masked_image.reshape((masked_image.shape[0] * masked_image.shape[1], 3))
############################################################
#Clustering process
###############################################################
# cluster the pixel intensities
clt = MiniBatchKMeans(n_clusters = num_clusters)
#clt = KMeans(n_clusters = args["clusters"])
clt.fit(pixels)
#assign labels to each cluster
labels = clt.fit_predict(pixels)
#obtain the quantized clusters using each label
quant = clt.cluster_centers_.astype("uint8")[labels]
#reshape the feature vectors to images
quant = quant.reshape((h, w, 3))
image_rec = pixels.reshape((h, w, 3))
#convert from L*a*b* to RGB
quant = cv2.cvtColor(quant, cv2.COLOR_RGB2BGR)
#quant = cv2.cvtColor(quant, cv2.COLOR_LAB2BGR)
#define result path for labeled images
result_img_path = save_path + 'cluster_out.png'
# save color_quantization results
cv2.imwrite(result_img_path, quant)
counts = Counter(labels)
# sort to ensure correct color percentage
counts = dict(sorted(counts.items()))
center_colors = clt.cluster_centers_
# We get ordered colors by iterating through the keys
ordered_colors = [center_colors[i] for i in counts.keys()]
hex_colors = [RGB2HEX(ordered_colors[i]) for i in counts.keys()]
rgb_colors = [ordered_colors[i] for i in counts.keys()]
#print(hex_colors)
index_bkg = [index for index in range(len(hex_colors)) if hex_colors[index] == '#000000']
#print(index_bkg[0])
#print(counts)
#remove background color
del hex_colors[index_bkg[0]]
del rgb_colors[index_bkg[0]]
# Using dictionary comprehension to find list
# keys having value .
delete = [key for key in counts if key == index_bkg[0]]
# delete the key
for key in delete: del counts[key]
fig = plt.figure(figsize = (6, 6))
plt.pie(counts.values(), labels = hex_colors, colors = hex_colors)
#define result path for labeled images
result_img_path = save_path + 'pie_color.png'
plt.savefig(result_img_path)
#build a histogram of clusters and then create a figure representing the number of pixels labeled to each color
hist = utils.centroid_histogram(clt)
#remove the background color cluster
clt.cluster_centers_ = np.delete(clt.cluster_centers_, index_bkg[0], axis=0)
#build a histogram of clusters using center lables
numLabels = utils.plot_centroid_histogram(save_path,clt)
#create a figure representing the distribution of each color
bar = utils.plot_colors(hist, clt.cluster_centers_)
#save a figure of color bar
utils.plot_color_bar(save_path, bar)
return rgb_colors
def color_region(image, mask, save_path, num_clusters):
# read the image
#grab image width and height
(h, w) = image.shape[:2]
#apply the mask to get the segmentation of plant
masked_image_ori = cv2.bitwise_and(image, image, mask = mask)
#define result path for labeled images
result_img_path = save_path + 'masked.png'
cv2.imwrite(result_img_path, masked_image_ori)
# convert to RGB
image_RGB = cv2.cvtColor(masked_image_ori, cv2.COLOR_BGR2RGB)
# reshape the image to a 2D array of pixels and 3 color values (RGB)
pixel_values = image_RGB.reshape((-1, 3))
# convert to float
pixel_values = np.float32(pixel_values)
# define stopping criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
# number of clusters (K)
#num_clusters = 5
compactness, labels, (centers) = cv2.kmeans(pixel_values, num_clusters, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
# convert back to 8 bit values
centers = np.uint8(centers)
# flatten the labels array
labels_flat = labels.flatten()
# convert all pixels to the color of the centroids
segmented_image = centers[labels_flat]
# reshape back to the original image dimension
segmented_image = segmented_image.reshape(image_RGB.shape)
segmented_image_BRG = cv2.cvtColor(segmented_image, cv2.COLOR_RGB2BGR)
#define result path for labeled images
result_img_path = save_path + 'clustered.png'
cv2.imwrite(result_img_path, segmented_image_BRG)
'''
fig = plt.figure()
ax = Axes3D(fig)
for label, pix in zip(labels, segmented_image):
ax.scatter(pix[0], pix[1], pix[2], color = (centers))
result_file = (save_path + base_name + 'color_cluster_distributation.png')
plt.savefig(result_file)
'''
#Show only one chosen cluster
#masked_image = np.copy(image)
masked_image = np.zeros_like(image_RGB)
# convert to the shape of a vector of pixel values
masked_image = masked_image.reshape((-1, 3))
# color (i.e cluster) to render
#cluster = 2
clrs = sns.color_palette('husl', n_colors = num_clusters) # a list of RGB tuples
color_conversion = interp1d([0,1],[0,255])
for cluster in range(num_clusters):
print("Processing Cluster{0} ...\n".format(cluster))
#print(clrs[cluster])
#print(color_conversion(clrs[cluster]))
masked_image[labels_flat == cluster] = centers[cluster]
#print(centers[cluster])
#convert back to original shape
masked_image_rp = masked_image.reshape(image_RGB.shape)
#masked_image_BRG = cv2.cvtColor(masked_image, cv2.COLOR_RGB2BGR)
#cv2.imwrite('maksed.png', masked_image_BRG)
gray = cv2.cvtColor(masked_image_rp, cv2.COLOR_BGR2GRAY)
# threshold the image, then perform a series of erosions +
# dilations to remove any small regions of noise
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
#c = max(cnts, key=cv2.contourArea)
'''
# compute the center of the contour area and draw a circle representing the center
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# draw the countour number on the image
result = cv2.putText(masked_image_rp, "#{}".format(cluster + 1), (cX - 20, cY), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2)
'''
if not cnts:
print("findContours is empty")
else:
# loop over the (unsorted) contours and draw them
for (i, c) in enumerate(cnts):
#result = cv2.drawContours(masked_image_rp, c, -1, (0, 0, 255), 2)
result = cv2.drawContours(masked_image_rp, c, -1, color_conversion(clrs[cluster]), 2)
#result = result(np.where(result == 0)== 255)
result[result == 0] = 255
result_BRG = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
result_img_path = save_path + 'result_' + str(cluster) + '.png'
cv2.imwrite(result_img_path, result_BRG)
'''
result_BRG = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
result_img_path = save_path + 'result_all.png'
cv2.imwrite(result_img_path, result_BRG)
'''
counts = Counter(labels_flat)
# sort to ensure correct color percentage
counts = dict(sorted(counts.items()))
center_colors = centers
# We get ordered colors by iterating through the keys
ordered_colors = [center_colors[i] for i in counts.keys()]
hex_colors = [RGB2HEX(ordered_colors[i]) for i in counts.keys()]
rgb_colors = [ordered_colors[i] for i in counts.keys()]
#print(hex_colors)
index_bkg = [index for index in range(len(hex_colors)) if hex_colors[index] == '#000000']
#print(index_bkg[0])
#print(counts)
#remove background color
del hex_colors[index_bkg[0]]
del rgb_colors[index_bkg[0]]
# Using dictionary comprehension to find list
# keys having value .
delete = [key for key in counts if key == index_bkg[0]]
# delete the key
for key in delete: del counts[key]
fig = plt.figure(figsize = (6, 6))
plt.pie(counts.values(), labels = hex_colors, colors = hex_colors)
#define result path for labeled images
result_img_path = save_path + 'pie_color.png'
plt.savefig(result_img_path)
return rgb_colors
if __name__ == '__main__':
# construct the argument and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required = True, help = "path to image file")
ap.add_argument("-ft", "--filetype", required = True, help = "image filetype")
args = vars(ap.parse_args())
# setting path to model file
file_path = args["path"]
ext = args['filetype']
#accquire image file list
filetype = ('*.' + ext)
image_file_path = file_path + filetype
#accquire image file list
imgList = sorted(glob.glob(image_file_path))
image_file = imgList[0]
abs_path = os.path.abspath(image_file)
filename, file_extension = os.path.splitext(abs_path)
file_size = os.path.getsize(image_file)/MBFACTOR
# make the folder to store the results
#current_path = abs_path + '/'
base_name = os.path.splitext(os.path.basename(filename))[0]
print("Exacting traits for image : {0}\n".format(str(base_name)))
# save folder construction
mkpath = os.path.dirname(abs_path) +'/' + base_name
mkdir(mkpath)
save_path = mkpath + '/'
print ("results_folder: " + save_path)
if (file_size > 5.0):
print("It will take some time due to larger file size {0} MB".format(str(int(file_size))))
else:
print("Segmentaing plant object using automatic color clustering method... ")
image = cv2.imread(image_file)
print("Shape: {}".format(image.shape))
#make backup image
orig = image.copy()
################################
r, g, b = cv2.split(orig)
r = r.flatten()
g = g.flatten()
b = b.flatten()
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(r, g, b)
result_file = (save_path + base_name + 'color_distributation.png')
plt.savefig(result_file)
################################################
args_colorspace = 'lab'
args_channels = '1'
args_num_clusters = 2
#color clustering based plant object segmentation
thresh = color_cluster_seg(orig, args_colorspace, args_channels, args_num_clusters)
# save segmentation result
result_file = (save_path + base_name + '_seg' + file_extension)
#print(filename)
cv2.imwrite(result_file, thresh)
num_clusters = 5
#save color quantization result
#rgb_colors = color_quantization(orig, thresh, save_path, num_clusters)
rgb_colors = color_region(orig, thresh, save_path, num_clusters)
#print ("List index-value are : ")
selected_color = rgb2lab(np.uint8(np.asarray([[rgb_colors[0]]])))
for index, value in enumerate(rgb_colors):
#print(index, value)
curr_color = rgb2lab(np.uint8(np.asarray([[value]])))
diff = deltaE_cie76(selected_color, curr_color)
print(index, value, diff)