OpenCV in Python: reading images, basic transforms, edges and face detection

OpenCV is a C++ computer vision library with official Python bindings (cv2). In Python, images are plain NumPy arrays, so you can mix OpenCV calls with NumPy slicing and arithmetic. This post walks through the basics: I/O, simple transforms, thresholding, blurring, edge detection, and face detection with a Haar cascade.
Installation
Section titled “Installation”The official wheels on PyPI come in four variants. Install only one of them in a given environment; they all provide the same cv2 module and conflict with each other.
pip install opencv-python # main modules, with GUI support (imshow)pip install opencv-contrib-python # main + extra "contrib" modulespip install opencv-python-headless # no GUI; for servers and Dockerpip install opencv-contrib-python-headlessThe examples below were checked against OpenCV 4.x. OpenCV 5 moved the Haar cascade classifier into the contrib modules; see the note in the face detection section.
Reading, displaying and saving images
Section titled “Reading, displaying and saving images”import sysimport cv2
image = cv2.imread("image.jpg")if image is None: sys.exit("could not read image.jpg")
print(image.shape) # (height, width, 3)Two things trip people up here:
imreaddoes not raise an exception when the file is missing or unreadable; it returnsNone. Check for it, or the error surfaces later as a confusing failure in another function.- Colour images are loaded in BGR channel order, not RGB. Convert with
cv2.cvtColor(image, cv2.COLOR_BGR2RGB)before passing the array to Matplotlib or Pillow.
To show the image in a window:
cv2.imshow("Image", image)cv2.waitKey(0) # wait for any key presscv2.destroyAllWindows()imshow needs a desktop session and a non-headless build. In Jupyter or on a server, convert to RGB and display with Matplotlib instead.
imwrite picks the format from the file extension and returns True on success:
cv2.imwrite("output.png", image)Basic operations
Section titled “Basic operations”Grayscale
Section titled “Grayscale”gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)Many algorithms (thresholding, Canny, Haar cascades) work on a single-channel image, so this is usually the first step.
Resizing
Section titled “Resizing”resize takes the target size as (width, height), the opposite order to image.shape:
small = cv2.resize(image, (200, 150), interpolation=cv2.INTER_AREA)
# or scale by a factor and keep the aspect ratiohalf = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)INTER_AREA gives better results when shrinking; INTER_LINEAR (the default) or INTER_CUBIC are better when enlarging.
Rotating
Section titled “Rotating”getRotationMatrix2D builds a 2×3 affine matrix from a centre, an angle in degrees (positive is counter-clockwise), and a scale. warpAffine applies it:
height, width = image.shape[:2]center = (width // 2, height // 2)
matrix = cv2.getRotationMatrix2D(center, 30, 1.0)rotated = cv2.warpAffine(image, matrix, (width, height))The output keeps the original size, so the corners get cut off. For exact 90° turns, cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE) is simpler and loses nothing.
Image processing
Section titled “Image processing”Thresholding
Section titled “Thresholding”threshold returns a tuple of the threshold used and the result image. Pixels above the threshold become maxval, the rest become 0:
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# let Otsu's method pick the threshold from the histogramotsu_value, otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)For uneven lighting, cv2.adaptiveThreshold computes a threshold per neighbourhood instead of one for the whole image.
Blurring
Section titled “Blurring”gaussian = cv2.GaussianBlur(image, (5, 5), 0) # kernel size must be odd; sigma 0 = derive from sizemedian = cv2.medianBlur(image, 5) # good for salt-and-pepper noisebilateral = cv2.bilateralFilter(image, 9, 75, 75) # smooths while keeping edges sharpEdge detection
Section titled “Edge detection”Canny takes a low and a high threshold for the hysteresis step. Blurring first reduces edges from noise:
edges = cv2.Canny(cv2.GaussianBlur(gray, (5, 5), 0), 50, 150)A high-to-low ratio of about 2:1 to 3:1 is a common starting point; tune from there.
Face detection with a Haar cascade
Section titled “Face detection with a Haar cascade”Haar cascades are an older, fast but fairly inaccurate detector. The pip wheels ship the pre-trained XML files, and cv2.data.haarcascades gives you their directory:
import sysimport cv2
image = cv2.imread("people.jpg")if image is None: sys.exit("could not read people.jpg")gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier( cv2.data.haarcascades + "haarcascade_frontalface_default.xml")if face_cascade.empty(): sys.exit("could not load cascade")
faces = face_cascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for x, y, w, h in faces: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # green in BGR
cv2.imwrite("faces.jpg", image)print(f"found {len(faces)} face(s)")scaleFactor controls how much the image shrinks between detection passes (smaller is slower but finds more sizes), and minNeighbors is how many overlapping detections a candidate needs before it counts; raise it if you get false positives. Like imread, a bad path doesn’t raise: the classifier just loads empty, which is why the empty() check is there.
Further reading
Section titled “Further reading”- OpenCV-Python tutorials
- opencv-python on PyPI, which explains the four package variants
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)