对象变量未重置(Object variables not resetting)

编程入门 行业动态 更新时间:2024-10-23 22:35:15
对象变量未重置(Object variables not resetting)

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

@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

这是Keeper课程

class Keeper: _x = [] _y = [] # constructor, requires a 3-D array def __init__(self, third_dim): if third_dim.ndim == 3: for row in third_dim: for col in row: self._x.append(col[0]) self._y.append(col[1]) else: raise NotThirdDimension("Entered array was not three dimensional") print(self._x) print(self._y) # return object "X" array def getX(self): return self._x # return object "Y" array def getY(self): return self._y def empty(self): self._x = [] self._y = []

忽略__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.empty()。 我还注意到Keeper对象正在改变内存中的位置,也许这​​是问题的一部分,但我不确定我哪里出错了。 完整代码可在此处获得

I'm working on creating a vision tracking program using a pipeline. In my pipeline I have the following code:

@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

and here is Keeper class

class Keeper: _x = [] _y = [] # constructor, requires a 3-D array def __init__(self, third_dim): if third_dim.ndim == 3: for row in third_dim: for col in row: self._x.append(col[0]) self._y.append(col[1]) else: raise NotThirdDimension("Entered array was not three dimensional") print(self._x) print(self._y) # return object "X" array def getX(self): return self._x # return object "Y" array def getY(self): return self._y def empty(self): self._x = [] self._y = []

Disregard the print statements in __init__, they are only for debugging purposes.

Example output in current state:

[[[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>

It appears that the values in my Keeper object are not being reset, despite calling kp.empty(). I've also noted that the Keeper object is changing place in memory, perhaps this is part of the issue but I'm not sure where I am going wrong. Full code is available here

最满意答案

问题是用户@ juanpa.arrivillaga说,我使用的是class属性而不是实例属性。 为解决我的问题,需要对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])

The issue was as user @juanpa.arrivillaga said, I was using class attributes instead of instance attributes. To remedy my issue the following changes needed to be made to 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])

更多推荐

本文发布于:2023-07-14 20:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107288.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   对象   Object   variables   resetting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!