I have found an example of MSER in Python OpenCV. When I try to run it I get an error when it tries to reshape a list
/numpy.array
. The error is:
AttributeError: 'list' object has no attribute 'reshape'
How can I fix this issue? In the below simple code I have commented where the error occurs:
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()
I think the mser.detectRegions(img)
has evolved a bit over various release versions. In my case I have OpenCV version:
import cv2
print cv2.__version__
>>> 3.3.0
And the mser.detectRegions(img)
is returning a tupe with two values instead of a single return value. You can fix this by ignoring the second value of tuple:
regions, _ = mser.detectRegions(img)
I have raised the point of version because a lot of examples available online use regions = mser.detectRegions(img)
. Which may lead to confusion.
As of now I am not sure of the version which caused this ambiguity, so I am suggesting a quick fix to your problem.