I am trying to use cv2.distanceTransform()
method in Python. And I am getting an error when running the following line of code:
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
I get the following error when running this code:
AttributeError: 'module' object has no attribute 'DIST_L2'
Similar questions have been asked before, and i know that this problem occurs when you import 'something' when your python file name is 'something.py'. However, my python file name is segment3.py.
Can anyone please help me with this? I am trying to do segmentation using watershed algorithm. I am working on Fedora20. Thanks in advance!
To solve the Python "AttributeError: module has no attribute", make sure you haven't named your local modules with names of remote modules, e.g. datetime.py or requests.py and remove any circular dependencies in import statements.
It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling.
The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.
Solution for AttributeError Errors and exceptions in Python can be handled using exception handling i.e. by using try and except in Python. Example: Consider the above class example, we want to do something else rather than printing the traceback Whenever an AttributeError is raised.
Should be rewritten as below:
(dist_transform, labels) = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5)
Instead of cv2.DIST_L2
, use:
cv2.cv.CV_DIST_L2
I was having the same problem, but after some research, the documentation mention an example file on the source code (opencv_source/samples/python2/distrans.py) which uses this constant instead. I tested here and it worked as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With