我的代码目前包括加载图像,这是成功的,我认为与问题没有任何联系。
然后我继续将彩色图像转换为名为 rgb 的 np.array
# convert image into array
rgb = np.array(img)
red = rgb[:,:,0]
green = rgb[:,:,1]
blue = rgb[:,:,2]
Run Code Online (Sandbox Code Playgroud)
为了仔细检查我对这个数组的理解,以防这可能是问题的根源,它是一个数组,使得 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]
Run Code Online (Sandbox Code Playgroud)
我在尝试运行时收到的错误如下:
索引错误:索引 400 超出轴 0 的范围,大小为 400
我认为这可能与我给 i 和 j 的边界有关,所以我尝试只对图像的一小部分内部进行排序,但仍然出现相同的错误。在这一点上,我什至不知道错误的根源是什么,更不用说解决方案了。