OpenCV Cheat Sheet
OpenCV computer vision reference covering image I/O, filtering, edge detection, contours, drawing, and video capture in Python.
2 PagesIntermediateApr 10, 2026
Read, Write & Display
Load, convert, and save images.
python
import cv2img = cv2.imread("photo.jpg") # loaded in BGR order, not RGBgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)resized = cv2.resize(img, (640, 480))cv2.imwrite("output.jpg", resized)cv2.imshow("Window", img)cv2.waitKey(0)cv2.destroyAllWindows()
Filtering & Thresholding
Common preprocessing operations.
python
blurred = cv2.GaussianBlur(img, (5, 5), sigmaX=0)edges = cv2.Canny(gray, threshold1=100, threshold2=200)_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))dilated = cv2.dilate(thresh, kernel, iterations=1)
Contours & Drawing
Detect shapes and annotate images.
python
contours, hierarchy = cv2.findContours( thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)cv2.drawContours(img, contours, -1, (0, 255, 0), 2)cv2.rectangle(img, (10, 10), (100, 100), (255, 0, 0), 2)cv2.circle(img, (50, 50), 20, (0, 0, 255), -1)cv2.putText(img, "Label", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
Core Functions
Building blocks used across most CV pipelines.
- cv2.imread / imwrite- load/save images (BGR channel order)
- cv2.cvtColor- convert color spaces (BGR2GRAY, BGR2RGB, BGR2HSV)
- cv2.GaussianBlur / medianBlur- noise reduction filters
- cv2.Canny- edge detection
- cv2.findContours- detect object outlines in a binary image
- cv2.VideoCapture- read frames from a camera or video file
- cv2.CascadeClassifier- Haar cascade face/object detection
Pro Tip
OpenCV loads and stores images in BGR channel order, not RGB — convert with cv2.cvtColor(img, cv2.COLOR_BGR2RGB) before displaying with matplotlib or passing images to most deep-learning frameworks.
Was this cheat sheet helpful?
Explore Topics
#OpenCV#OpenCVCheatSheet#DataScience#Intermediate#ReadWriteDisplay#FilteringThresholding#ContoursDrawing#CoreFunctions#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance