Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

python - What is the cv2.cv replacement in OpenCV3?

I'm using OpenCV3, and with the python bindings there is no cv2.cv module:

In [1]: import cv2

In [2]: from cv2 import cv
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-15a6578c139c> in <module>()
----> 1 from cv2 import cv

ImportError: cannot import name cv

However, I have some legacy code of the form:

hsv_im = cv2.cvtColor(image, cv2.cv.CV_BGR2HSV)

When running this, I get the error:

In [7]: hsv_im = cv2.cvtColor(image, cv2.cv.CV_BGR2HSV)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-e784072551f2> in <module>()
----> 1 hsv_im = cv2.cvtColor(image, cv2.cv.CV_BGR2HSV)

AttributeError: 'module' object has no attribute 'cv'

What is the equivalent of this code in OpenCV3?


Related questions:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From OpenCV 2.X OpenCV 3.0 a few things changed.

Specifically:

  • cv2.cv doesn't exists in OpenCV 3.0. Use simply cv2.
  • some defines changed, e.g. CV_BGR2HSV is now COLOR_BGR2HSV.

So you need to change this line:

hsv_im = cv2.cvtColor(image, cv2.cv.CV_BGR2HSV)

to:

hsv_im = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...