索引错误:索引 1431 超出轴 0 的范围,大小为 741

问题描述

我正在定义一个具有容差的区域增长函数。我在图像尺寸内限制 x 和 y。 当我使用 x 或 y 时,代码有效。 但是当我同时使用 x 和 y 时,代码会产生一个错误

"IndexError: index 1431 is out of bounds for axis 0 with size 741"

此外,输出图像(使用 x 或 y 时)会生成具有互换轴 (773 x 741) 的图像。图片尺寸为 741 x 773。

图片

    https://drive.google.com/file/d/1wpUAAdU8DeJjp0TLY19X1FJ-xW2-JEma/view?usp=sharing

你能指出我哪里出错了吗?

函数代码:region_GRING_average

def distance(a,b):
    return np.abs(int(a)-int(b))                       

#for a two-dimentional image and a single marker [x,y] position
def region_growing_average(img,img_t,tolerance,marker,m=0):
    
    # include this statement before applying region growing: img_t = np.zeros(img.shape)
    x = marker[0]
    y = marker[1]
    if (x+1 < img.shape[0] and x-1 >=0 and y+1 < img.shape[1] and y-1 >=0):
        img_t[x,y] = 1
        
        # check matrix border and conquering criterion for the 4-neigbourhood
        if (x+1 < img_t.shape[0] and img_t[x+1,y] == 0  and distance(img[x-m,y],img[x+1,y]) <= tolerance):
            region_growing_average(img,[x+1,m+1)
            
        if (x-1 >= 0 and img_t[x-1,y] == 0  and distance(img[x+m,img[x-1,y]) <= tolerance):
            print(x,y,m,x+m,img[x+m,img[x,y+1],distance(img[x+m,y]))
            region_growing_average(img,[x-1,m+1)
            
        if (y+1 < img_t.shape[1] and img_t[x,y+1] == 0 and distance(img[x,y-m],y+1]) <= tolerance):
            region_growing_average(img,[x,m+1)
            
        if (y-1 >= 0 and img_t[x,y-1] == 0  and distance(img[x,y+m],y-1],m+1)
        
    #It will return a binary image: img_t
img = im[:,:,2]  ##trying only one band
img_t = np.zeros(img.shape)
marker = [50,250]
region_growing_average(img,257,marker)  #it should generate a white image

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)