Toggle navigation
Toggle navigation
This project
Loading...
Sign in
이수빈
/
Find_your_own_personal_color
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
starbucksdolcelatte
2019-04-05 15:29:51 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
ebfea0368ee2c0f8733d2b6e7b9758086770d3f8
ebfea036
1 parent
8c9f3c7f
Created DominantColors class
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
99 additions
and
0 deletions
dominant_colors.py
dominant_colors.py
0 → 100644
View file @
ebfea03
import
cv2
import
numpy
as
np
from
sklearn.cluster
import
KMeans
import
matplotlib.pyplot
as
plt
from
mpl_toolkits.mplot3d
import
Axes3D
class
DominantColors
:
CLUSTERS
=
None
IMAGE
=
None
COLORS
=
None
LABELS
=
None
def
__init__
(
self
,
image
,
clusters
=
3
):
self
.
CLUSTERS
=
clusters
self
.
IMAGE
=
image
def
dominantColors
(
self
):
#read image
#img = cv2.imread(self.IMAGE)
img
=
self
.
IMAGE
#convert to rgb from bgr
img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_BGR2RGB
)
#reshaping to a list of pixels
img
=
img
.
reshape
((
img
.
shape
[
0
]
*
img
.
shape
[
1
],
3
))
#save image after operations
self
.
IMAGE
=
img
#using k-means to cluster pixels
kmeans
=
KMeans
(
n_clusters
=
self
.
CLUSTERS
)
kmeans
.
fit
(
img
)
#the cluster centers are our dominant colors.
self
.
COLORS
=
kmeans
.
cluster_centers_
#save labels
self
.
LABELS
=
kmeans
.
labels_
#returning after converting to integer from float
return
self
.
COLORS
.
astype
(
int
)
def
rgb_to_hex
(
self
,
rgb
):
return
'#
%02
x
%02
x
%02
x'
%
(
int
(
rgb
[
0
]),
int
(
rgb
[
1
]),
int
(
rgb
[
2
]))
def
plotClusters
(
self
):
#plotting
fig
=
plt
.
figure
()
ax
=
Axes3D
(
fig
)
for
label
,
pix
in
zip
(
self
.
LABELS
,
self
.
IMAGE
):
ax
.
scatter
(
pix
[
0
],
pix
[
1
],
pix
[
2
],
color
=
self
.
rgb_to_hex
(
self
.
COLORS
[
label
]))
plt
.
show
()
def
plotHistogram
(
self
):
#labels form 0 to no. of clusters
numLabels
=
np
.
arange
(
0
,
self
.
CLUSTERS
+
1
)
#create frequency count tables
(
hist
,
_
)
=
np
.
histogram
(
self
.
LABELS
,
bins
=
numLabels
)
hist
=
hist
.
astype
(
"float"
)
hist
/=
hist
.
sum
()
#appending frequencies to cluster centers
colors
=
self
.
COLORS
#descending order sorting as per frequency count
colors
=
colors
[(
-
hist
)
.
argsort
()]
hist
=
hist
[(
-
hist
)
.
argsort
()]
#creating empty chart
chart
=
np
.
zeros
((
50
,
500
,
3
),
np
.
uint8
)
start
=
0
#creating color rectangles
for
i
in
range
(
self
.
CLUSTERS
):
end
=
start
+
hist
[
i
]
*
500
#getting rgb values
r
=
colors
[
i
][
0
]
g
=
colors
[
i
][
1
]
b
=
colors
[
i
][
2
]
#using cv2.rectangle to plot colors
cv2
.
rectangle
(
chart
,
(
int
(
start
),
0
),
(
int
(
end
),
50
),
(
r
,
g
,
b
),
-
1
)
start
=
end
#display chart
plt
.
figure
()
plt
.
axis
(
"off"
)
plt
.
imshow
(
chart
)
plt
.
show
()
Please
register
or
login
to post a comment