0
0
Computer Visionml~5 mins

Resizing images in Computer Vision

Choose your learning style9 modes available
Introduction

Resizing images changes their size to fit your needs. It helps models work faster and use less memory.

When you want all images to have the same size before training a model.
When you need smaller images to speed up processing on a phone or small device.
When you want to prepare images for display on a website or app with fixed space.
When you want to reduce the file size to save storage or bandwidth.
When you want to crop or zoom into a specific part of an image by resizing.
Syntax
Computer Vision
resized_image = cv2.resize(image, (new_width, new_height))

The cv2.resize function is from the OpenCV library.

The size is given as (width, height) in pixels.

Examples
Resize image to 100 pixels wide and 100 pixels tall.
Computer Vision
resized = cv2.resize(img, (100, 100))
Resize image to 200 pixels wide and 150 pixels tall.
Computer Vision
resized = cv2.resize(img, (200, 150))
Resize image to half its original width and height using scale factors.
Computer Vision
resized = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
Sample Model

This code creates a small 3x3 color image, resizes it to 6x6, and prints the shapes and pixel values before and after resizing.

Computer Vision
import cv2
import numpy as np

# Create a simple 3x3 image with 3 color channels (RGB)
image = np.array([
    [[255, 0, 0], [0, 255, 0], [0, 0, 255]],
    [[255, 255, 0], [0, 255, 255], [255, 0, 255]],
    [[0, 0, 0], [127, 127, 127], [255, 255, 255]]
], dtype=np.uint8)

print('Original image shape:', image.shape)

# Resize image to 6x6 pixels
resized_image = cv2.resize(image, (6, 6))

print('Resized image shape:', resized_image.shape)

# Show pixel values of resized image
print('Resized image array:')
print(resized_image)
OutputSuccess
Important Notes

Resizing can change the image quality; enlarging may cause blurriness.

Use interpolation methods like cv2.INTER_LINEAR or cv2.INTER_AREA for better results.

Always keep the aspect ratio if you want to avoid stretching the image.

Summary

Resizing changes image size to fit your needs.

Use cv2.resize with target width and height.

Resizing helps models run faster and saves space.