opencv-python简单提取海报图片主色

颜色的划分主要参考这个,代码思路十分简单,写给别人的,拿来统计某一季小猪佩奇海报颜色的分布占比,基本可以调查的小朋友最喜欢颜色排名对应起来。

小猪佩奇的海报 嘿嘿

颜色分类参考,靠个人感觉。。品红我感觉就很想分红诶

就是转变为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))

应该也不是很准,但多少能和下面这个调查有些对应起来。。。。。就当是个玩具吧

结局:橙色结果排第一是用的第一种统计方法,蓝色第一的是第二种。据某新闻传播文科生回复,结论可以让她使用。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值