Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python AttributeError: 'module' object has no attribute 'DIST_L2'

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!

like image 807
Vartika Avatar asked Jun 04 '14 05:06

Vartika


People also ask

How do I fix Python module has no attribute?

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.

What does Object has no attribute mean in Python?

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.

What does it mean that list Object has no attribute?

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.

How does Python handle attribute errors?

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.


2 Answers

Should be rewritten as below:

(dist_transform, labels) = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5) 
like image 73
derjohng Avatar answered Oct 18 '22 18:10

derjohng


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.

like image 43
andreterron Avatar answered Oct 18 '22 19:10

andreterron