Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 54 ยท Performance and Production

Making OpenCV Fast

Speed up OpenCV code: avoid Python pixel loops, use vectorized NumPy and cv2 calls, work on ROIs, try UMat and OpenCL, set threads, and profile with real timings.

OpenCV is fast because its heavy lifting runs in compiled C++. A Python program that calls cv2.GaussianBlur on a large image spends nearly all its time in optimized native code. The same program becomes painfully slow the moment you write for y in range(h): for x in range(w): and touch pixels one at a time from Python. Most "OpenCV is slow" stories are really "my Python loop is slow" stories.

This lesson gives you a short list of habits that matter, in the order that usually pays off: measure first, remove Python loops, do less work, then reach for threads and the GPU. The live examples use NumPy in real Pyodide to make the first three habits visible. OpenCV-specific switches such as UMat and setNumThreads are read-only code, because opencv-python cannot run in this page.

Speed checklist

Measure Profile before you optimize.
No pixel loops Use whole-array operations.
Do less Smaller images, smaller regions.
Then hardware Threads, UMat, GPU builds.

The first three are free and usually enough.

1 / 2

Measure first

Guessing which line is slow is how people spend a day optimizing the wrong thing. Time the real pipeline with cv2.getTickCount, or with Python's time.perf_counter, which works everywhere.

Python
import time
import cv2

image = cv2.imread("big.jpg")

start = time.perf_counter()
blurred = cv2.GaussianBlur(image, (15, 15), 0)
elapsed_ms = (time.perf_counter() - start) * 1000
print(f"blur took {elapsed_ms:.1f} ms")

Rules for honest timing: run the operation several times and take the best or the median (the first run includes warm-up), measure just the code you care about, and use a realistic image size. For a whole program, python -m cProfile -s cumtime script.py shows which functions eat the time, and a line profiler such as line_profiler pinpoints lines inside them.

For video, remember the budget: at 30 frames per second, one frame must finish in about 33 ms, including reading, processing and drawing.

Replace Python loops with array operations

A pixel loop in Python executes millions of interpreter steps. One whole-array expression executes one fast native loop. Here is the same task, brightening an image by 50 with saturation, done both ways, with a check that the results agree.

Python Loop versus vectorized

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 timing varies from machine to machine, so the example prints a yes/no comparison instead of seconds. On a typical computer the gap is far larger than 20 times. The vectorized version also shows an important correctness detail: converting to a wider type (int16) before adding avoids uint8 wraparound, which Data Types, Ranges and Saturation covers in depth.

In OpenCV itself, the vectorized form is a single call: cv2.convertScaleAbs(img, alpha=1, beta=50) or cv2.add(img, 50), both of which saturate for you. The rule: if a cv2 function does what you need, call it; otherwise use NumPy slicing, broadcasting and boolean masks; write a Python loop only as a last resort. If an algorithm truly needs a loop, move it into compiled code with a tool like Numba or write it in C++.

Do less work

The cheapest operation is the one you skip.

  • Shrink first. Many algorithms (face detection, contour finding, optical flow) work fine on a half-size image. Cost grows with the pixel count, so halving each side removes 75% of the work. Use cv2.resize(..., interpolation=cv2.INTER_AREA) when shrinking.
  • Convert once. Do cvtColor to grayscale once, not in every function that needs it.
  • Work on a region of interest (ROI). Once you know where the action is, process only that part. A NumPy slice is a view, so cropping costs nothing.
  • Skip frames. Run an expensive detector every 5th frame and a cheap tracker in between (see Motion and Object Tracking).
  • Reuse buffers. Passing an existing array as dst= avoids allocating a new one each frame.
Python ROI slices are free views

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 view is great for speed, and it is also the reason Accessing and Modifying Pixels warns you to call .copy() when you need an independent image.

Threads and builds

OpenCV parallelizes many of its functions internally. You can inspect and change the thread count.

Python
import cv2

print("threads:", cv2.getNumThreads())
cv2.setNumThreads(4)          # 0 disables OpenCV's own threading
print("optimized code enabled:", cv2.useOptimized())
print(cv2.getBuildInformation())   # CPU features, parallel backend, OpenCL, CUDA

getBuildInformation() is the way to find out what your wheel was compiled with. The standard opencv-python wheels include SIMD optimizations and a threading backend but not CUDA. If you need NVIDIA GPU support for OpenCV's own functions, you build OpenCV from source with CUDA enabled; there is no official pip wheel for that.

If you also run your own threads or processes (for example a web server with many workers), more OpenCV threads can fight with them. In that case, setting cv2.setNumThreads(1) per worker and using multiple worker processes often gives better total throughput.

UMat and OpenCL

Many OpenCV functions accept a cv2.UMat, a wrapper that lets the library run the operation through OpenCL, on a GPU or other accelerator when one is available (the "transparent API"). You wrap an array once, chain operations, and copy back at the end.

Python
import cv2

print("OpenCL available:", cv2.ocl.haveOpenCL())
cv2.ocl.setUseOpenCL(True)

frame = cv2.imread("big.jpg")
u = cv2.UMat(frame)                       # upload once

gray = cv2.cvtColor(u, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (9, 9), 0)
edges = cv2.Canny(blur, 50, 150)

result = edges.get()                      # download once, back to a NumPy array

Whether UMat helps depends on the machine. Uploads and downloads cost time, so it pays off only for several chained operations on larger images, and a small image can be slower than plain arrays. Measure both paths on your target hardware before you commit.

Smarter algorithm choices

Sometimes the answer is a cheaper method, not a faster implementation.

  • Use a separable filter: GaussianBlur already does two 1D passes, but a custom filter2D with a large 2D kernel does not.
  • Use cv2.INTER_NEAREST or INTER_LINEAR when quality does not matter.
  • Use ORB instead of SIFT when speed matters more than the best matching quality.
  • Use integral images for repeated box sums.
  • Decode video at a lower resolution if your camera or codec allows it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a nested Python for loop over pixels slow?

    Each iteration goes through the interpreter, which is orders of magnitude slower than one native whole-array operation.
  2. 2. What is true about a NumPy slice such as frame[100:200, 300:400]?

    Basic slicing returns a view, so it is free but changes made through it affect the original.
  3. 3. You halve the width and height of an image before running an algorithm whose cost is proportional to the pixel count. How much work remains?

    Half the width times half the height is a quarter of the pixels.
  4. 4. Which first step should you take before optimizing?

    Measuring prevents wasted effort on code that is not the bottleneck.

Summary

  • Time the real pipeline first; run several repeats and use realistic sizes.
  • Replace Python pixel loops with cv2 calls or whole-array NumPy expressions.
  • Do less: shrink images, convert once, process ROIs, skip frames, reuse buffers.
  • setNumThreads controls OpenCV's internal threading; getBuildInformation shows what your build supports; standard wheels have no CUDA.
  • UMat may use OpenCL but only helps for chains of work on larger images; measure it.

Next, learn to avoid the classic traps in Common Mistakes and Debugging.