如何使用python open cv计算图像中的红色像素数?
我目前正在做一个项目,我需要计算图像中出现的红、蓝、绿、黄、橙和紫像素的数量。由于我是opencv新手,我找不到任何方法可以解决我的问题。不用使用opencv,您也可以轻松做到这一点 假设您有一个名为analysis.PNG的图像,为了找到RGB百分比,您可以使用以下代码如何使用python open cv计算图像中的红色像素数?,python,opencv,image-processing,Python,Opencv,Image Processing,我目前正在做一个项目,我需要计算图像中出现的红、蓝、绿、黄、橙和紫像素的数量。由于我是opencv新手,我找不到任何方法可以解决我的问题。不用使用opencv,您也可以轻松做到这一点 假设您有一个名为analysis.PNG的图像,为了找到RGB百分比,您可以使用以下代码 from scipy import misc def picture_to_arr(image): arr = misc.imread(image) arr_list=arr.t
from scipy import misc
def picture_to_arr(image):
arr = misc.imread(image)
arr_list=arr.tolist()
r=g=b=0
for row in arr_list:
for item in row:
r=r+item[0]
g=g+item[1]
b=b+item[2]
total=r+g+b
red=r/total*100
green=g/total*100
blue=b/total*100
print ("the percentage of red content=",red,"%")
print ("the percentage of green content=",green,"%")
print ("the percentage of blue content=",blue,"%")
picture_to_arr('analysis.PNG')
一个好的开始是使用直方图绘制图像的颜色。看起来是这样的:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('home.jpg')
color = ('b','g','r')
for i,col in enumerate(color):
histr = cv2.calcHist([img],[i],None,[256],[0,256])
plt.plot(histr,color = col)
plt.xlim([0,256])
plt.show()
在更好地理解OpenCV之后,您可以轻松地适应解决初始问题。请向我们展示一些代码和您所经历的一些努力。您必须定义什么是红色?RGB[255,0,0]中的纯红色或任何红色阴影?如果是后者,您应该使用HSV,这样可以更容易地获得正确的颜色,然后对它们进行计数:我发现您需要计算一些红色、蓝色和紫色像素。但紫色是红色和蓝色的混合物。我是否正确地理解了您只想计算一些具有一些硬定义的RGB颜色值的像素数?我看到您想计算一些红色、蓝色和紫色像素数。但紫色是红色和蓝色的混合物。我是否正确地理解了,您只想计算一些具有硬定义的RGB颜色值的像素数?