Python 需要找到一种计算G像素百分比的方法
我需要为一个项目计算给定图片中绿色像素的数量 我已经找到了生成图像绿色部分的方法。只需要找到一种方法来计算给定图像的绿色百分比。如何对图像目录进行循环 这是我收集的代码。请帮助我获取选定饱和区域百分比中的绿色像素Python 需要找到一种计算G像素百分比的方法,python,image-processing,python-imaging-library,hsv,Python,Image Processing,Python Imaging Library,Hsv,我需要为一个项目计算给定图片中绿色像素的数量 我已经找到了生成图像绿色部分的方法。只需要找到一种方法来计算给定图像的绿色百分比。如何对图像目录进行循环 这是我收集的代码。请帮助我获取选定饱和区域百分比中的绿色像素 import cv2 import numpy as np import matplotlib.pyplot as plt from PIL import Image import numpy as np img = cv2.imread('
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
img = cv2.imread('Image')
grid_RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(20,8))
dimensions = img.shape
# height, width, number of channels in image
height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]
print('Image Dimension : ',dimensions)
print('Image Height : ',height)
print('Image Width : ',width)
print('Number of Channels : ',channels)
# area is calculated as “height x width”
area = height * width
# display the area
print("Area of the image is : ", area)
plt.imshow(grid_RGB) # Printing the original picture after converting to RGB
grid_HSV = cv2.cvtColor(grid_RGB, cv2.COLOR_RGB2HSV) # Converting to HSV
lower_green = np.array([25,52,72])
upper_green = np.array([102,255,255])
mask= cv2.inRange(grid_HSV, lower_green, upper_green)
res = cv2.bitwise_and(img, img, mask=mask) # Generating image with the green part
print("Green Part of Image")
plt.figure(figsize=(20,8))
plt.imshow(res)
# Load image and convert to HSV
im = Image.open('.image').convert('HSV')
# Extract Hue channel and make Numpy array for fast processing
Hue = np.array(im.getchannel('H'))
# Make mask of zeroes in which we will set greens to 1
mask = np.zeros_like(Hue, dtype=np.uint8)
# Set all green pixels to 1
mask[(Hue>80) & (Hue<90)] = 1
print(mask.mean()/100 * area/100)
# Now print percentage of green pixels
print((mask.mean()*100))
print((mask.mean()*mask.size)/100)
导入cv2
将numpy作为np导入
将matplotlib.pyplot作为plt导入
从PIL导入图像
将numpy作为np导入
img=cv2.imread('Image')
grid_RGB=cv2.CVT颜色(img,cv2.COLOR_BGR2RGB)
plt.图(figsize=(20,8))
尺寸=img.shape
#图像中的高度、宽度、通道数
高度=img.形状[0]
宽度=img.形状[1]
通道=图像形状[2]
打印('图像尺寸:',尺寸)
打印('图像高度:',高度)
打印('图像宽度:',宽度)
打印('通道数:',通道数)
#面积计算为“高度x宽度”
面积=高度*宽度
#显示区域
打印(“图像区域为:”,区域)
plt.imshow(grid_RGB)#转换为RGB后打印原始图片
grid_HSV=cv2.CVT颜色(grid_RGB,cv2.COLOR_RGB2HSV)#转换为HSV
下_green=np.数组([25,52,72])
上_green=np.数组([102255255])
遮罩=cv2.inRange(栅格、下绿色、上绿色)
res=cv2.按位_和(img,img,mask=mask)#生成带有绿色部分的图像
打印(“图像的绿色部分”)
plt.图(figsize=(20,8))
plt.imshow(res)
#加载图像并转换为HSV
im=Image.open('.Image').convert('HSV'))
#提取色调通道并制作Numpy阵列以实现快速处理
色调=np.array(im.getchannel('H'))
#制作零掩码,在其中我们将绿色设置为1
掩码=np.zeros_like(色调,dtype=np.uint8)
#将所有绿色像素设置为1
mask[(Hue>80)&(Hue下面的方法不是很精确,但是它做得很好。你可以找到完整的源代码
green\u pixel\u count=0
总像素=1024#一些虚拟值
#查找每个扇区的绿色像素计数
对于img中的i:
对于i中的j:
如果(j[1]>j[0]和j[1]>j[2]和j[0]只是一个问题,我如何使用你的代码来计算饱和图片?正如我所说,它不是100%准确。我使用这种方法来计算卫星图像的绿化百分比。饱和度变化不大。因此它工作得很好。
green_pixel_count = 0
TOTAL_PIXELS = 1024 # Some dummy value
# Find green pixel count for each sector
for i in img:
for j in i:
if(j[1]>j[0] and j[1]>j[2] and j[0]<100 and j[2]<100):
green_pixel_count += 1
greenery_percentage = green_pixel_count*100/TOTAL_PIXELS