颜色的划分主要参考这个,代码思路十分简单,写给别人的,拿来统计某一季小猪佩奇海报颜色的分布占比,基本可以调查的小朋友最喜欢颜色排名对应起来。
小猪佩奇的海报 嘿嘿
颜色分类参考,靠个人感觉。。品红我感觉就很想分红诶
就是转变为HSV空间上,遍历像素点,参考上图,按照颜色的分类,自己设置相似的颜色类别。
具体占比的计算,不知道哪种更科学,一种是 计算单张图片某类颜色的占比,最后取一季海报的平均数,
另一种粗暴的计算每个颜色类别在整季海报像素中的比值。这里用的是第一种。小学生代码。。如下:
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 8 21:35:55 2020
@author: chen
"""
import cv2
import numpy as np
import glob
import os
import tqdm
import pprint
#c_d= {'红':0,
# '橙':0,
# '黄':0,
# '黄绿':0,
# '绿':0,
# '青绿':0,
# '青':0,
# '靛':0,
# '蓝':0,
# '紫':0,
# '品':0,
# '紫红':0
# }
c_d= {'红':0,
'橙':0,
'黄':0,
'绿':0,
'蓝':0,
'紫':0,
'黑':0,
'白':0,
'粉':0,
'棕':0,
'灰':0,
}
img_path_file = 'D:\pig'
def rgb2hsv(r, g, b):
r, g, b = r/255.0, g/255.0, b/255.0
mx = max(r, g, b)
mn = min(r, g, b)
df = mx-mn
if mx == mn:
h = 0
elif mx == r:
h = (60 * ((g-b)/df) + 360) % 360
elif mx == g:
h = (60 * ((b-r)/df) + 120) % 360
elif mx == b:
h = (60 * ((r-g)/df) + 240) % 360
if mx == 0:
s = 0
else:
s = df/mx
v = mx
return h, s, v
all_imgs = glob.glob(os.path.join(img_path_file,'*.jpg'))
persent = {'红':0,
'橙':0,
'黄':0,
'绿':0,
'蓝':0,
'紫':0,
'黑':0,
'白':0,
'粉':0,
'棕':0,
'灰':0,
}
total = 0
for img_path in all_imgs:
img = cv2.imread(img_path)
img = cv2.resize(img,(608,608))
row = img.shape[0]
col = img.shape[1]
R = img[:,:,2]
G = img[:,:,1]
B = img[:,:,0]
for r in range(row):
for c in range(col):
# h = h_tabel[r][c]
h,s,v = rgb2hsv(R[r][c],G[r][c],B[r][c])
# H_tabel[r][c] = h
if (345<h<360) or (0<h<=15):
if 0.45<=s<=1:
c_d['红'] +=1
elif 0.08<s<0.45:
c_d['粉'] +=1
if 15 <h<=45 :#and: (0.16<s<1) and (0.16<v<1):
c_d['橙'] +=1
if 53<h<=65 :#and (0.16<s<1) and (0.16<v<1):
c_d['黄'] +=1
if 105<h<=150 :#and (0.16<s<1) and (0.16<v<1):
c_d['绿'] +=1
# elif 200<h<=225:
# c_d['蓝'] +=1
if 225<h<255 :#and (0.16<s<1) and (0.16<v<1):
c_d['蓝'] +=1
if (270<h<290):# and (0.16<s<1) and (0.16<v<1):
c_d['紫'] +=1
elif 295<h<=320:
c_d['粉'] +=1
if 0<=v<=(0.15):
c_d['黑'] +=1
if (0<=s<=0.005) and (0.99<=v<=1):
c_d['白'] +=1
if (0<=s<=0.08) and (0.6<v<=0.8):
c_d['灰'] +=1
if (25<h<40) and (0.5<s<0.64) and (0.44<v<0.6):
c_d['棕'] +=1
c_d_order=sorted(c_d.items(),key=lambda x:x[1],reverse=True)
#直接按总像素计算
#for i,n in c_d_order:
# total +=n
#persent_final = {}
#for k ,v in c_d_order:#.items():
# persent_final[k] = v/total
#persent_final_ord=sorted(persent_final.items(),key=lambda x:x[1],reverse=True)
##pprint.pprint(persent_final_ord)
#for fk , fp in persent_final_ord:
# print(fk +': {:.4f}%'.format(fp*100))
#计算单张图百分比
for i,n in c_d_order:
total +=n
for k ,v in c_d_order:#.items():
persent[k] += v/total A
c_d= {'红':0,
'橙':0,
'黄':0,
'绿':0,
'蓝':0,
'紫':0,
'黑':0,
'白':0,
'粉':0,
'棕':0,
'灰':0,
}
total = 0
persent_final = {}
for kp,vp in persent.items():
persent_final[kp] = vp/len(all_imgs)
persent_final_ord=sorted(persent_final.items(),key=lambda x:x[1],reverse=True)
for fk , fp in persent_final_ord:
print(fk +': {:.4f}%'.format(fp*100))
应该也不是很准,但多少能和下面这个调查有些对应起来。。。。。就当是个玩具吧
结局:橙色结果排第一是用的第一种统计方法,蓝色第一的是第二种。据某新闻传播文科生回复,结论可以让她使用。。。