python - 计算图像中白色的总像素数

标签 python image opencv image-processing

我想计算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()

结果:

enter image description here

好...
发现背景上的许多灰色像素为白色。

关于python - 计算图像中白色的总像素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60551064/

相关文章:

python - Python 上的拉普拉斯过滤器无法按我的预期工作

python - 将 Pandas 数据框列转换为 np.datetime64

javascript - 类似Photoshop中功能 "Levels"的实现

python - Pandas groupby 聚合传递组名进行聚合

python - 如何获取桌面上像素的颜色? (Linux)

WPF 图像缓存

c++ - 将 Mat 图像转置为更大的 Mat 图像,Opencv

ios - iOS 上的 OpenCV ZXing 不兼容

python - 抓取时获取错误实例方法没有属性 '__getitem__'

python - 替换列表中的元素