python - 如何用Python和Opencv计算白色区域?

标签 python opencv

我正在尝试计算该图像中白色像素的面积:

我的代码:

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

img = cv2.imread("tepsi.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv,(21, 10, 15), (30, 255, 255) )
cv2.imshow("orange", mask)
cv2.waitKey()
cv2.destroyAllWindows()

如何解决这个问题?实际上我的目的是找到食物托盘的空区域。

感谢您的回答。

最佳答案

enter image description here感谢您的建议,我找到了解决上图问题的方法。但我会尝试你的建议...

注意:我的托盘是白色区域,目前保持不变。

我的解决方案:

import numpy as np
import cv2

image = cv2.imread("petibor_biskuvi.png")
dimensions = image.shape

height= image.shape[0]
width = image.shape[1]
size = height*width

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)


_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV)

cnts, hier = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
size_elements = 0
for cnt in cnts:
    cv2.drawContours(image,cnts, -1, (0, 0, 255), 3)
    size_elements += cv2.contourArea(cnt)
    print(cv2.contourArea(cnt))



cv2.imshow("Image", image)
print("size elements total : ", size_elements)
print("size of pic : ", size)
print("rate of fullness : % ", (size_elements/size)*100)
cv2.waitKey(0)

关于python - 如何用Python和Opencv计算白色区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62492753/

相关文章:

python - 使用otsu二值化时如何使用OpenCV python分割图像

python - 使用 Python Pillow lib 设置颜色深度

python - pandas:输出两列字符串列表之间的差异

python - 如何在 Visual Studio IronPython 项目中配置导入路径

python - 将文本文件中的 0 和 1 转换为二维数组

c++ - QT 找不到 openCV 的库

c++ - OpenCV 距离变换中的像素索引

python - OpenCV边界矩形(Python)

python - 调用 OpenCV 函数时冲突的 Numpy 和 OpenCV2 数据类型

python - 将图像拼接在一起 Opencv -Python