如何使用 python 计算图像中特定颜色的像素数
How to count number of pixels for specific color from Image Using python
我想计算图像中特定颜色的像素总数 将包含该特定颜色的所有 Sade,例如蓝色,它将检查所有蓝色阴影并将 return蓝色像素总数
输入的内容是检查蓝色而不是蓝色阴影
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))
蓝色示例图片
您可以使用:
blue = sum(np.all(np.array(im.getdata()) == np.array([30,144,255]), axis=1))
请注意,蓝色值更有可能为零,因为图像有一个精确的行等于 [30,144,255]
的可能性很小
如果您想要不同深浅的蓝色,则必须指定要添加到变量中的 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))
我建议在 Python/OpenCV 中使用 HSV 颜色 space 的一系列色调来制作面具。然后计算掩码中 non-zero 个值的数量。
输入:
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
我想计算图像中特定颜色的像素总数 将包含该特定颜色的所有 Sade,例如蓝色,它将检查所有蓝色阴影并将 return蓝色像素总数
输入的内容是检查蓝色而不是蓝色阴影
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))
蓝色示例图片
您可以使用:
blue = sum(np.all(np.array(im.getdata()) == np.array([30,144,255]), axis=1))
请注意,蓝色值更有可能为零,因为图像有一个精确的行等于 [30,144,255]
如果您想要不同深浅的蓝色,则必须指定要添加到变量中的 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))
我建议在 Python/OpenCV 中使用 HSV 颜色 space 的一系列色调来制作面具。然后计算掩码中 non-zero 个值的数量。
输入:
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