我想计算this image中白色像素的数量。
我定义了白色的HSV值。
# white color
low_white = np.array([0, 0, 0])
high_white = np.array([0, 0, 255])
white_mask = cv2.inRange(hsv_frame, low_white, high_white)
white= cv2.bitwise_and(frame, frame, mask=white_mask)
但是,由于天气条件和图像中的阴影,它不会将整个白色像素都算作白色。它仅将驾驶室的顶部视为白色。
如何在白色HSV定义中包括最大数量的颜色范围?
最佳答案
您可以像本post中所述调整灵敏度。
为了使解决方案更好地工作,还可以先增加亮度:
import numpy as np
frame = cv2.imread('truck.png')
# Incere frame brighness
bright_frame = cv2.add(frame, 50)
# Conver image after appying CLAHE to HSV
hsv_frame = cv2.cvtColor(bright_frame, cv2.COLOR_BGR2HSV)
sensitivity = 70 # Higher value allows wider color range to be considered white color
low_white = np.array([0, 0, 255-sensitivity])
high_white = np.array([255, sensitivity, 255])
white_mask = cv2.inRange(hsv_frame, low_white, high_white)
white = cv2.bitwise_and(frame, frame, mask=white_mask)
cv2.imwrite('white.png', white) #Save out to file (for testing).
# Show result (for testing).
cv2.imshow('white', white)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果:
好...
发现背景上的许多灰色像素为白色。
关于python - 计算图像中白色的总像素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60551064/