Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

68. Sliding window detection

medium
GeneralGeneral
staff

Build a simple sliding-window object detector that scans an image and returns the best-matching window for a given template. You’ll compute a similarity score at each window location and pick the top result.

The matching score is normalized cross-correlation (NCC):

NCC(P,T)=∑i,j(Pij−Pˉ)(Tij−Tˉ)∑i,j(Pij−Pˉ)2∑i,j(Tij−Tˉ)2\text{NCC}(P, T)=\frac{\sum_{i,j}(P_{ij}-\bar{P})(T_{ij}-\bar{T})}{\sqrt{\sum_{i,j}(P_{ij}-\bar{P})^2}\sqrt{\sum_{i,j}(T_{ij}-\bar{T})^2}}

Requirements

Implement the function

python

Rules:

  • Slide the template over image from top-left to bottom-right using the given stride.
  • At each position, compute NCC between the current image patch and template.
  • Return the top-left coordinate of the window with the maximum NCC score.
  • Do not use any prebuilt template-matching utilities (e.g., OpenCV).
  • Use only NumPy and built-in Python libraries.

Example

python

Output:

python
Input Signature
ArgumentType
imagenp.ndarray
strideint
templatenp.ndarray
Output Signature
Return NameType
valuetuple

Constraints

  • Use NumPy; no OpenCV template-matching functions.

  • Only fully-contained windows; top-left coordinates returned.

  • Handle zero-variance denominator without crashing.

Hint 1

Convert image and template to NumPy arrays, and iterate only over valid top-left corners: r in [0..H-h], c in [0..W-w] stepping by stride.

Hint 2

Precompute the template’s mean-centered version T0 = T - T.mean() and its norm ||T0|| once; only the image patch statistics change per window.

Hint 3

For each patch P, compute P0 = P - P.mean(), then NCC = sum(P0*T0) / (||P0||*||T0||). If the denominator is 0 (zero variance patch or template), treat the score as -inf so it can’t win.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
staff
senior
entry
Tags
normalized-cross-correlation
sliding-window
template-matching
numpy
22 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit