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!

Francisco
  • 10,918
  • 6
  • 34
  • 45
Vartika Sharma
  • 269
  • 1
  • 3
  • 10

5 Answers5

24

Should be rewritten as below:

(dist_transform, labels) = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5) 
derjohng
  • 877
  • 2
  • 9
  • 18
  • 2
    I got only one ret value though, so I had to write dist_ransform = cv2.distanceTransform(opening2,cv2.cv.CV_DIST_L2,5) – N4ppeL Jun 29 '16 at 15:17
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.

andreterron
  • 184
  • 3
  • its still not working. but why is this problem coming anyway? DIST_L2 is there in opencv – Vartika Sharma Jun 12 '14 at 11:34
  • @Vartika Are you getting the same error? or is the resulting image just different than the expected? – andreterron Jun 13 '14 at 19:09
  • nope, not the same Error but I am getting no Image. The error is - File "vpython/segment3.py", line 32, in 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 – Vartika Sharma Jun 14 '14 at 14:40
  • 1
    BTW, distanceTransform will return 'tuple', not only 'dist_transform'. Like, (dist_transform, labels) = cv2.distanceTransform(opening,cv2.cv.CV_DIST_L2,5) – derjohng Jul 03 '14 at 12:15
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)
lynvie
  • 1,028
  • 1
  • 14
  • 25
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
Community
  • 1
  • 1
Bob Baxley
  • 3,551
  • 1
  • 22
  • 28
0

cv2.cv.CV_DIST_L2 works as a replacement