cv2.resize: OpenCV Image Resizing Guide

The function in OpenCV used to adjust the size of an image is called cv2.resize. Its usage is: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]).

Description of Parameters:

  1. src: Source image that needs to be resized.
  2. dsize: Adjust the size of the image after resizing by specifying the width and height or a scaling factor.
  3. dst: an optional parameter, representing the target image.
  4. fx: Optional parameter, specifying the scaling factor in the horizontal direction.
  5. fy: optional parameter, specifies the scaling factor in the vertical direction.
  6. Interpolation: Optional parameter that specifies the method of image interpolation.

Example usage:

import cv2

# 读取源图像
src = cv2.imread('image.jpg')

# 调整图像大小为指定宽度和高度
resized_img = cv2.resize(src, (500, 400))

# 调整图像大小为指定缩放因子
resized_img = cv2.resize(src, None, fx=0.5, fy=0.5)

# 使用插值方法调整图像大小
resized_img = cv2.resize(src, (500, 400), interpolation=cv2.INTER_LINEAR)

When adjusting the image size, the resulting image size can be determined by specifying the size of the target image or the scaling factor. Additionally, different image interpolation methods can also be selected to control the pixel interpolation method when adjusting the image size.

bannerAds