-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorHistogram_ColorScatterPlot.py
More file actions
114 lines (71 loc) · 2.96 KB
/
Copy pathColorHistogram_ColorScatterPlot.py
File metadata and controls
114 lines (71 loc) · 2.96 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
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 6 22:38:00 2023
Accepts image and then spits out
HSV histogram and color scatterplot
-FIX STATISTICS
@author: J.A. LOMAS
"""
import cv2 as cv
import os
import numpy as np
#------------------------------------------------imageFile is some RGB image
imageFile = os.path.join('Green_Sanghyun.jpg')
pix2 = cv.imread(imageFile)
#-----------------------------------------------Convert the BRG image to RGB
img = cv.cvtColor(pix2, cv.COLOR_BGR2RGB)
#-----------------------------------------------Convert the RGB image to HSV
pix2 = cv.cvtColor(img, cv.COLOR_RGB2HSV)
#-----------------------------------------------------Splitting HSV channels
h, s, v = cv.split(pix2)
#--------------------------------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))
#---------------------------------------------------------------------------
#-----------------------------------------------HISTOGRAM ------------------
#---------------------------------------------------------------------------
from scipy.stats import circmean, circstd
import matplotlib.pyplot as plt
H_DIST = flat_pix2[:,0]
rads = np.deg2rad(H_DIST)
circmean = circmean(rads,
high = np.pi,
low=0)
h_mu = np.rad2deg(circmean)
circstd = circstd(rads,
high = np.pi,
low=0)
h_sig = np.rad2deg(circstd)
S_DIST = flat_pix2[:,1]
s_mu = np.mean(S_DIST)
s_sig = np.std(S_DIST)
V_DIST = flat_pix2[:,2]
v_mu = np.mean(V_DIST)
v_sig = np.std(V_DIST)
fig1, axs = plt.subplots(1, 3, sharey=True, tight_layout=True)
# We can set the number of bins with the *bins* keyword argument.
axs[0].hist(H_DIST, bins=180)
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
fig2 = plt.figure()
ax = fig2.add_subplot(projection='polar')
c = ax.scatter(np.pi*H_DIST/180, S_DIST,
c=np.pi*H_DIST/180, s=area,
cmap='hsv', alpha=0.75)
#----------------------------------OpenCV only has H values between 0 and 180
ax.set_thetamin(0)
ax.set_thetamax(180)
plt.show()