如何使用 python 计算图像中特定颜色的像素数

发布于 2025-01-14 00:01:11 字数 498 浏览 1 评论 0原文

我想计算特定颜色图像的像素总数,女巫将拥有该特定颜色的所有萨德,例如蓝色,它将检查所有蓝色阴影并返回蓝色像素总数

输入的内容但检查蓝色不适用于蓝色阴影

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))

Sample Image for blue color
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱她像谁 2025-01-21 00:01:11

我建议在 Python/OpenCV 中使用 HSV 颜色空间中的一系列色调来获得蒙版。然后计算掩码中非零值的数量。

输入:

在此处输入图像描述

import cv2
import numpy as np

# load image
img = cv2.imread('blue_bag.png')

# convert to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)

# create mask for blue color in hsv
# blue is 240 in range 0 to 360, so for opencv it would be 120
lower = (100,100,100)
upper = (160,255,255)
mask = cv2.inRange(hsv, lower, upper)

# count non-zero pixels in mask
count=np.count_nonzero(mask)
print('count:', count)

# save output
cv2.imwrite('blue_bag_mask.png', mask)

# Display various images to see the steps
cv2.imshow('mask',mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

掩码:

在此处输入图像描述

count: 34231

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:

enter image description here

import cv2
import numpy as np

# load image
img = cv2.imread('blue_bag.png')

# convert to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)

# create mask for blue color in hsv
# blue is 240 in range 0 to 360, so for opencv it would be 120
lower = (100,100,100)
upper = (160,255,255)
mask = cv2.inRange(hsv, lower, upper)

# count non-zero pixels in mask
count=np.count_nonzero(mask)
print('count:', count)

# save output
cv2.imwrite('blue_bag_mask.png', mask)

# Display various images to see the steps
cv2.imshow('mask',mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

Mask:

enter image description here

count: 34231
许你一世情深 2025-01-21 00:01:11

您可以使用:

blue = sum(np.all(np.array(im.getdata()) == np.array([30,144,255]), axis=1))

请注意,蓝色值更有可能为零,因为图像有可能具有等于 [30,144,255] 的精确行

You can use:

blue = sum(np.all(np.array(im.getdata()) == np.array([30,144,255]), axis=1))

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]

野侃 2025-01-21 00:01:11

如果您想要不同深浅的蓝色,则必须指定要添加到变量中的 rgb 值范围(相当于不同深浅的蓝色))

例如:-

# If you want an array of the shades

blue = []
values = [<shades of blue>]
for pixel in im.getdata():
    for value in values:
        if pixel == value: 
            blue.append(value)
print('blue=' + str(blue))

# If you just want to add the number of times your shades appear

blue = 0
values = [<shades of blue>]
for pixel in im.getdata():
    for value in values:
        if pixel == value: 
            blue += 1
print('blue=' + str(blue))

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 :-

# If you want an array of the shades

blue = []
values = [<shades of blue>]
for pixel in im.getdata():
    for value in values:
        if pixel == value: 
            blue.append(value)
print('blue=' + str(blue))

# If you just want to add the number of times your shades appear

blue = 0
values = [<shades of blue>]
for pixel in im.getdata():
    for value in values:
        if pixel == value: 
            blue += 1
print('blue=' + str(blue))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文