Python 计算图像中白色的总像素数

Python 计算图像中白色的总像素数,python,image,opencv,image-processing,Python,Image,Opencv,Image Processing,我想数一数图像中的白色像素数。 我定义了白色的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值

# 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定义中包含最大数量的颜色范围

您可以按照本节所述调整灵敏度

为了使解决方案更好地工作,还可以先增加亮度:

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()
结果:

嗯…

背景上的许多灰色像素显示为白色

谢谢,@Rotem,你能检查一下这个问题并给我一些建议吗?