Sub topic 6
Choosing an Interpolation Method
This chapter assumes that you know basic Python, can install packages with pip, and understand NumPy arrays and image dimensions. You should also know that an image has a width, height, and color channels.
When you resize an image, the new image usually has a different number of pixels. The pixels in the new image do not always line up exactly with pixels in the original. OpenCV must estimate the missing color values. This process is called interpolation.
Why interpolation matters
Interpolation affects sharpness, edges, small details, and processing speed. The best method depends mainly on whether you are shrinking or enlarging the image.
flowchart TD
A[Choose a new image size] --> B{Shrinking or enlarging?}
B -->|Shrinking| C[Try INTER_AREA]
B -->|Enlarging| D[Try INTER_LINEAR or INTER_CUBIC]
C --> E[Inspect edges and fine details]
D --> E
E --> F[Choose the result that fits the task]The most commonly used methods are:
| Method | Typical use | Result |
|---|---|---|
| `INTER_NEAREST` | Masks, labels, pixel art | Fast, blocky edges |
| `INTER_LINEAR` | General enlargement | Smooth and reasonably fast |
| `INTER_AREA` | Shrinking photographs | Reduces aliasing and preserves area |
| `INTER_CUBIC` | Higher-quality enlargement | Smoother, slower, may soften edges |
| `INTER_LANCZOS4` | High-quality enlargement | Sharp detail, slower and sometimes ringing |
Nearest-neighbor interpolation chooses the closest original pixel. It does not create new blended colors. This makes it useful for segmentation masks, where each pixel represents a class such as “road” or “background.” Blending mask colors would produce invalid class values.
Linear interpolation estimates a pixel from nearby pixels. It is a good default for ordinary resizing and is usually faster than cubic or Lanczos interpolation.
Area interpolation considers the source pixels that cover each destination pixel. It is often the best first choice when reducing a photograph because it helps prevent jagged patterns and lost detail.
Cubic interpolation uses a larger neighborhood than linear interpolation. It often gives better-looking enlargement, but it takes more computation. Test it when image quality matters more than speed.
Applying a method with `cv2.resize`
The interpolation argument selects the method. When you provide dsize, OpenCV expects (width, height), while an image shape is reported as (height, width, channels).
import cv2
img = cv2.imread("photo.jpg")
if img is None:
raise FileNotFoundError("Could not read photo.jpg")
height, width = img.shape[:2]
# Shrink the image by half.
small = cv2.resize(
img,
(width // 2, height // 2),
interpolation=cv2.INTER_AREA,
)
# Enlarge the image by two times.
large = cv2.resize(
img,
None,
fx=2.0,
fy=2.0,
interpolation=cv2.INTER_CUBIC,
)
cv2.imwrite("small.jpg", small)
cv2.imwrite("large.jpg", large)When using scale factors, set dsize to None and provide both fx and fy. Keeping them equal preserves the aspect ratio. If they differ, the image stretches or compresses.
Common mistakes
- Using
INTER_NEARESTfor a photograph can create visibly blocky results. - Using
INTER_AREAfor a class mask can mix class values. Use nearest-neighbor instead. - Confusing
(width, height)incv2.resizewith(height, width)inimg.shape. - Expecting interpolation to restore detail that was already lost. Enlarging creates estimates, not original information.
- Resizing repeatedly can compound blur. Resize from the original image when possible.
Exercise
Which interpolation method is usually a good starting point for shrinking a photograph, and which method is suitable for a segmentation mask?
Show answer
Use cv2.INTERAREA for shrinking a photograph. Use cv2.INTERNEAREST for a segmentation mask so class labels are not blended.