How to install OpenCV for Python on windows

OpenCV or open computer vision is an incredible library for doing everything from face detection, face recognition, and object recognition to edge detection and image manipulation, and much more. In this guide, I will help you to get OpenCV installed on your workstation running Windows

Prerequisites:

  1. A working windows operating system.

  2. You must have Python installed ( I recommend python3 ) for python 2 you can refer to this guide

So now that you already have python Let’s get started with the installation process.

  1. Install via Pip

What is pip?

pip is a package management system used to install and manage software packages written in Python.

Step 1: Open Command Prompt or Windows Powershell

Step 2: Type in the following commands

pip install opencv-contrib-python --upgrade

2. Test OpenCV Installation

In the command prompt or any text editor you like write down the following code and if the installation process is successful everything should run fine.

import cv2

print(cv2.__version__)

and executing the above code will give you the version of OpenCV you are running on.

3. Running your first program in OpenCV

Web Camera Quick Test

step 1. Create a new file camera-test.py (Any name you like but should end with .py extension)

import cv2 cap = cv2.VideoCapture(0)while True:
   # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',frame)
    cv2.imshow('gray',gray)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Save it and execute it by

c:\>python camera-test.py

Executing the above code will open two windows in which you could see 1 image in grayscale and 1 image in normal color mode.

To quit the app just hit the key ‘q’ on the video window to stop the camera.