OpenCV Python Cheat Sheet
OpenCV Python Cheat Sheet
Table of Contents
OpenCV Python Image Processing Cheat Sheet
1. Installation
5. Drawing Shapes
6. Image Thresholding
8. Edge Detection
9. Morphological Operations
python
import cv2
import numpy as np
5. Drawing Shapes
python
6. Image Thresholding
python
8. Edge Detection
python
9. Morphological Operations
Page 4 of 9
python
equalized = cv2.equalizeHist(gray)
14. Bitwise Operations
Page 5 of 9
python
template = cv2.imread('template.jpg', 0)
res = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + template.shape[1], top_left[1] + template.shape[0])
cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 2)
python
fgbg = cv2.createBackgroundSubtractorMOG2()
fgmask = fgbg.apply(frame)
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, 45, 1.0) # Rotate by 45 degrees
rotated = cv2.warpAffine(img, M, (w, h))
This cheat sheet should help you quickly reference image processing techniques using OpenCV in
Python! Let me know if you need any specific implementations.
Ummm nice actually... Now this time in a tabular format with all
the details like what the purpose
Here's your OpenCV Python Image Processing Cheat Sheet in a tabular format, including the
operation, purpose, and example code for easy reference.
in a window cv2.destroyAllWindows()
dimensions
interest (ROI)
color space
an image
(bounding box)
Draw a Circle Draw a filled or outlined cv2.circle(img, (150,150), 50, (0,0,255), -1)
circle
Write Text on Image Overlay text on an image cv2.putText(img, 'OpenCV', (50,250), cv2.FONT_HERSHEY_SIMPLEX,
1, (0,255,255), 2)
Simple Thresholding Convert grayscale image _, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
to binary
smooth an image
noise
blurring
(Canny)
Page 8 of 9
an image
an image
image cv2.CHAIN_APPROX_SIMPLE)
contours
cascades
Transformation perspective
on two images
two images
an image cv2.TM_CCOEFF_NORMED)
Optical Flow (Motion Detect motion between flow = cv2.calcOpticalFlowFarneback(old_gray, new_gray, None,
webcam
frame
Subtraction objects
angle
Page 9 of 9
This table provides a quick reference to OpenCV's key functions for image processing. Let me know if
you need further details or modifications!