OpenCV Python Library Cheatsheet

Computer Vision is a rapidly evolving field that involves teaching machines to interpret and understand visual information from the world around us. OpenCV (Open Source Computer Vision Library) plays a pivotal role in this domain by providing a plethora of tools and functions for image and video processing. To help both beginners and seasoned practitioners navigate through the rich features of OpenCV, we present a comprehensive cheatsheet that covers essential concepts and functions.

1. Installation

Before diving into the OpenCV world, make sure you have it installed. You can use the following command for Python:

pip install opencv-python

For the latest version:

pip install opencv-python-headless

2. Importing OpenCV

import cv2

3. Reading and Displaying Images

# Read an image from file
image = cv2.imread('path/to/image.jpg')

# Display the image
cv2.imshow('Image', image)

4. Reading and Displaying Videos

# Read a video from file or camera
cap = cv2.VideoCapture('path/to/video.mp4')  # or 0 for default camera

while True:
    ret, frame =

    # Display the video frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):


5. Basic Image Operations

Grayscale Conversion

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


resized_image = cv2.resize(image, (width, height))


blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)

6. Drawing on Images


cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), thickness)


cv2.rectangle(image, (x, y), (x + width, y + height), (0, 255, 0), thickness)

Circles, (center_x, center_y), radius, (0, 255, 0), thickness)

7. Image Thresholding

ret, thresholded_image = cv2.threshold(gray_image, threshold_value, max_value, cv2.THRESH_BINARY)

8. Contour Detection

contours, hierarchy = cv2.findContours(thresholded_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

9. Face Detection (Using Haarcascades)

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray_image, scaleFactor, minNeighbors)

10. Image Transformation

Affine Transformation

M = cv2.getAffineTransform(pts1, pts2)
transformed_image = cv2.warpAffine(image, M, (width, height))

Perspective Transformation

M = cv2.getPerspectiveTransform(pts1, pts2)
perspective_transformed_image = cv2.warpPerspective(image, M, (width, height))


This cheatsheet provides a quick reference for essential OpenCV operations. Keep in mind that this is just the tip of the iceberg, and OpenCV offers many more advanced features. Experiment with these functions and explore the vast possibilities that OpenCV brings to the world of Computer Vision.


1. What is OpenCV, and how is it used in Computer Vision?

OpenCV, or Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It provides a wide range of tools and functions for image and video processing, making it a key resource for tasks such as image recognition, object detection, and video analysis in the field of Computer Vision.

2. How can I install OpenCV on my system?

You can install OpenCV for Python using the following command:
pip install opencv-python
For the latest version:
pip install opencv-python-headless
This installs the necessary packages to begin using OpenCV in your Python projects.

3. How can I perform basic image operations like resizing and blurring using OpenCV?

OpenCV provides simple functions for basic image operations. For resizing, you can use cv2.resize(image, (width, height)), and for blurring, you can apply a Gaussian blur using cv2.GaussianBlur(image, (kernel_size, kernel_size), 0).

4. What is the importance of thresholding in image processing with OpenCV?

Thresholding is a fundamental image processing technique that simplifies visual data for further analysis. In OpenCV, the cv2.threshold function is used to convert grayscale images into binary images by setting a threshold value. It is commonly employed in tasks like image segmentation and object detection.

5. How can I use OpenCV for face detection?

OpenCV provides pre-trained Haarcascades for face detection. You can use the cv2.CascadeClassifier to load the face cascade and then apply it to detect faces in an image using the detectMultiScale method. Here’s a basic example:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') faces = face_cascade.detectMultiScale(gray_image, scaleFactor, minNeighbors)
Adjust the parameters like scaleFactor and minNeighbors based on your specific use case for optimal results.