0

我在 Python OpenCV 中找到了一个 MSER 示例。当我尝试运行它时,当它尝试重塑list/时出现错误numpy.array。错误是:

AttributeError:“列表”对象没有属性“重塑”

我该如何解决这个问题?在下面的简单代码中,我注释了错误发生的位置:

import cv2
import numpy as np

img = cv2.imread('../images/Capture2.JPG', 0);
vis = img.copy()
mser = cv2.MSER_create()
regions = mser.detectRegions(img)

hulls = []
for p in regions:
    # Error on below line: 'AttributeError: 'list' object has no attribute 'reshape''
    hulls.append( cv2.convexHull(p.reshape(-1, 1, 2)) )

    # Note a numpy array isn't working either: error: 'ValueError: cannot reshape array of size 2605 into shape (1,2)'
    p = np.array(p)
    hulls.append( cv2.convexHull(p.reshape(-1, 1, 2)) ) 

cv2.polylines(vis, hulls, 1, (0, 255, 0))
cv2.imshow('img', vis)
cv2.waitKey(0)
cv2.destroyAllWindows()
4

1 回答 1

1

我认为它mser.detectRegions(img)已经在各种发行版本中有所发展。就我而言,我有 OpenCV 版本:

import cv2
print cv2.__version__
>>> 3.3.0

并且mser.detectRegions(img)返回一个具有两个值而不是单个返回值的元组。您可以通过忽略元组的第二个值来解决此问题:

regions, _ = mser.detectRegions(img)

我提出了版本的观点,因为网上有很多例子可供使用regions = mser.detectRegions(img)。这可能会导致混乱。

截至目前,我不确定导致这种歧义的版本,所以我建议快速解决您的问题。

于 2018-07-10T10:58:05.047 回答