简体   繁体   English

使用 OpenCV 和 Python 获取图像中颜色的百分比

[英]Getting the percentage of a color in an image using OpenCV and Python

So I have this python script that detects and print a range of HSV color in an image but i want to add another functionality to it.所以我有这个 python 脚本来检测和打印图像中的一系列 HSV 颜色,但我想向它添加另一个功能。

It want it to be able to print the percentage of that color.它希望它能够打印该颜色的百分比。

My Script:我的脚本:

import cv2
import numpy as np
import matplotlib.pyplot as plt


img = cv2.imread('C:/Users/Vishu Rana/Documents/PY/test_cases/image.jpg')


grid_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(20,8))
plt.imshow(grid_RGB) # Printing the original picture after converting to RGB


grid_HSV = cv2.cvtColor(grid_RGB, cv2.COLOR_RGB2HSV) # Converting to HSV
lower_green = np.array([25,52,72])
upper_green = np.array([102,255,255])

mask= cv2.inRange(grid_HSV, lower_green, upper_green)
res = cv2.bitwise_and(img, img, mask=mask) # Generating image with the green part

print("Green Part of Image")
plt.figure(figsize=(20,8))
plt.imshow(res)

What do i need to add in this code to make it also print the percentage of green color.我需要在此代码中添加什么才能使其也打印绿色的百分比。

OpenCV arrays create a mask that uses the value 255. A simple way to get the percentage of green is simply implement the following code after you generate the mask. OpenCV 数组创建一个使用值 255 的掩码。获取绿色百分比的一种简单方法是在生成掩码后执行以下代码。

    green_perc = (mask>0).mean()

A more thorough explanation was asked about why this works.有人询问了有关为什么会起作用的更彻底的解释。 When OpenCV creates a mask it will create a 2 dimensional array with values of 0s and values of 255. In the context of this question the values of 255 are the parts of the mask that indicate the picture is green.当 OpenCV 创建一个掩码时,它将创建一个值为 0 和 255 的二维数组。在这个问题的上下文中,255 的值是掩码中指示图片为绿色的部分。

The reason that (mask>0).mean() works is because we only have values of 255 and 0. Mask > 0 will create a boolean array of True/False for every value in our mask. (mask>0).mean() 起作用的原因是因为我们只有 255 和 0 的值。Mask > 0 将为掩码中的每个值创建一个 True/False 布尔数组。

The value True will indicate that the part of the array is green and the value of False will indicate it is not green. True 值表示数组的一部分是绿色的,False 值表示它不是绿色的。 Taking the mean of this boolean array will give us the percentage of the array that is green.取这个布尔数组的平均值将为我们提供绿色数组的百分比。 (when taking the mean of a boolean array True will take the value of 1 and False will take the value of 0). (当取布尔数组的平均值时,True 将取值为 1,而 False 将取值为 0)。

Another way to get the same result is to implement code like this.获得相同结果的另一种方法是实现这样的代码。

green_perc = (mask==255).mean()

A comment above also mentions the solution of np.sum(mask)/np.size(mask).上面的评论还提到了 np.sum(mask)/np.size(mask) 的解决方案。 This does not work right because OpenCV uses the value 255. You could tweak this to get the same percentage by dividing this value by 255.这不起作用,因为 OpenCV 使用值 255。您可以通过将此值除以 255 来调整它以获得相同的百分比。

green_perc = (np.sum(mask) / np.size(mask))/255

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM