How to install OpenCV in Python?

OpenCV is an open-source Python library, which used to understand the content of the digital image. The CV is the abbreviation form of computer vision. It extracts the description from the real-time image or digital image, which may be an object, a text description, and so on.

We can perform many tasks using the OpenCV library such as face detection, face recognition, blob detection, edge-detection, image filter, template matching, and etc. To work with the OpenCV, we need to install it in our Python environment.

Installation of OpenCV

Here, we install the OpenCV using the following ways.

  • Using pip command
  • Using Anaconda

Using pip command

Open a command prompt and type the following command.

pip install opencv-contrib-python --upgrade

Or, we can install it without extra module by the following command.

pip install opencv-python

Now, we will verify the proper installation of OpenCV. Import the cv2 module and print its version.

If it has properly installed, then it will show its version.

Using Anaconda

Anaconda is a software package of Python. Anaconda with Jupyter is a the best way to work with the OpenCV. First, we need to install the Anaconda graphics installer from its official site.

Now, choose a suitable bit installer.

Once download is completed. Open the Anaconda prompt and type the following command.

conda install -c conda-forge opencv

Then, hit the enter button and it will download all rated OpenCV configuration.

Now, we are ready to work with the OpenCV.

Well, for importing the opencv first you need to follow these steps:-

  • Install all packages related to opencv like numpy and all into their default locations. Python will be installed to C:/Python27/.
  • After installation, open Python IDLE. Enter import numpy and make sure Numpy is working fine.
  • Download latest OpenCV release.
  • Goto OpenCV/build/python/2.7 folder.
  • Copy cv2.pyd to C:/Python27/lib/site-packages
  • Then open the terminal and simply type:-

import cv2
print cv2.version