对象变量未重置

2024-04-19 19:17:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用一个管道创建一个视觉跟踪程序。在我的管道中,我有以下代码:

@staticmethod
def __approx_contours(input_contours):
    output = []
    kp = None
    for contour in input_contours:
        error = 0.1*cv2.arcLength(contour, True)
        approx = cv2.approxPolyDP(contour, error, True)
        print(approx)
        kp = Keeper(approx)
        print(kp)
    if kp == None:
        return output
    for x,y in zip(kp.getX(),kp.getY()):
        output.append((x,y))
    kp.empty()
    return output

这里是守门员班

^{pr2}$

忽略__init__中的print语句,它们仅用于调试目的。在

当前状态下的输出示例:

[[[183 169]]

 [[187 323]]]
Keeper
[183, 187]
[169, 323]
<keeper.Keeper object at 0x05199630>

[[[ 62 117]]

 [[ 93 366]]

 [[187 256]]]
Keeper
[183, 187, 62, 93, 187]
[169, 323, 117, 366, 256]
<keeper.Keeper object at 0x06F10B70>

似乎我的Keeper对象中的值没有被重置,尽管调用了kp.空(). 我还注意到Keeper对象在内存中的位置正在改变,也许这是问题的一部分,但我不确定我哪里出错了。完整代码可用here


Tags: 代码innoneforinputoutput管道error
1条回答
网友
1楼 · 发布于 2024-04-19 19:17:59

问题是作为用户@胡安帕.阿里维拉加我使用的是类属性而不是实例属性。要解决我的问题,需要对Keeper进行以下更改:

class Keeper:

    # constructor, requires a 3-D array
    def __init__(self, third_dim):
        self.x = []
        self.y = []
        if third_dim.ndim == 3:
            for row in third_dim:
                for col in row:
                    self.x.append(col[0])
                    self.y.append(col[1])

相关问题 更多 >