[英]Python comparing which RGB value has the most pixels in an image
So I am using openCV and I am taking a list of RGB values, comparing them to a selection of pixels from an image and then returning which RGB value from the list there is the most of. 因此,我正在使用openCV,并获取RGB值列表,将它们与图像中的像素选择进行比较,然后返回列表中最多的RGB值。 I have a file called colorCalc.py and face.py. 我有一个名为colorCalc.py和face.py的文件。 ColorCalc is just methods while face uses methods from Colorcalc.py. ColorCalc只是方法,而face使用Colorcalc.py中的方法。 The main issue I have is with the function colorCheck, it seems that the first if statement does not seem to check as it is always true, which based on the numbers on the calculation from colorCheck, it should not be. 我遇到的主要问题是函数colorCheck,似乎第一个if语句似乎始终不正确,因为根据colorCheck计算得出的数字,它应该不正确。 The face.py script is also included in case someone wants to replicate what I have done exactly. 如果有人想复制我所做的确切工作,还包括face.py脚本。 The below is from ColorCalc.py 下面是从ColorCalc.py
def colorCheck(src,source):
prevCount=0
counter=0;
ans=20
for i in range(len(src)):
counter=0
for s in range(len(source)):
if colorDifference(src[0][0],src[0][1],src[0][2],source[0,s,0],source[0,s,1], source[0,s,2])>2:
counter= counter+1
print("OKAY")
if ans== 20:
prevCount=counter
counter=0
ans=src[0,3]
elif counter>=prevCount:
prevCount=counter
counter=0
ans=src[0,3]
#print(colorDifference(src[0][0],src[0][1],src[0][2],source[0,s,0],source[0,s,1],source[0,s,2]))
return ans
This just creates the array that I am comparing to While the next function compares RGB values 这只是创建我要比较的数组,而下一个函数比较RGB值
def colorArray():
colArr=[[0 for x in range(3)] for y in range(12)]
#these colors are from darkest to lightest skin tone. The number at the end is to tell which value we are considering after sorting
#black skin
colArr[0]=[117,85,61,0]
colArr[1]=[159,136,121,1]
#bronze Skin
colArr[2]=[156,115,69,2]
colArr[3]=[182,157,69,3]
#medium brown skin
colArr[4]=[196,142,88,4]
colArr[5]=[217,181,149,5]
#light brown skin
colArr[6]=[220,180,119,6]
colArr[7]=[236,195,155,7]
#cream-colored skin
colArr[8]=[230,189,149,8]
colArr[9]=[239,210,180,9]
#pale skin
colArr[10]=[231,207,183,10]
colArr[11]=[240,222,205,11]
colArr= numpy.asarray(colArr)
return colArr
def colorDifference(r,g,b,r1,g1,b1):
red= abs((r-r1)**2)
blue= abs((b-b1)**2)
green= abs((g-g1**2))
answer=math.sqrt(red+blue+green)
return answer;
The next is how I am actually using this, which is the face.py script 接下来是我实际使用的方式,这是face.py脚本
import numpy as np
import cv2 as cv
import math
import colorCalc as col
#load xml files
face_cascade = cv.CascadeClassifier('C:\Program
Files\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
eye_cascade = cv.CascadeClassifier('C:\Program
Files\opencv\sources\data\haarcascades\haarcascade_eye.xml')
#define image and convert to grayscale
pic= cv.imread('face.jpg')
gray = cv.imread('face.jpg',0)
img= pic
color = np.array([])
#scan for faces and draw rectangle
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
x1= faces[0][0]
x2= faces[0][2]
y1= faces[0][1]
y2= faces[0][3]
This takes a slice of a numpyArray created with openCV 这需要使用openCV创建的numpyArray的一部分
source=img[x1:x1+x2, y1:y1+y2, :]
while this actually calls the function 虽然这实际上调用了该函数
colArr=col.colorArray()
answer=col.colorCheck(colArr,source)
print(answer)
You have a typo. 你有错字 Change this line 更改此行
green= abs((g-g1**2))
to 至
green= abs((g-g1)**2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.