如何使用 python 计算图像中特定颜色的像素数
我想计算特定颜色图像的像素总数,女巫将拥有该特定颜色的所有萨德,例如蓝色,它将检查所有蓝色阴影并返回蓝色像素总数
输入的内容但检查蓝色不适用于蓝色阴影
from PIL import Image
im = Image.open('rin_test/Images33.png')
black = 0
red = 0
for pixel in im.getdata():
if pixel == (30,144,255): # if your image is RGB (if RGBA, (0, 0, 0, 255) or so
blue += 1
print('blue=' + str(blue))
I want to calculate the total number of pixels from a image for specific color witch will have all Sade of that specific color for example blue its will check all shade of blue and will return Total count of blue pixels
What is typed but its checking for blue not for shades of blue
from PIL import Image
im = Image.open('rin_test/Images33.png')
black = 0
red = 0
for pixel in im.getdata():
if pixel == (30,144,255): # if your image is RGB (if RGBA, (0, 0, 0, 255) or so
blue += 1
print('blue=' + str(blue))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议在 Python/OpenCV 中使用 HSV 颜色空间中的一系列色调来获得蒙版。然后计算掩码中非零值的数量。
输入:
掩码:
I would suggest using a range of hues in HSV color space in Python/OpenCV to get a mask. Then count the number of non-zero values in the mask.
Input:
Mask:
您可以使用:
请注意,蓝色值更有可能为零,因为图像有可能具有等于
[30,144,255]
的精确行You can use:
Note that the blue value is more likely to be zero since there is a slight chance for the image to have an exact row that is equal to
[30,144,255]
如果您想要不同深浅的蓝色,则必须指定要添加到变量中的 rgb 值范围(相当于不同深浅的蓝色))
例如:-
If you want different shades of blue, you have to specify a range of rgb values(that amount to different shades of blue) to be added to your variable)
For example :-