Skip to content
Tutorials Center
My learning

Topics

Lesson 120 of 149 ยท Data and the Web

Computer Vision with OpenCV

Learn OpenCV with Python. Create and inspect images as NumPy arrays, convert colors, resize, blur, threshold, detect edges and count shapes with contours, with runnable examples.

OpenCV (Open Source Computer Vision Library) is the standard toolbox for teaching computers to look at pictures: finding edges, counting objects, tracking motion, reading barcodes, detecting faces. The Python package is called opencv-python and you import it as cv2. Its core idea is simple and powerful: an image is just a NumPy array of numbers, so everything you already know about NumPy applies.

Pillow is great for everyday picture editing such as resizing, cropping and converting formats. OpenCV is the tool when you want to analyse what is in the picture. In this lesson you build images from scratch (so every example runs in your browser with no files), then use OpenCV to convert colors, resize, blur, threshold, find edges and count shapes.

In your own projects you install it with pip install opencv-python (or opencv-python-headless on servers without a screen). Reading real photos works with cv2.imread("photo.jpg"); the examples below create their pictures in code so the page does not need any files.

Images are arrays

A color image is a 3-dimensional array with shape (height, width, 3). Each pixel holds three numbers from 0 to 255 for blue, green and red. OpenCV uses the order BGR, not RGB. That is the single most common source of "why are my colors wrong?" bugs. A grayscale image has only two dimensions, (height, width).

Python Build an image and inspect it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that coordinates for drawing functions are (x, y) (column first), while NumPy indexing is [row, column], which is [y, x]. Mixing these up is the second most common mistake.

Seeing the result

OpenCV can open a window with cv2.imshow() on your own computer, but a browser page has no window. Here we use Matplotlib to display arrays instead. Because Matplotlib expects RGB, we convert from BGR first with cv2.cvtColor.

Python Show images side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Color spaces and grayscale

Many algorithms work on grayscale because one number per pixel is easier than three. cv2.cvtColor(img, code) converts between color spaces. Besides grayscale, HSV (hue, saturation, value) is popular because "all the green things" is a simple range on one channel, while in BGR it is a messy mix. Note that OpenCV stores hue from 0 to 179, not 0 to 359.

Python Pick out a color with HSV

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Resizing and flipping

cv2.resize(img, (width, height)) takes the size as (width, height), again the opposite order from img.shape. Choose the interpolation to match your goal: INTER_AREA for shrinking, INTER_CUBIC or INTER_LINEAR for enlarging, and INTER_NEAREST to keep hard pixel edges. cv2.flip and cv2.rotate handle mirroring and quarter turns.

Python
small = cv2.resize(img, (100, 60), interpolation=cv2.INTER_AREA)
half = cv2.resize(img, None, fx=0.5, fy=0.5)     # scale by a factor instead
mirrored = cv2.flip(img, 1)                       # 1 = left-right, 0 = up-down
turned = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)

Blurring, thresholding and edges

Real pictures are noisy, so most pipelines blur first. A Gaussian blur replaces each pixel by a weighted average of its neighbors. The kernel size must be odd, such as (5, 5).

Thresholding turns grayscale into pure black and white: pixels above a limit become 255, the rest 0. Otsu's method picks the limit automatically. Canny finds edges, the places where brightness changes sharply, using two thresholds (weak and strong).

Python Blur, threshold and Canny edges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The exact numbers depend on the random noise, but you should see the noise drop a lot after blurring and the edges hug the square's border near rows and columns 30 and 70.

Counting objects with contours

A contour is the outline of a white blob in a black-and-white image. cv2.findContours returns them all, and you can measure each with cv2.contourArea, cv2.boundingRect or cv2.arcLength. This is how simple counting tools (coins, cells, screws) work: threshold, find contours, filter by size.

Python Count and measure shapes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The rectangle drawn from (10, 10) to (60, 50) covers 51 by 41 pixels, yet contourArea reports 2,000 (50 by 40). OpenCV measures the polygon that runs through the pixel centers of the outline, so the area is one pixel smaller in each direction. That is normal.

Video, faces and what to try next

OpenCV also reads video with cv2.VideoCapture("clip.mp4") (or camera 0): you loop, read one frame at a time and process each frame as an image. Classic face detection uses a trained Haar cascade file that ships with the package.

Python
import cv2

cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
photo = cv2.imread("group.jpg")
gray = cv2.cvtColor(photo, cv2.COLOR_BGR2GRAY)
faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in faces:
    cv2.rectangle(photo, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imwrite("faces.jpg", photo)

This block is read-only on this site: it needs a real photo and the cascade data file. Modern projects often use deep-learning detectors through cv2.dnn or libraries built on PyTorch, but the surrounding workflow (load, convert, resize, draw, save) is the same.

Common mistakes

  • Forgetting BGR order, so blue shows as orange in Matplotlib or Pillow. Convert with cv2.COLOR_BGR2RGB.
  • Confusing (x, y) in drawing and resize with [row, column] in array indexing.
  • cv2.imread does not raise an error for a missing file. It returns None, and the next line crashes. Always check if img is None.
  • Drawing functions change the array in place. Use img.copy() if you want to keep the original.
  • Thresholds and Canny limits are tuned by experiment; there is no value that works for every picture.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In what channel order does OpenCV store a color pixel by default?

    OpenCV stores blue first, then green, then red. Convert with cv2.cvtColor(img, cv2.COLOR_BGR2RGB) before showing the image with libraries that expect RGB.
  2. 2. An image has img.shape == (480, 640, 3). What size do you pass to cv2.resize to make it half as wide and half as tall?

    shape is (height, width, channels) but cv2.resize wants (width, height). The target is 320 wide and 240 tall.
  3. 3. What does cv2.imread("missing.jpg") do when the file does not exist?

    It fails silently by returning None. Check for it right away so the error message points at the real problem.
  4. 4. Why do most pipelines blur an image before running Canny edge detection?

    Noise creates tiny brightness jumps everywhere. Smoothing first leaves only the strong edges that belong to real objects.
  5. 5. Which pair of calls is the usual way to count blobs in a grayscale picture?

    Thresholding produces a black-and-white image, and findContours returns one outline per white blob. You can then filter them by area.

Summary

  • OpenCV (cv2) treats images as NumPy arrays: (height, width, 3) in BGR order for color, (height, width) for grayscale.
  • Drawing functions take (x, y) points; array indexing is [row, column]; cv2.resize takes (width, height).
  • cv2.cvtColor converts color spaces (BGR to gray, RGB or HSV); cv2.inRange makes masks for color selection.
  • Blur with GaussianBlur, binarize with threshold (Otsu picks the limit), find edges with Canny.
  • findContours plus contourArea, boundingRect and approxPolyDP finds and measures objects.
  • imread returns None on failure, and cascades or video need real files, so those examples are read-only here.

Next: Game Development with Pygame.