-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJim_ColorHistogram_ColorScatterPlot.py
More file actions
278 lines (202 loc) · 8.61 KB
/
Copy pathJim_ColorHistogram_ColorScatterPlot.py
File metadata and controls
278 lines (202 loc) · 8.61 KB
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
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 6 22:38:00 2023
Accepts image and then spits out
HSV histogram and color scatterplot
I THINK THE CIRCLE FUNCTION IS WORKING!!!!!!!!
@author: J.A. LOMAS
"""
import cv2 as cv
import os
import numpy as np
import matplotlib.pyplot as plt
import traceback # for exception reporting
import pandas as pd
from math import pi
def getContours(binaryImage, mode='TREE', method=cv.CHAIN_APPROX_SIMPLE):
# returns filtered contours of binary image
# RETR_LIST for all, RETR_TREE for external only(?), CCOMP for
try:
# get contours (works for different versions of openCV)
if mode == 'LIST': # gets all of the contours regardless of hierarchy(?)
try:
contours, _ = cv.findContours(binaryImage, cv.RETR_CCOMP, method)
except:
_, contours, _ = cv.findContours(binaryImage, cv.RETR_CCOMP, method)
else:
try:
contours, _ = cv.findContours(binaryImage, cv.RETR_TREE, method)
except:
_, contours, _ = cv.findContours(binaryImage, cv.RETR_TREE, method)
return contours
except:
return None
print(traceback.format_exc())
def getBiggestContour(contours, num_contours=1):
# returns the largest contour(s)
try:
if len(contours) == 1:
return contours
elif len(contours) > 1 and num_contours == 1:
biggest = contours[0]
for contour in contours:
if cv.contourArea(contour) > cv.contourArea(biggest):
biggest = contour
return (biggest,)
elif len(contours) > 1 and len(contours) > num_contours > 1:
biggest = list(contours[:num_contours])
for contour in contours[num_contours:]:
areas = [cv.contourArea(cont) for cont in biggest]
if cv.contourArea(contour) > min(areas):
_ = biggest.pop(areas.index(min(areas)))
biggest.append(contour)
return tuple(biggest)
elif num_contours > len(contours) > 1:
return contours
else:
return contours
except:
print(traceback.format_exc())
#--------------------------------------------------imageFile is some RGB image
imageFile = os.path.join('Xiao_1um.png')
pix2 = cv.imread(imageFile)
#-------------------------------------------------Convert the BRG image to RGB
img = cv.cvtColor(pix2, cv.COLOR_BGR2RGB)
try:
# convert to grayscale before thresholding
#(we *could* do color thresholding as well if necessary)
grayscale_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
# threshold *inverted* image (first number is cutoff value,
# second number assigned to all pixels exceeding cutoff)
bin_img = cv.threshold(grayscale_img,
25,
255,
cv.THRESH_BINARY_INV)[1]
# get list of contours:
cntrs = getContours(cv.bitwise_not(bin_img))
# keep biggest contour only:
cntr = getBiggestContour(cntrs)
# show contour:
img_cntr = img.copy()
cv.drawContours(img_cntr, cntr, -1, color=(255, 0, 0), thickness=3)
fig0 = plt.imshow(img_cntr)
# create blank mask:
mask = np.full(bin_img.shape, 0, dtype=np.uint8)
# use biggest contour to define ROI area:
cv.drawContours(mask, cntr, -1, (255, 255, 255), cv.FILLED)
#---------------------------------------------Convert the RGB image to HSV
pix2 = cv.cvtColor(img, cv.COLOR_RGB2HSV)
# --------------------------------------------------Splitting HSV channels
h, s, v = cv.split(pix2)
except:
print(traceback.format_exc())
#--------------------------------Making some empty matrices of the same size
pix_test = np.zeros((pix2.shape[0],pix2.shape[1]))
pix_mid = np.zeros((pix2.shape[0],pix2.shape[1]))
#----------------------------------------Creating a list of all the HSV values
flat_pix2=pix2.reshape((pix2.shape[1]*pix2.shape[0],3))
flat_mask = mask.reshape((mask.shape[1] * mask.shape[0]), 1)
#----------------------------------------Creating a list of all the RGB values
flat_img=img.reshape((img.shape[1]*img.shape[0],3))
#--------------------------------------------------------------------------
#----------------------------------------------------FILTERING-------------
#--------------------------------------------------------------------------
dictionary_HSV = {
'H':flat_pix2[:,0],
'S':flat_pix2[:,1],
'V':flat_pix2[:,2],
'filter':flat_mask[:,0]}
df = pd.DataFrame(dictionary_HSV)
dictionary_RGB = {
'R':flat_img[:,0],
'G':flat_img[:,1],
'B':flat_img[:,2],
'filter':flat_mask[:,0]}
dfimg = pd.DataFrame(dictionary_RGB)
#------------------------------------Now filter just the image portion
#------------------------------------use "!=" instead of "==" to get background
df2 = df.loc[df['filter'] == 255]
df3 = dfimg.loc[dfimg['filter'] == 255]
#---------------------------------------------------------------------------
#-----------------------------------------------HISTOGRAM ------------------
#---------------------------------------------------------------------------
from scipy.stats import circmean, circstd
#-------------------------------------------------------------------------RGB
R_DIST = df3['R']
R_mu = np.mean(R_DIST)
R_sig = np.std(R_DIST)
G_DIST = df3['G']
G_mu = np.mean(G_DIST)
G_sig = np.std(G_DIST)
B_DIST = df3['B']
B_mu = np.mean(B_DIST)
B_sig = np.std(B_DIST)
fig1, axes = plt.subplots(1, 3, sharey=False, tight_layout=True)
# We can set the number of bins with the *bins* keyword argument.
axes[0].hist(R_DIST, bins=256)
axes[0].set_title('R_dist\n'
fr'$\mu={R_mu:.0f}$, $\sigma={R_sig:.0f}$')
axes[1].hist(G_DIST, bins=256)
axes[1].set_title('G_dist\n'
fr'$\mu={G_mu:.0f}$, $\sigma={G_sig:.0f}$')
axes[2].hist(B_DIST, bins=256)
axes[2].set_title('B_dist\n'
fr'$\mu={B_mu:.0f}$, $\sigma={B_sig:.0f}$')
#----------------------------------------------------------------------H Data
#----------------------------------circular mean and stdev since H is a circle
H_DIST = df2['H'].round(0).astype(int)
#H_DIST = H_DIST*2
rads = np.deg2rad(H_DIST)
circmn = circmean(rads*2)
h_mu = np.rad2deg(circmn)
crcstd = circstd(rads*2)
h_sig = np.rad2deg(crcstd)
#-----------------------------------------------------------------------S Data
S_DIST = df2['S']
s_mu = np.mean(S_DIST)
s_sig = np.std(S_DIST)
#-----------------------------------------------------------------------V Data
V_DIST = df2['V']
#------------For filtering just the V values from the thresholding filter
#FILTERED_V_DIST = [i for i,j in zip(V_DIST, flat_mask) if j == 255]
v_mu = np.mean(V_DIST)
v_sig = np.std(V_DIST)
fig2, axs = plt.subplots(1, 3, sharey=False, tight_layout=True)
# We can set the number of bins with the *bins* keyword argument.
axs[0].hist(H_DIST, bins=360)
axs[0].set_title('H_dist\n'
fr'$\mu={h_mu:.0f}$, $\sigma={h_sig:.0f}$')
axs[1].hist(S_DIST, bins=256)
axs[1].set_title('S_dist\n'
fr'$\mu={s_mu:.0f}$, $\sigma={s_sig:.0f}$')
axs[2].hist(V_DIST, bins=256)
axs[2].set_title('V_dist\n'
fr'$\mu={v_mu:.0f}$, $\sigma={v_sig:.0f}$')
#-----------------------------------------------------------------------------
#-----------------------------------------------SCATTER PLOT -----------------
#-----------------------------------------------------------------------------
#----------------------------------------------------------Define size of dots
area = 1.5
#------------------------------------Making sure color values are not squished
graphbounds = pd.Series([0, pi/2, pi])
satbounds = pd.Series([255, 255, 255])
rads = pd.concat([rads, graphbounds])
S_DIST = pd.concat([S_DIST, satbounds])
#--------------------------------------------------------------PLOTTING STARTS
fig3 = plt.figure()
axe = fig3.add_subplot(projection='polar')
axe.set_yticks([100, 200, 255])
axe.errorbar(circmn,s_mu,
xerr= crcstd,yerr= s_sig,
capsize=7,
fmt= '^',
c='k')
c = axe.scatter(rads*2,
S_DIST,
c=rads,
s=area,
cmap='hsv',
alpha=1)
#----------------------------------OpenCV only has H values between 0 and 180
#ax.set_thetamin(0)
#ax.set_thetamax(180)