【问题标题】:Trace an image in python在 python 中跟踪图像
【发布时间】:2017-12-24 12:40:23
【问题描述】:

我编写了这个 python 脚本来跟踪图像。但它抛出了一个错误。它显示 “IndexError: index 181 is out of bounds for axis 0 with size 181” 我的图像尺寸为 181x158。为了纠正这个错误,我减小了范围,但是没有用。

import cv2
import numpy as np
global p
a = cv2.imread('t.png',0);
b = (255 -a);
c = np.asarray(b);
p = np.count_nonzero(c)
[ay , ax] = c.shape;
z = np.zeros(c.shape, dtype=np.int)

def startTrace(yt,xt):
    global p
    p = p-1
    z[yt,xt] = 255;
    c[yt,xt] =0;
    if (c[yt, xt+1] > 0):
        startTrace(yt,xt+1)
    elif (c[yt+1,xt+1] > 0):
        startTrace(yt+1,xt+1)
    elif (c[yt+1,xt] > 0):
        startTrace(yt+1,xt)
    elif (c[yt+1,xt-1] >0) :
        startTrace(yt+1,xt-1)
    elif (c[yt,xt-1] >0):
        startTrace(yt,xt-1)
    elif (c[yt-1,xt-1] > 0):
        startTrace(yt-1,xt-1)
    elif (c[yt-1,xt] > 0):
        startTrace(yt-1,xt)
    elif (c[yt-1,xt+1] > 0):
        startTrace(yt-1,xt+1)


while (p > 0):
    for y in range(1,ay-2):
        for x in range(1,ax-2):
            if c[y,x] > 0 :
                startTrace(y,x);

【问题讨论】:

  • python 列表从零开始,所以如果 alist 的长度为 5,则第一项是 alist[0],最后一项是 alist[4]
  • 尝试在startTrace 中限制ytxt 的范围。它们可能会超出图像的大小,例如axay
  • 跟踪是什么意思?此外,您的 startTrace 没有基本条件,递归可能会无限运行。
  • @ZdaR 或由于循环增加而崩溃,没有任何限制。
  • @ZdaR 此代码用于跟踪二进制图像

标签: python numpy opencv image-processing


【解决方案1】:

请注意,您的代码是递归的(startTrace 会调用自己),您不知道它会调用自己多少次。事实上,你能保证对 startTrace() 的一次调用将永远退出吗? startTrace() 可以永远调用 startTrace() 吗?这最终会导致堆栈溢出。但这还不是你的问题。

代码失败是因为每次调用 startTrace 的参数 (+1, -1) 与最初调用 startTrace() 的参数不同。即使在“while”内调用确保您没有越界,如果递归调用 startTrace(),每个新调用可能具有原始参数 +1,最终将增长到越界(没有检查在 startTrace() 中,参数在图像的范围内)。您应该在函数的开头添加一个 if 来检查 xt 和 yt 是否在图像的边界内。

无论如何,我建议在 OpenCV 中搜索一种可以满足您需求的方法。例如,看看 findContours。

【讨论】:

    【解决方案2】:

    感谢大家的帮助。 此代码工作正常。

    import cv2
    import numpy as np
    
    
    global p
    a = cv2.imread('t.png',0);
    [ty,tx] = a.shape;
    o = np.zeros((ty+2,tx+2),dtype=np.int)
    o[1:ty+1,1:tx+1] = (255 -a);
    c = np.asarray(o);
    p = np.count_nonzero(c)
    [ay , ax] = c.shape;
    z = np.zeros(c.shape, dtype=np.int)
    
    def startTrace(yt,xt):
        global p
        cv2.imshow('image',z);
        z[yt,xt] = 255;
        c[yt,xt] =0;
        p = np.count_nonzero(c)
        if((yt < ay-1) and (xt < ax -1)): 
            if (c[yt, xt+1] > 0):
                startTrace(yt,xt+1)
            elif (c[yt+1,xt+1] > 0):
                startTrace(yt+1,xt+1)
            elif (c[yt+1,xt] > 0):
                startTrace(yt+1,xt)
            elif (c[yt+1,xt-1] >0) :
                startTrace(yt+1,xt-1)
            elif (c[yt,xt-1] >0):
                startTrace(yt,xt-1)
            elif (c[yt-1,xt-1] > 0):
                startTrace(yt-1,xt-1)
            elif (c[yt-1,xt] > 0):
                startTrace(yt-1,xt)
            elif (c[yt-1,xt+1] > 0):
                startTrace(yt-1,xt+1)
    
    
    while (p > 0):
        for y in range(1,ay-2):
            for x in range(1,ax-2):
                if (c[y,x] > 0) :
                    startTrace(y,x);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2019-09-22
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多