Read, show, write
import cv2
img = cv2.imread("photo.jpg", cv2.IMREAD_COLOR) # BGR, None if missing
if img is None:
raise FileNotFoundError("photo.jpg")
cv2.imshow("win", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("copy.png", img)
Headless servers: avoid imshow; save files or use Jupyter matplotlib after BGR→RGB.
Arrays and color
import numpy as np
print(img.shape, img.dtype) # (H, W, 3), uint8 typical
roi = img[100:200, 50:300]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
Main modules
- core — matrices, drawing primitives.
- imgproc — filters, morphology, contours, histograms.
- features2d / calib3d — keypoints, homography, stereo.
- videoio / video — capture, optical flow, tracking helpers.
- dnn — run ONNX/Caffe/TF/Torch models.
Install notes
Do not install opencv-python and opencv-contrib-python in the same environment—they conflict. For CUDA-enabled builds you typically compile from source or use vendor-provided wheels.
Takeaways
- BGR default; many ML stacks expect RGB—convert explicitly.
- OpenCV excels at classical CV and deployment-friendly inference.
- Pair with PyTorch/TensorFlow when you need training loops.
Quick FAQ
Wrong path, unicode path issues on Windows, or unsupported format—verify with
os.path.exists.PIL is convenient for simple transforms; OpenCV is faster for video, geometric ops, and DNN pre/post-processing in many workflows.