如何使用Numpy/Keras对加载的224×224图像进行平移式零填充?
Solution for Translating 224x224 Image with Zero-Padding
Got it, let's walk through how to implement the image translation you need while keeping the 224×224 size and filling empty areas with 0. Since you're already using Keras' image preprocessing tools, we can pair that with numpy for the translation logic—it's straightforward and fits right into your existing workflow.
Step 1: Understand the Core Logic
The approach is simple:
- Create an empty 224×224×3 array filled with 0 (this will be our output image)
- Calculate the correct position to place the original image inside this empty array based on your x/y shift values
- Copy the original image into that position, leaving the unused space as 0
Step 2: Full Implementation Code
Here's a complete example that builds directly on your existing code:
from keras.preprocessing import image import numpy as np def translate_image(img_array, x_shift, y_shift): # Get the shape of the input image (224, 224, 3) height, width, channels = img_array.shape # Initialize an empty array filled with 0 (matches the input shape) translated_img = np.zeros_like(img_array) # Calculate where to place the original image in the translated array start_x = max(0, x_shift) start_y = max(0, y_shift) # Calculate the end coordinates for the placed image end_x = min(width, width + x_shift) end_y = min(height, height + y_shift) # Calculate which part of the original image to use (handles negative shifts) orig_start_x = max(0, -x_shift) orig_start_y = max(0, -y_shift) orig_end_x = min(width, width - x_shift) orig_end_y = min(height, height - y_shift) # Copy the original image region into the translated array translated_img[start_y:end_y, start_x:end_x, :] = img_array[orig_start_y:orig_end_y, orig_start_x:orig_end_x, :] return translated_img # Your existing image loading code image_path = "your_image_path.jpg" img = image.load_img(image_path, target_size=(224, 224)) x = image.img_to_array(img) # Example usage: Shift right by 30 pixels, down by 20 pixels translated_x = translate_image(x, x_shift=30, y_shift=20) # Optional: Visualize the result (using matplotlib) import matplotlib.pyplot as plt plt.subplot(121) plt.imshow(x.astype('uint8')) plt.title('Original Image') plt.subplot(122) plt.imshow(translated_x.astype('uint8')) plt.title('Translated Image (Zero-Padded)') plt.show()
Key Details to Know
- Shift Directions: Positive
x_shiftmoves the image right, negative moves it left. Positivey_shiftmoves it down, negative moves it up. - Zero-Padding: Any area not covered by the original image after translation stays 0 (appears as black in RGB images, which aligns with your requirement).
- Shape Consistency: The output array maintains the exact (224, 224, 3) shape as your input, so it works seamlessly with any downstream Keras processing you might be doing.
This method is lightweight, uses only libraries you're already working with, and gives you full control over the translation amount.
内容的提问来源于stack exchange,提问作者Shamoon
相关产品推荐
相关产品推荐

