我正在尝试计算该图像中白色像素的面积:
我的代码:
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()
如何解决这个问题?实际上我的目的是找到食物托盘的空区域。
感谢您的回答。
最佳答案
感谢您的建议,我找到了解决上图问题的方法。但我会尝试你的建议...
注意:我的托盘是白色区域,目前保持不变。
我的解决方案:
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/