gpt4 book ai didi

python - 每行中绿色和白色像素的百分比

转载 作者:行者123 更新时间:2023-12-05 04:23:03 32 4
gpt4 key购买 nike

我有一个输入图像,如下所示。 enter image description here

我需要获取每行图像中绿色和白色像素的总百分比和计数,并将特定行作为输出。我已经尝试过获取百分比和单行计数的代码。如下所示:

import cv2
import numpy as np
from PIL import Image

img_s = Image.open('img_15.png')
width, height = img_s.size
total_pixels= width * height


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

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

# create mask for green color in hsv
# green is 60 in opencv
lower = (40,100,100)
upper = (100,255,255)
mask = cv2.inRange(hsv, lower, upper)

# count non-zero pixels in mask
count=np.count_nonzero(mask)
count_z = np.sum(mask == 0)
print("Total pixels:", total_pixels)
print('green count:', count, 'green %:', '{0:.2f}%'.format((count/total_pixels * 100)))
print('white count:', count_z, 'white %', '{0:.2f}%'.format((count_z/total_pixels * 100)))

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

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

我需要为每一行而不是单个行扩展它。

最佳答案

我想你想要这样的东西。基本思想是使用 np.sum()axis=1 来计算每行中的非零元素(即绿色像素):

#!/usr/bin/env python3

import cv2
import numpy as np

# Load image and get dimensions
img = cv2.imread('EBlso.png')
h, w = img.shape[:2]
total_pixels = h * w

# Convert to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Create mask for green color in HSV
# Green is 60 in OpenCV
lower = (40,100,100)
upper = (100,255,255)
mask = cv2.inRange(hsv, lower, upper)
cv2.imwrite('mask.png', mask)

# Count non-zero pixels in mask
count = np.count_nonzero(mask)
count_z = total_pixels - count
print(f'Total pixels: {total_pixels}')
print(f'Green count: {count}, green %: {count*100/total_pixels:.2f}')
print(f'White count: {count_z}, white % {count_z*100/total_pixels:.2f}')

# Calculate row-wise stats
GreensPerRow = np.sum(mask>0, axis=1)

for row, greens in enumerate(GreensPerRow):
print(f'Row: {row}, nGreen: {greens}, pcntGreen: {greens*100/w:.2f}')

示例输出

Total pixels: 197100
Green count: 82942 green %: 42.08%
White count: 114158 white % 57.92%
Row: 0, nGreen: 0, pcntGreen: 0.00
Row: 1, nGreen: 0, pcntGreen: 0.00
Row: 2, nGreen: 0, pcntGreen: 0.00
Row: 3, nGreen: 0, pcntGreen: 0.00
Row: 4, nGreen: 0, pcntGreen: 0.00
Row: 5, nGreen: 0, pcntGreen: 0.00
Row: 6, nGreen: 0, pcntGreen: 0.00
Row: 7, nGreen: 0, pcntGreen: 0.00
Row: 8, nGreen: 0, pcntGreen: 0.00
Row: 9, nGreen: 0, pcntGreen: 0.00
Row: 10, nGreen: 0, pcntGreen: 0.00
Row: 11, nGreen: 0, pcntGreen: 0.00
Row: 12, nGreen: 0, pcntGreen: 0.00
Row: 13, nGreen: 0, pcntGreen: 0.00
Row: 14, nGreen: 386, pcntGreen: 71.48
Row: 15, nGreen: 387, pcntGreen: 71.67
...
...

请注意,这里没有真正需要使用 HSV 色彩空间,因为您的绿色 rgb(76,175,80) 很容易与白色 rgb(255,255,255) 区分开来,通过对红色或蓝色 channel 进行阈值处理,比如说 128。

请注意,我忽略了灰色像素,假设它们不是绿色,而是白色。如果该假设不正确,您可以轻松更正。

关于python - 每行中绿色和白色像素的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73800076/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com