杰瑞科技汇

Python OpenCV如何调整图像尺寸?

Of course! In OpenCV, "size" can refer to a few different concepts, but it most commonly means the dimensions of an image (its width and height).

Python OpenCV如何调整图像尺寸?-图1
(图片来源网络,侵删)

Let's break down the key ways to work with size in Python OpenCV.

The Primary Way: image.shape

This is the most important and commonly used attribute. When you load an image, it becomes a NumPy array. The .shape property of this array tells you the dimensions of the image.

The shape is returned as a tuple in the format: (height, width, channels)

  • height: The number of rows (pixels) in the image.
  • width: The number of columns (pixels) in the image.
  • channels: The number of color channels (e.g., 3 for BGR, 1 for Grayscale).

Example:

import cv2
import numpy as np
# Load an image. Make sure the image 'my_image.jpg' exists in your script's directory.
# OpenCV loads images in BGR format by default.
image = cv2.imread('my_image.jpg')
# Check if the image was loaded successfully
if image is None:
    print("Error: Could not read the image.")
else:
    # Get the shape of the image
    height, width, channels = image.shape
    print(f"Image Shape: {image.shape}")
    print(f"Height: {height} pixels")
    print(f"Width: {width} pixels")
    print(f"Channels: {channels}")
    # For a grayscale image, the shape would be (height, width)
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    print(f"\nGrayscale Image Shape: {gray_image.shape}")

Output:

Python OpenCV如何调整图像尺寸?-图2
(图片来源网络,侵删)
Image Shape: (1080, 1920, 3)
Height: 1080 pixels
Width: 1920 pixels
Channels: 3
Grayscale Image Shape: (1080, 1920)

Getting Width and Height Separately

While you can unpack the shape tuple as shown above, OpenCV also provides convenient functions to get just the width or just the height. These are often used for convenience and readability.

  • image.shape[0] -> Height
  • image.shape[1] -> Width

But the preferred OpenCV way is:

  • cv2.image.size(image) -> Height
  • cv2.image.cols(image) -> Width

Note: cv2.image.size and cv2.image.cols are older functions. The most common and "Pythonic" way is to use image.shape. However, you will see image.shape[1] and image.shape[0] used frequently.

Example:

import cv2
image = cv2.imread('my_image.jpg')
if image is None:
    print("Error: Could not read the image.")
else:
    # Method 1: Using indexing on the shape tuple
    height_from_shape = image.shape[0]
    width_from_shape = image.shape[1]
    print(f"From shape - Height: {height_from_shape}, Width: {width_from_shape}")
    # Method 2: Using the dedicated OpenCV functions (less common in pure Python scripts)
    # These are more C++ style but work in Python bindings.
    height_from_func = cv2.image.size(image) # Note: This might be deprecated or behave differently in some versions.
    width_from_func = cv2.image.cols(image)
    print(f"From functions - Height: {height_from_func}, Width: {width_from_func}")
    # The most recommended and clear way in Python:
    h, w, _ = image.shape
    print(f"Recommended unpacking - Height: {h}, Width: {w}")

The Total Number of Pixels: image.size

This is another NumPy array attribute. It gives you the total number of elements (pixels) in the array.

  • For a color image: size = height * width * channels
  • For a grayscale image: size = height * width

Example:

import cv2
image = cv2.imread('my_image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print(f"Color Image Shape: {image.shape}")
print(f"Total pixels in color image: {image.size}") # 1080 * 1920 * 3
print(f"\nGrayscale Image Shape: {gray_image.shape}")
print(f"Total pixels in grayscale image: {gray_image.size}") # 1080 * 1920

Changing the Size: cv2.resize()

This is one of the most frequently used functions. You need to resize an image to fit a model's input requirements, display it in a smaller window, or scale it for analysis.

The function is: cv2.resize(src, dsize, fx, fy, interpolation)

  • src: The source image.
  • dsize: The desired output size as a tuple (width, height). This is the most common way to use it.
  • fx, fy: Scale factors along the horizontal and vertical axes. You can use dsize or fx/fy, but not both.
  • interpolation: The method used to interpolate pixel values. Common methods are:
    • cv2.INTER_NEAREST: Fastest, but lowest quality.
    • cv2.INTER_LINEAR: Default, good for upscaling.
    • cv2.INTER_CUBIC: Better quality for upscaling, but slower.
    • cv2.INTER_AREA: Best for downscaling. This is the recommended method for reducing image size.

Example:

import cv2
import numpy as np
# Create a dummy image for this example
image = np.zeros((500, 800, 3), dtype=np.uint8) # A black image of 500x800
cv2.putText(image, "Original", (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 255, 255), 5)
# --- Resize to a specific size (width, height) ---
# Resize to 300x200 pixels
resized_fixed = cv2.resize(image, (300, 200), interpolation=cv2.INTER_AREA)
# --- Resize using scale factors ---
# Make the image half its original size
fx = 0.5
fy = 0.5
resized_scaled = cv2.resize(image, None, fx=fx, fy=fy, interpolation=cv2.INTER_AREA)
# --- Display the images ---
cv2.imshow('Original Image', image)
cv2.imshow('Resized to Fixed Size (300x200)', resized_fixed)
cv2.imshow('Resized by Scale Factor (0.5x)', resized_scaled)
cv2.waitKey(0) # Wait for a key press
cv2.destroyAllWindows() # Close all windows

Summary Table

Concept Code / Attribute Description Returns
Image Dimensions image.shape The primary way to get an image's dimensions. (height, width, channels)
Image Width image.shape[1] Get the width (number of columns). int
Image Height image.shape[0] Get the height (number of rows). int
Total Pixels image.size The total number of elements in the NumPy array. int
Resizing Image cv2.resize() Changes the dimensions of an image. A new, resized image.
分享:
扫描分享到社交APP
上一篇
下一篇