import opencv vs import cv2
Question:
I recently installed opencv-python-3.4.0.12 via pip install on Mac OS. When I run the Python interpreter, import cv2
works fine whereas import opencv
raises ModuleNotFoundError. However, when I run the Python3 Interpreter, import opencv
works fine whereas import cv2
raises ModuleNotFoundError.
What is the reason behind this difference and on a related note, should I use import opencv
or import cv2
? Does cv2
refer to OpenCV version 2?
Answers:
You should import cv2
. OpenCV releases two types of Python interfaces, cv
and cv2
. latest one is cv2
. This will give you an idea whether you have installed opencv
correctly.
The naming of cv2
is a historical accident.
History lesson: OpenCV began as a C API. The first Python bindings back then used the cv
import. With OpenCV version 2.0, a C++ API was introduced. That is also when C++ includes saying #include <opencv2/...>
showed up. The C++ API uses the cv::
namespace. The Python import was now called cv2
, and carried the old C API in the cv2.cv
namespace.
The 2
basically just means "The new API".
Since then, versions 3.x and 4.x, "the C++ API" is still the primary API, and currently the only API, because the C API was removed. However, the location of headers has not changed with new major versions and the name of the Python module import also has not changed.
The opencv-python
packages only provide the cv2
import. That is the import for all v3.x and 4.x versions, i.e. the current version, and probably will carry into v5.x. The recommended import and usage is:
import cv2 as cv # to mirror the `cv::` namespace
# use cv.imread() and so on
There is no opencv
import. If that "worked", it’s wrong and something is tricking you. Check the installed packages and remove anything shady. The only trustworthy packages giving you OpenCV in Python are the official ones, which is opencv-python
on PyPI and its sibling packages (package with additional contrib modules, package excluding GUI modules).
There is no cv
import. In the future, these packages may be changed to drop the old name and offer the cv
import directly.
I recently installed opencv-python-3.4.0.12 via pip install on Mac OS. When I run the Python interpreter, import cv2
works fine whereas import opencv
raises ModuleNotFoundError. However, when I run the Python3 Interpreter, import opencv
works fine whereas import cv2
raises ModuleNotFoundError.
What is the reason behind this difference and on a related note, should I use import opencv
or import cv2
? Does cv2
refer to OpenCV version 2?
You should import cv2
. OpenCV releases two types of Python interfaces, cv
and cv2
. latest one is cv2
. This will give you an idea whether you have installed opencv
correctly.
The naming of cv2
is a historical accident.
History lesson: OpenCV began as a C API. The first Python bindings back then used the cv
import. With OpenCV version 2.0, a C++ API was introduced. That is also when C++ includes saying #include <opencv2/...>
showed up. The C++ API uses the cv::
namespace. The Python import was now called cv2
, and carried the old C API in the cv2.cv
namespace.
The 2
basically just means "The new API".
Since then, versions 3.x and 4.x, "the C++ API" is still the primary API, and currently the only API, because the C API was removed. However, the location of headers has not changed with new major versions and the name of the Python module import also has not changed.
The opencv-python
packages only provide the cv2
import. That is the import for all v3.x and 4.x versions, i.e. the current version, and probably will carry into v5.x. The recommended import and usage is:
import cv2 as cv # to mirror the `cv::` namespace
# use cv.imread() and so on
There is no opencv
import. If that "worked", it’s wrong and something is tricking you. Check the installed packages and remove anything shady. The only trustworthy packages giving you OpenCV in Python are the official ones, which is opencv-python
on PyPI and its sibling packages (package with additional contrib modules, package excluding GUI modules).
There is no cv
import. In the future, these packages may be changed to drop the old name and offer the cv
import directly.