简体   繁体   中英

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!

应改写如下:

(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.

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)

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

cv2.cv.CV_DIST_L2作为替代品

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM