[英]OpenCV's RotatedRect has no attribute 'size' in Python3. How to work around this?
I found out that the .size.height
and .size.width
operators of OpenCV's RotatedRect class don't work in Python whereas they work in C++.我发现OpenCV 的 RotatedRect 类的.size.height
和.size.width
运算符在 Python 中不起作用,而在 C++ 中起作用。 Let me elaborate with a simplified code snippet:让我用一个简化的代码片段来详细说明:
cap = cv2.VideoCapture('video1.mp4')
filter = RandomClass(20)
while(cap.isOpened()):
ret, frame = cap.read() # Read a frame
res = filter.classMain(frame) # Process the frame
if (res == 0):
print('Success') # If processing completed, print Success
cap.release()
where the class definition is as follows:其中类定义如下:
import cv2
import numpy as np
class RandomClass:
def __inti__(self):
self.set_skip_first(True)
def get_skip_first(self):
return self.skip_first
def set_skip_first(self, value):
self.skip_first = value
def classMain(self, frame):
if not get_skip_first():
self.expand_minRect(100) # expand the minRect by 100 pixels
# use the expanded rectangle for some other processing here
else:
self.set_skip_first(False)
# create a mask with cv2.inRange
contour = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE, offset=(0,0))[1]
# iterate over each contour and find the index of the largest contour
self.minRect = cv2.minAreaRect(np.array(self.contours[self.largest_contour_index]))
# execute some other processing here
return 0
def expand_minRect(self, value):
self.minRect.size.height = self.minRect.size.height + value
self.minRect.size.width = self.minRect.size.width + value
The error I'm receiving is as follows.我收到的错误如下。 The exact lines work perfectly fine in the C++ version of the above code.确切的行在上述代码的 C++ 版本中工作得很好。
File "filename", line 106, in expand_minRect文件“文件名”,第 106 行,在 expand_minRect 中
self.minRect.size.height = self.minRect.size.height + value self.minRect.size.height = self.minRect.size.height + 值
AttributeError: 'tuple' object has no attribute 'size' AttributeError: 'tuple' 对象没有属性 'size'
I tried the following.我尝试了以下方法。 I was expecting the second printed value (of variable width2
) to be greater than the first printed value (of variable width1
) by value
.我期待第二印刷值(可变的width2
)为比所述第一印刷值(变量更大width1
通过) value
。
def expand_minRect(self, value):
_,(width1, height1),_ = self.minRect
print(width)
self.minRect[1][0] = self.minRect[1][0] + value
_,(width2,height2),_ = self.minRect
print(w)
However it didn't work as the variable type of self.minRect[1][0]
is Tuple and Tuples cannot be modified.但是它不起作用,因为self.minRect[1][0]
的变量类型是元组并且元组不能被修改。
File "filename", line 111, in expand_minRect文件“文件名”,第 111 行,在 expand_minRect 中
self.minRect 1 [0] = self.minRect 1 [0] + value self.minRect 1 [0] = self.minRect 1 [0] + 值
TypeError: 'tuple' object does not support item assignment类型错误:“元组”对象不支持项目分配
I did some research, I couldn't find a Python documentation for RotatedRect but I found a stackoverflow answer stating that我做了一些研究,我找不到 RotatedRect 的 Python 文档,但我找到了一个stackoverflow 的答案,指出
Python still lacks of RotatedRect class Python 仍然缺少 RotatedRect 类
So all things to a side, assuming that the RotatedRect support in Python3 is incomplete, how can I work around this and expand the width and height of my minRect
variable?因此,所有事情都放在一边,假设 Python3 中的 RotatedRect 支持不完整,我该如何解决这个问题并扩展minRect
变量的宽度和高度?
According to this tutorial , minAreaRect
returns a rectangle with ((center_x,center_y),(width,height),angle)
.根据本教程, minAreaRect
返回一个带有((center_x,center_y),(width,height),angle)
的矩形。 Thus, if you modify your expand_minRect
to recreate it with the correct components, it should work.因此,如果您修改expand_minRect
以使用正确的组件重新创建它,它应该可以工作。
def expand_minRect(self, value):
self.minRect = (self.minRect[0], # keep the center
(self.minRect[1][0] + value, self.minRect[1][1] + value), # update the size
self.minRect[2]) # keep the angle
Note: The problem emerges from the fact that OpenCV python has a less object-oriented implementation than OpenCV c++.注意:问题源于 OpenCV python 的面向对象实现比 OpenCV c++ 少。 It does not return a struct which enables accessing each attribute by name (like " .size
").它不返回允许按名称访问每个属性的结构(如“ .size
”)。 You need to know the tuple order and assumptions for each element.您需要知道每个元素的元组顺序和假设。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.