Image Resizing with OpenCV and PyTorch
This article explains how to resize images using OpenCV's cv2.resize function and how to scale multi‑dimensional tensors in PyTorch with torch.nn.functional.interpolate, providing detailed parameter descriptions and practical code examples for both single images and batch processing.
Image scaling is commonly performed with the cv2.resize() function from the opencv‑python package. The function signature is cv2.resize(src, size, fx, fy, interpolation) , where src is the input image, size specifies the output dimensions, fx and fy are optional scaling factors for the horizontal and vertical axes, and interpolation selects the interpolation method (e.g., cv.INTER_NEAREST , cv.INTER_LINEAR , cv.INTER_CUBIC , cv.INTER_AREA ).
Typical usage for a single image:
<code>import cv2
# Read the original image
image = cv2.imread("XX.jpg")
# Desired size (e.g., 640 pixels)
size = 640
image_resize = cv2.resize(image, size)</code>Batch resizing of all images in a directory and saving the results:
<code>import os
import cv2
# Directory containing original images
datadir = "XXX"
# Destination folder
save_path = 'XXX'
if not os.path.exists(save_path):
os.makedirs(save_path)
SIZE = 640
path = os.path.join(datadir)
img_list = os.listdir(path)
for i in img_list:
img_array = cv2.imread(os.path.join(path, i), cv2.IMREAD_COLOR)
new_image = cv2.resize(img_array, (SIZE, SIZE))
cv2.imwrite(os.path.join(save_path, i), new_image)</code>When working with feature maps represented as tensors, OpenCV cannot be used directly; instead, PyTorch provides torch.nn.functional.interpolate for resizing. The function signature is torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None) . Here, input is the tensor (typically 4‑D with shape B×C×H×W), size defines the output dimensions, scale_factor scales the input by a factor, mode selects the up‑sampling algorithm (e.g., 'nearest', 'bilinear', 'bicubic'), and align_corners controls corner alignment for certain modes.
Example of scaling a 3‑D tensor by first adding a channel dimension, applying a quarter‑scale down‑sampling, and then removing the added dimension:
<code># Assume train_mask is a 3‑D tensor
train_mask = train_mask.unsqueeze(1).float()
train_mask = torch.nn.functional.interpolate(train_mask, scale_factor=1/4, mode='bilinear', align_corners=False)
train_mask = train_mask.squeeze(1).float()</code>The key point is that torch.nn.functional.interpolate() operates on each channel's height and width, requiring a 4‑D tensor (B, C, H, W). This method is preferred for resizing feature maps in deep learning pipelines.
Disclaimer: This article is compiled from online sources; copyright belongs to the original authors.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.