报错信息:
【Error】 IndexError: index 300 is out of bounds for axis 0 with size 300
问题描述
遍历300*300的图片,在遍历到边界i=299
或者j=299
的时候,存在i+1
或者j+1
的行为,导致数组越界。
for i in range(h):
for j in range(w):
if gbin[i][j] == 0 and img[i][j] < h_thresh*255
and img[i][j] >= l_thresh*255:
if gbin[i-1][j-1] or gbin[i-1][j] or gbin[i-1][j+1]
or gbin[i][j-1] or gbin[i][j+1] or gbin[i+1][j-1]
or gbin[i+1][j] or gbin[i+1][j+1]:
gbin[i][j] = 255
解决方案:
边界情况特殊考虑,遍历图片时需要特别注意。