"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Efficiently Crop Random Image Patches from a 4D Numpy Array using Strided-Based Slicing?

How to Efficiently Crop Random Image Patches from a 4D Numpy Array using Strided-Based Slicing?

Published on 2024-11-08
Browse:281

How to Efficiently Crop Random Image Patches from a 4D Numpy Array using Strided-Based Slicing?

Efficient Numpy Slicing for Random Image Cropping

For efficient cropping of random 16x16 patches from a 4D Numpy array representing multiple color images (where the first dimension is the number of images, and the second and third are the equal width and height), a strided-based approach can be utilized.

Utilizing np.lib.stride_tricks.as_strided or scikit-image's view_as_windows

These methods create sliding windows as views into the input array, reducing memory overhead. Scikit-image's view_as_windows simplifies the setup by specifying the window shape as a tuple whose elements correspond to the dimensions of the input array. Axes for sliding are assigned window lengths, and other axes are set to 1.

Code Example

# Import scikit-image for view_as_windows
from skimage.util.shape import view_as_windows

# Get sliding windows
w = view_as_windows(X, (1,16,16,1))[...,0,:,:,0]

# Generate random per-image offsets
x = np.random.randint(0,12,X.shape[0])
y = np.random.randint(0,12,X.shape[0])

# Index and extract specific windows
out = w[np.arange(X.shape[0]),x,y]

# Reformat if necessary
out = out.transpose(0,2,3,1)

This code generates four random (x_offset, y_offset) pairs and extracts 4 random 16x16 patches within the given parameters, with minimum memory overhead.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3