Python AttributeError: 'module' object has no attribute 'DIST_L2'
Asked Answered
P

5

14

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!

Permeable answered 4/6, 2014 at 5:4 Comment(3)
Google didn't turn up anything called DIST_LT2. Are you sure the thing you're looking for exists and has this name?Adeleadelheid
I'm not very familiar with OpenCV, but are you sure you didn't mean: cv2.DIST_L2 instead of cv2.DIST_LT2?Dry
really sorry about it.. Yes it is cv2.DIST_L2 but the error is same, its just a typing errorPermeable
C
24

Should be rewritten as below:

(dist_transform, labels) = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5) 
Conventionalize answered 3/7, 2014 at 12:17 Comment(1)
I got only one ret value though, so I had to write dist_ransform = cv2.distanceTransform(opening2,cv2.cv.CV_DIST_L2,5)Macnamara
M
15

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.

Marceline answered 10/6, 2014 at 8:6 Comment(4)
its still not working. but why is this problem coming anyway? DIST_L2 is there in opencvPermeable
@Vartika Are you getting the same error? or is the resulting image just different than the expected?Marceline
nope, not the same Error but I am getting no Image. The error is - File "vpython/segment3.py", line 32, in <module> dist_transform = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5) cv2.error: /builddir/build/BUILD/opencv-2.4.7/modules/imgproc/src/distransform.cpp:723: error: (-210) source image must be 8uC1 and the distance map must be 32fC1 (or 8uC1 in case of simple L1 distance transform) in function cvDistTransformPermeable
BTW, distanceTransform will return 'tuple', not only 'dist_transform'. Like, (dist_transform, labels) = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5)Conventionalize
H
11

This is a late reply, but in order to get through the tutorial you are doing, you really need to install openCV 3.0. Then the syntax in the tutorial is correct.

For openCV 3.0:

dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)

For openCV 2.x:

dist_transform = cv2.distanceTransform(opening, cv2.cv.CV_DIST_L2, 5)
Hallett answered 12/2, 2015 at 2:30 Comment(0)
U
3

The next bug you will run into in completing the tutorial is cv2.connectedComponents not being available. See OpenCV for Python - AttributeError: 'module' object has no attribute 'connectedComponents'.

The trick is to install opencv3, which can easily be done with Anaconda using

conda install -c https://conda.binstar.org/menpo opencv3
Ullman answered 19/10, 2015 at 22:51 Comment(0)
I
0

cv2.cv.CV_DIST_L2 works as a replacement

Intemperate answered 22/4, 2017 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.