top of page
  • ishangala16

OpenCV Basics Python

What is OpenCV?

OpenCV is an open-source library mainly for real-time computer vision. OpenCV supports a wide variety of programming languages like Python, C++, Java,etc It can be used to process images and videos to identify objects, faces, etc.

How to install OpenCV Python?

OpenCV can be directly downloaded and installed with the use of pip (package manager).

To install OpenCV, just go to the command-line and type the following command:

pip install opencv-python

Performing basic tasks using OpenCV:

1) Reading an Image

Reading an image is a very important step as all further operations are based only on this image.

To read an image we use the imread() function present in OpenCV. The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix.

# Importing the OpenCV library
import cv2

# Reading the image using imread() function
image = cv2.imread('image.png')

#Displaying the image that we read using imshow() in a window 
cv2.imshow(“Frame”, image)

2) Resizing the Image

Resizing is mainly required to reduce the size of a very large image into the desired size. To resize the image we can use the resize() function. The resize function takes images and dimensions as input.

image = cv2.resize(image, (800, 800))

Replace (800,800) with the desired resolution to resize the image.

3) Reading a Video

In OpenCV, a video can be read either by using the feed from a camera connected to a computer or by reading a video file. The first step towards reading a video file is to create a VideoCapture object. Its argument can be either the device index or the name of the video file to be read.

cap = cv2.VideoCapture(‘video.mp4')

To use webcam feed as the source we can replace ‘video.mp4’ with ‘0’ or the corresponding camera to be selected. In most cases, only one camera is connected to the system. So, all we do is pass ‘0’ and OpenCV uses the only camera attached to the computer. When more than one camera is connected to the computer, we can select the second camera by passing ‘1’, the third camera by passing ‘2’ and so on.

cap = cv2.VideoCapture(0)

To display the video or perform detections, OpenCV follows a frame by frame approach in which the video is split into frames and then detections or other tasks are performed on each frame.

import cv2

# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('video.mp4')

# Check if camera opened successfully
if (cap.isOpened()== False): 
  print("Error opening video stream or file")

# Read until video is completed
  # Capture frame-by-frame
  ret, frame =
  if ret == True:

    # Display the resulting frame

    # Press Q on keyboard to  exit
    if cv2.waitKey(25) & 0xFF == ord('q'):

  # Break the loop

# When everything done, release the video capture object

# Closes all the frames

4) Convert color in OpenCV

Once you load the image, you may require the image in a specific color format eg. HSV so OpenCV can also convert it to different color schemes using different flags in cvtColor.


Different color schemes can be used. cv2.COLOR_BGR2RGB is most commonly used for color conversion. Here are some other flags for cvtColor: COLOR_BGR2GRAY, COLOR_BGR2HSV, and COLOR_BGR2YUV, etc.

Face Detection using OpenCV & Python

Face detection using Haar cascades is a machine learning based approach where a cascade function is trained with a set of input data.

Haar Cascade is an Object Detection Algorithm used to identify faces in an image or a real time video. The algorithm uses edge or line detection features proposed by Viola and Jones in their research paper “Rapid Object Detection using a Boosted Cascade of Simple Features” published in 2001. The algorithm is given a lot of positive images consisting of faces, and a lot of negative images not consisting of any face to train on them.

OpenCV already contains many pre-trained classifiers for face, eyes, smiles, etc.. We will be using the face classifier. You can experiment with other classifiers as well.

You need to download the trained classifier XML file (haarcascade_frontalface_default.xml), which is available in OpenCv’s GitHub repository. Save it to your working location.

Download Link:

(Right Click and Click Save As & Enter)

GitHub repository:

1) To detect faces in images:

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') #save this xml file in the same folder you are currently working in.

# Read the input image
img = cv2.imread('test.jpg') #Replace test.jpg with the name of your file

# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)

# Draw rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

# Display the output
cv2.imshow('img', img)


Things to note:

  1. The detection works only on grayscale images. So it is important to convert the color image to grayscale.

  2. detectMultiScale function (line 10) is used to detect the faces. It takes 3 arguments — the input image, scaleFactor and minNeighbours. i) scaleFactor specifies how much the image size is reduced with each scale. ii) minNeighbours specifies how many neighbors each candidate rectangle should have to retain it.

  3. faces contains a list of coordinates for the rectangular regions where faces were found. We use these coordinates to draw the rectangles in our image.

Similarly, we can detect faces in videos. As you know videos are basically made up of frames, which are still images. So we perform the face detection for each frame in a video.

2)To detect faces in video:

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# To capture video from a webcam. 
cap = cv2.VideoCapture(0)

# To use a video file as input 
# cap = cv2.VideoCapture('filename.mp4')

while True:
    # Read the frame
    _, img =

    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)

    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

    # Display
    cv2.imshow('img', img)

    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k==27:
# Release the VideoCapture object


The only difference here is that we use an infinite loop to loop through each frame in the video. We use to read each frame. The first value returned is a flag that indicates if the frame was read correctly or not. The second value returned is the still frame on which we will be performing the detection.

There are many trained classifiers available, you can try them as well.

Conclusion :

We have seen the basics of OpenCV that everyone should know. Now you can create various Computer Vision based applications.. For more information, you can explore the documentation of OpenCV. Happy learning!!

42 views0 comments

Recent Posts

See All



bottom of page