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!

3
  • Google didn't turn up anything called DIST_LT2. Are you sure the thing you're looking for exists and has this name? Commented Jun 4, 2014 at 5:09
  • 2
    I'm not very familiar with OpenCV, but are you sure you didn't mean: cv2.DIST_L2 instead of cv2.DIST_LT2? Commented Jun 4, 2014 at 5:09
  • really sorry about it.. Yes it is cv2.DIST_L2 but the error is same, its just a typing error Commented Jun 6, 2014 at 19:35

5 Answers 5

24

Should be rewritten as below:

(dist_transform, labels) = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5) 
Sign up to request clarification or add additional context in comments.

1 Comment

I got only one ret value though, so I had to write dist_ransform = cv2.distanceTransform(opening2,cv2.cv.CV_DIST_L2,5)
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.

4 Comments

its still not working. but why is this problem coming anyway? DIST_L2 is there in opencv
@Vartika Are you getting the same error? or is the resulting image just different than the expected?
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 cvDistTransform
BTW, distanceTransform will return 'tuple', not only 'dist_transform'. Like, (dist_transform, labels) = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5)
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)

Comments

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

Comments

0

cv2.cv.CV_DIST_L2 works as a replacement

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.