我正在定义一个具有容差的区域增长函数。我将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
你能告诉我哪里出了错吗?
函数代码:区域\增长\平均值
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, img_t, tolerance, [x+1, y], m+1)
if (x-1 >= 0 and img_t[x-1,y] == 0 and distance(img[x+m,y], img[x-1, y]) <= tolerance):
print(x,y, m, x+m, img[x+m,y], img[x, y+1], distance(img[x+m,y], img[x-1, y]))
region_growing_average(img, img_t, tolerance, [x-1, y], m+1)
if (y+1 < img_t.shape[1] and img_t[x,y+1] == 0 and distance(img[x,y-m], img[x, y+1]) <= tolerance):
region_growing_average(img, img_t, tolerance, [x, y+1], m+1)
if (y-1 >= 0 and img_t[x,y-1] == 0 and distance(img[x,y+m], img[x, y+1]) <= tolerance):
region_growing_average(img, img_t, tolerance, [x, 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, img_t, 257, marker) #it should generate a white image
暂无答案!
目前还没有任何答案,快来回答吧!