gpt4 book ai didi

Python 3 : I am trying to find find all green pixels in an image by traversing all pixels using an np. 数组,但无法解决索引错误

转载 作者:行者123 更新时间:2023-11-28 22:16:27 27 4
gpt4 key购买 nike

我的代码目前包含加载图像,这是成功的,我认为与问题无关。

然后我继续将彩色图像转换为名为 rgb 的 np.array

    # convert image into array
rgb = np.array(img)
red = rgb[:,:,0]
green = rgb[:,:,1]
blue = rgb[:,:,2]

仔细检查我对这个数组的理解,如果这可能是问题的根源,它是一个数组,这样 rgb[x-coordinate, y-coordinate, color band] 的值在 0-255 之间红色、绿色或蓝色。

然后,我的想法是做一个嵌套的for循环来遍历我图像的所有像素(620px,400px)并根据绿色与蓝色和红色的比例对它们进行排序,以试图挑出较绿色的像素并设置所有其他人为黑色或 0。

for i in range(xsize):
for j in range(ysize):
color = rgb[i,j] <-- Index error occurs here
if(color[0] > 128):
if(color[1] < 128):
if(color[2] > 128):
rgb[i,j] = [0,0,0]

我在尝试运行时收到的错误如下:

IndexError:索引 400 超出尺寸为 400 的轴 0 的范围

我认为这可能与我给 i 和 j 的边界有关,所以我尝试只对图像的一小部分进行排序,但仍然遇到相同的错误。在这一点上,我什至不知道错误的根源是什么,更不用说解决方案了。

最佳答案

在直接回答您的问题时,y 轴在 numpy 数组中首先给出,然后是 x 轴,因此请交换您的指数。


不那么直接,您会发现 for 循环在 Python 中非常慢,您通常最好改用 numpy 向量化操作。此外,您通常会发现在 HSV colourspace 中更容易找到绿色阴影。 .

让我们从 HSL 色轮开始:

enter image description here

假设你想把所有的绿色变成黑色。所以,从那个维基百科页面,对应于绿色的色相是 120 度,这意味着你可以这样做:

#!/usr/local/bin/python3
import numpy as np
from PIL import Image

# Open image and make RGB and HSV versions
RGBim = Image.open("image.png").convert('RGB')
HSVim = RGBim.convert('HSV')

# Make numpy versions
RGBna = np.array(RGBim)
HSVna = np.array(HSVim)

# Extract Hue
H = HSVna[:,:,0]

# Find all green pixels, i.e. where 100 < Hue < 140
lo,hi = 100,140
# Rescale to 0-255, rather than 0-360 because we are using uint8
lo = int((lo * 255) / 360)
hi = int((hi * 255) / 360)
green = np.where((H>lo) & (H<hi))

# Make all green pixels black in original image
RGBna[green] = [0,0,0]

count = green[0].size
print("Pixels matched: {}".format(count))
Image.fromarray(RGBna).save('result.png')

给出:

enter image description here


这是一个稍微改进的版本,它保留了 alpha/透明度,并匹配红色像素以获得额外的乐趣:

#!/usr/local/bin/python3
import numpy as np
from PIL import Image

# Open image and make RGB and HSV versions
im = Image.open("image.png")

# Save Alpha if present, then remove
if 'A' in im.getbands():
savedAlpha = im.getchannel('A')
im = im.convert('RGB')

# Make HSV version
HSVim = im.convert('HSV')

# Make numpy versions
RGBna = np.array(im)
HSVna = np.array(HSVim)

# Extract Hue
H = HSVna[:,:,0]

# Find all red pixels, i.e. where 340 < Hue < 20
lo,hi = 340,20
# Rescale to 0-255, rather than 0-360 because we are using uint8
lo = int((lo * 255) / 360)
hi = int((hi * 255) / 360)
red = np.where((H>lo) | (H<hi))

# Make all red pixels black in original image
RGBna[red] = [0,0,0]

count = red[0].size
print("Pixels matched: {}".format(count))

result=Image.fromarray(RGBna)

# Replace Alpha if originally present
if savedAlpha is not None:
result.putalpha(savedAlpha)

result.save('result.png')

关键词:图像处理、PIL、Pillow、色调饱和度值、HSV、HSL、颜色范围、颜色范围、范围、素数。

enter image description here

关于Python 3 : I am trying to find find all green pixels in an image by traversing all pixels using an np. 数组,但无法解决索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52179821/

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