如何使用python open cv计算图像中的红色像素数?

2024-04-16 14:58:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个项目,在这个项目中,我需要计算图像中出现的红色、蓝色、绿色、黄色、橙色和紫色像素的数量。因为我刚接触opencv,所以我找不到任何方法可以解决我的问题。。。在


Tags: 项目方法图像数量像素opencv橙色蓝色
2条回答

一个好的开始应该是tutorial here,使用直方图来绘制图像的颜色。看起来像这样:

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之后,您可以很容易地适应解决最初的问题。在

无需使用“打开cv”,也可以轻松完成相同的操作。在

假设您有一个名为的图像分析.PNG,因此为了找到RGB百分比,可以使用以下代码。。在

    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')

相关问题 更多 >