开发者

Difference between "import cv" and "import opencv.cv" using Python + OpenCV?

开发者 https://www.devze.com 2023-01-28 15:52 出处:网络
I\'m trying to use OpenCV with Python and converting some C++ code. Anyway, if I do: import cv img = cv.LoadImage(\'image.jpg\')

I'm trying to use OpenCV with Python and converting some C++ code. Anyway, if I do:

import cv
img = cv.LoadImage('image.jpg')

It's开发者_JAVA技巧 ok. Or:

import opencv.cv as opcv
size = opcv.cvSize(40, 50)

But anyway, the cv module doesn't have the cvSize data structure and the opencv.cv doesn't have the LoadImage. So, what exactly does each module have? I tried looking in the documentation but couldn't find it. Am I supposed to use it like this or is my setup misconfigured?


The real answer is :) that both "import opencv.cv" or "from opencv import cv" are the old-style wrapping imports.

Since OpenCV 2.0, the new-style Python wrapping is used, and the style you should use looks like this:

# import only cv, no opencv
# this also brings in sub modules such as highgui
import cv
# no "cv" prepended before all method names
src_mat = cv.LoadImageM('yourfilename.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
# let's show the image in a window
cv.NamedWindow('your name', 1)
cv.ShowImage('your name', src_mat)
cv.WaitKey

The old-style wrappings made use of SWIG, the new-style wrappings, judging by the opencv 2.2 source code, seem to be self-made.


With:

import cv 

You're importing the cv module from wherever it exists in the python modules search directories. This could be a different version of the module stored somewhere outside the opencv package install as it appears to be in this case.

But with:

import opencv.cv

You're explicitly importing the opencv packages version of cv, i.e. the one in the install directory for the opencv package. This version almost guarantees you have the one from the opencv package and it seems to be the same as using the syntax:

from opencv import cv
0

精彩评论

暂无评论...
验证码 换一张
取 消