Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 54 ยท Practice and Review

OpenCV Interview Questions

Prepare for computer vision interviews with OpenCV questions on BGR, kernels, Canny, Otsu, homography, ORB vs SIFT and speed, each with a small live check.

OpenCV questions in interviews are rarely about obscure functions. Interviewers want to know whether you understand what an image is in memory, why a classic algorithm behaves the way it does, and which mistake silently ruins results. This page collects the questions that come up most, with answers short enough to say aloud and a tiny experiment for each claim. Backing a statement with a quick experiment is a habit worth practicing.

A note on what is real here: cv2 cannot run on this site, so function calls appear in read-only blocks, and every live check is a numpy or plain Python stand-in that demonstrates the idea. The stand-ins follow the documented behavior, but they are not the library itself.

Where questions come from

Memory Arrays, dtype, BGR, views.
Filtering Kernels, blur, Canny, Otsu.
Geometry Homography, affine, coordinates.
Practice Features, speed, debugging.

Answer the idea first, then name the function.

1 / 2

Images in memory

1. What is an image in OpenCV's Python API?

A plain numpy ndarray. A color image has shape (height, width, 3) and dtype uint8, and a grayscale image has shape (height, width). There is no special image class, so every numpy tool works on pixels. See Images Are NumPy Arrays.

2. Why do colors look wrong when I show an OpenCV image with matplotlib?

OpenCV stores channels in B, G, R order, while matplotlib and most libraries expect R, G, B. Convert with cv2.cvtColor(img, cv2.COLOR_BGR2RGB), or reverse the last axis. See Color Spaces.

Python BGR versus RGB

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.

3. What does cv2.imread return if the path is wrong?

None, with no exception. The error appears later, as a confusing AttributeError or an assertion inside another function. Always check the result:

Python
img = cv2.imread("photo.png")
if img is None:
    raise FileNotFoundError("photo.png could not be read")

4. Is a pixel addressed as (x, y) or (row, column)?

Both exist, which is the trap. Indexing an array uses img[row, col], which is img[y, x]. Drawing and geometry functions (cv2.circle, cv2.rectangle, cv2.resize) take points and sizes as (x, y) and (width, height), while img.shape reports (height, width, channels). See Common Mistakes and Debugging.

Python Row and column versus x and y

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.

5. What happens when you add 100 to a uint8 pixel of value 200?

With numpy, 200 + 100 wraps modulo 256 and gives 44. With cv2.add the result saturates at 255. Use whichever you mean, or widen the dtype first. See Data Types, Ranges and Saturation.

Python Wraparound versus saturation

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.

Filtering and edges

6. Why are kernel sizes odd?

An odd-sized kernel has a single center pixel, so the output is aligned with the input pixel it describes. An even size has no center and would shift the result by half a pixel. Blur functions such as GaussianBlur and medianBlur reject even sizes or require odd ones.

7. What is the difference between convolution and correlation, and which does filter2D do?

Convolution flips the kernel before sliding it over the image, and correlation does not. cv2.filter2D actually computes correlation. For symmetric kernels (box, Gaussian) the two are the same. For asymmetric kernels such as Sobel or emboss, flip the kernel yourself if you need true convolution. See Kernels and filter2D.

Python Correlation versus convolution on an impulse

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 impulse shows the effect clearly: convolution reproduces the kernel as written, and correlation reproduces it reversed.

8. How does the Canny detector work, and what do the two thresholds mean?

Canny runs four stages: smooth with a Gaussian, compute gradient magnitude and direction, thin edges with non-maximum suppression, then apply hysteresis thresholding. A pixel above the high threshold is a sure edge, a pixel below the low threshold is discarded, and a pixel in between is kept only if it connects to a sure edge. A common starting ratio is high = 2 to 3 times low. See Canny Edge Detection.

Python Hysteresis on one row of gradient values

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 weak run 50, 52 is dropped because it never touches a strong pixel, while the weak neighbors of the strong 90 survive.

9. How does Otsu's method choose a threshold?

It tries every threshold, splits the histogram into two classes, and keeps the one with the largest between-class variance. It works best when the histogram has two humps and fails when the object covers a tiny fraction of the picture. See Thresholding.

Python Otsu on a tiny bimodal sample

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.

Any threshold in the gap separates the two groups equally well, and the first one that reaches the maximum is reported.

Geometry and features

10. How many point pairs do you need for a homography, and why?

A homography is a 3 by 3 matrix defined up to scale, so it has 8 degrees of freedom. Each point pair gives two equations, so four pairs (no three collinear) fix it exactly. With more points use cv2.findHomography with RANSAC, which tolerates wrong matches. See Perspective Transforms and Feature Matching and Homography.

Python Solve a homography from four points

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.

11. ORB or SIFT, and how do their descriptors differ?

ORB uses binary descriptors compared with Hamming distance (count differing bits), so it is very fast, license-free and a good default on weak hardware. SIFT uses floating-point descriptors compared with L2 distance, is more robust to scale and lighting change but slower. SIFT is in the main package since OpenCV 4.4, after its patent expired. Pair binary descriptors with NORM_HAMMING and float ones with NORM_L2.

Python
orb = cv2.ORB_create(nfeatures=1000)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)
matches = bf.knnMatch(desc1, desc2, k=2)
good = [m for m, n in matches if m.distance < 0.75 * n.distance]   # Lowe ratio test
Python Hamming versus L2 distance

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.

Performance

12. Why is looping over pixels in Python slow, and what do you do instead?

Every iteration runs through the interpreter, while a vectorized operation runs one compiled loop over contiguous memory. Prefer OpenCV functions and numpy expressions, work on a smaller image or a region of interest when the task allows, and measure before optimizing. See Making OpenCV Fast.

Python Loop versus vectorized, same result

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 loop does 3,600 interpreter steps for a tiny image, and a real photo has millions of pixels. The vectorized line does the same job in one call.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A colleague displays an OpenCV image with matplotlib and the blues and reds are swapped. What is the most likely cause?

    OpenCV uses B, G, R channel order. Converting with COLOR_BGR2RGB or reversing the channel axis fixes it.
  2. 2. In Canny, a pixel between the low and high thresholds is kept when...

    That is hysteresis: weak pixels survive only if they are linked to strong edge pixels.
  3. 3. Why does a homography need at least four point pairs?

    A 3 by 3 matrix defined up to scale has 8 unknowns, and each correspondence supplies two equations.
  4. 4. Which distance matches ORB's binary descriptors?

    Binary descriptors are compared by counting differing bits, which is the Hamming distance (NORM_HAMMING).
  5. 5. What does cv2.imread return for a file that does not exist?

    imread signals failure by returning None, so you must check for it yourself.

Summary

  • An OpenCV image is a numpy array, with BGR channel order and (row, col) indexing but (x, y) in drawing and size arguments.
  • uint8 numpy math wraps; cv2.add saturates; imread returns None on failure.
  • filter2D is correlation, kernels are odd so they have a center, and Canny uses non-maximum suppression plus hysteresis.
  • Otsu maximizes between-class variance; a homography has 8 degrees of freedom and needs four point pairs.
  • ORB (binary, Hamming) trades some robustness for speed; SIFT (float, L2) is more robust but slower.
  • Vectorize first, then measure before tuning anything else.

Next up: OpenCV Cheat Sheet.