OpenCV Master Hub + Recipes (Python)
Preprocessing, edges/contours, morphology, feature detection, object tracking, homography/AR, OCR, and video I/O—copy‑paste ready.
Image I/O (read/write, show, decode)
import cv2
import numpy as np
img = cv2.imread("input.jpg") # BGR order
gray = cv2.imread("input.jpg", cv2.IMREAD_GRAYSCALE)
# Show (simple debug viewer)
cv2.imshow("Image", img); cv2.waitKey(0); cv2.destroyAllWindows()
# Write
cv2.imwrite("out.png", img)
# Decode from bytes (e.g., web)
data = open("input.jpg","rb").read()
arr = np.frombuffer(data, dtype=np.uint8)
img2 = cv2.imdecode(arr, cv2.IMREAD_COLOR)
Drawing & Annotations
canvas = img.copy()
cv2.rectangle(canvas, (50,50), (200,200), (0,255,0), 2) # BGR
cv2.circle(canvas, (300,150), 40, (255,0,0), -1) # filled
cv2.putText(canvas, "Hello", (50,40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,0,255), 2)
Color Spaces (BGR↔RGB, HSV, LAB)
# BGR -> RGB for plotting in matplotlib
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# BGR -> HSV for color thresholding
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = (35, 50, 50); upper = (85, 255, 255) # green-ish
mask = cv2.inRange(hsv, lower, upper)
seg = cv2.bitwise_and(img, img, mask=mask)
# BGR -> LAB for illumination‑robust operations
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
Filtering & Denoising
# Blur/denoise
blur = cv2.GaussianBlur(img, (5,5), 1.2)
median = cv2.medianBlur(img, 5)
bilateral = cv2.bilateralFilter(img, 9, 75, 75)
# Sharpen
import numpy as np
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
sharp = cv2.filter2D(img, -1, kernel)
# Histogram equalization (grayscale)
eq = cv2.equalizeHist(gray)
# CLAHE (better local contrast)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB); L,a,b = cv2.split(lab)
L2 = clahe.apply(L)
clahe_img = cv2.merge([L2,a,b])
clahe_img = cv2.cvtColor(clahe_img, cv2.COLOR_LAB2BGR)
Thresholds & Edge Detection
# Global & Otsu
ret, th = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Adaptive
adp = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 31, 5)
# Canny edges
edges = cv2.Canny(gray, 100, 200)
Contours, Approximation, Bounding Boxes
cnts, _ = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
canvas = img.copy()
for c in cnts:
area = cv2.contourArea(c)
if area < 200: continue
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(canvas, (x,y), (x+w,y+h), (0,255,0), 2)
# Polygon approximation
eps = 0.02*cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, eps, True)
cv2.drawContours(canvas, [approx], -1, (255,0,0), 2)
Morphology (erode/dilate, open/close)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
er = cv2.erode(th, kernel, iterations=1)
dl = cv2.dilate(th, kernel, iterations=1)
open_ = cv2.morphologyEx(th, cv2.MORPH_OPEN, kernel, iterations=1) # remove noise
close = cv2.morphologyEx(th, cv2.MORPH_CLOSE, kernel, iterations=1) # close gaps
Feature Detection (ORB/SIFT) & Matching
# ORB (free, fast)
orb = cv2.ORB_create(nfeatures=1000)
kp1, des1 = orb.detectAndCompute(img, None)
kp2, des2 = orb.detectAndCompute(img2, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda m: m.distance)[:50]
vis = cv2.drawMatches(img, kp1, img2, kp2, matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
# SIFT (requires contrib sometimes; robust, float descriptors)
# sift = cv2.SIFT_create(); use FLANN with KD‑trees for matching
Object Tracking (KCF/CSRT)
import cv2
cap = cv2.VideoCapture("video.mp4")
ok, frame = cap.read()
bbox = cv2.selectROI("Select", frame, False, False)
tracker = cv2.legacy.TrackerCSRT_create() # or TrackerKCF_create
tracker.init(frame, bbox)
while True:
ok, frame = cap.read()
if not ok: break
ok, bbox = tracker.update(frame)
if ok:
x,y,w,h = map(int, bbox)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("Track", frame)
if cv2.waitKey(1) == 27: break
cap.release(); cv2.destroyAllWindows()
Homography & AR Overlays
# Given matched points pts1 (source) and pts2 (destination)
H, mask = cv2.findHomography(pts1, pts2, cv2.RANSAC, 5.0)
# Warp overlay onto destination
h,w = overlay.shape[:2]
warp = cv2.warpPerspective(overlay, H, (dest.shape[1], dest.shape))
# Blend with mask
gray = cv2.cvtColor(warp, cv2.COLOR_BGR2GRAY)
m = (gray > 0).astype("uint8")*255
inv = cv2.bitwise_not(m)
base = cv2.bitwise_and(dest, dest, mask=inv)
aug = cv2.add(base, warp)
OCR with Tesseract
# pip install pytesseract pillow
import cv2, pytesseract
from PIL import Image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
prep = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
txt = pytesseract.image_to_string(Image.fromarray(prep), lang="eng")
print(txt)
Video I/O & Capture
# Read webcam
cap = cv2.VideoCapture(0) # or file path/rtsp url
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter("out.mp4", fourcc, 30.0, (640,480))
while True:
ok, frame = cap.read()
if not ok: break
frame = cv2.resize(frame, (640,480))
out.write(frame)
cv2.imshow("Cam", frame)
if cv2.waitKey(1) == 27: break
cap.release(); out.release(); cv2.destroyAllWindows()
Performance Tips
-
– Work in grayscale when possible; prefer uint8 to reduce memory/CPU.
– Reuse preallocated arrays and avoid copying images unnecessarily.
– Use vectorized operations (bitwise, threshold, morphology) over Python loops.
– For heavy compute, use cv2.cuda.* if GPU is available; otherwise, try smaller frames or ROI processing.
– Batch operations when reading/writing frames; avoid per‑frame reinitialization.
# Example: ROI processing
x,y,w,h = 100,100,200,200
roi = img[y:y+h, x:x+w]
roi_blur = cv2.GaussianBlur(roi, (9,9), 2.0)
img[y:y+h, x:x+w] = roi_blur
Troubleshooting & FAQ
Colors look wrong in matplotlib
Convert BGR→RGB before plotting: cvtColor(img, COLOR_BGR2RGB).
Contours empty or noisy
Preprocess with blur + threshold; adjust retrieval mode and min area filters.
Tracker loses target
Use CSRT for accuracy, KCF for speed; re‑initialize when confidence drops.
Webcam not opening
Try other indices (1,2), backends (CAP_DSHOW), or release other apps using the camera.
OCR garbage output
Improve preprocessing, set correct lang, try page segmentation modes (psm) via config.
Slow pipeline
Resize frames, process ROI, use uint8, move heavy steps to GPU (cv2.cuda) if available.
FAQ
ORB vs SIFT? ORB is fast and free (binary descriptors); SIFT is more robust with float descriptors—use FLANN for matching.
When to use HSV vs LAB? HSV simplifies color thresholding; LAB helps with illumination changes and local contrast (CLAHE).
How to stabilize video processing? Use temporal smoothing, running averages, and lock exposure/white balance on the camera when possible.
© 2025 Pythoneo · Designed to be highly linkable: comprehensive, evergreen, copy‑paste friendly, with performance and troubleshooting built‑in.