我有一批形状为
[batchsize,h,w,channel]
的图像。以及一批形状为 [batchsize,2]
的左上角点。
我需要用边界框(比方说尺寸为 [10,10])裁剪每个图像,及其相应的左上角点。
除了循环批量大小之外,任何人都知道如何做到这一点吗?
已经检查过的事情:
- tf.image.resize_image_with_crop_or_pad
但是图像必须位于
[h,w,channel]
- tf.slice
但是它的起始位置必须是 int
1。导入库:
import numpy as np
2。定义示例数据(替换为您的实际数据):
images = np.random.randint(0, 256, size=(10, 200, 200, 3)) # Batch of images
topleft_points = np.random.randint(0, 180, size=(10, 2)) # Top-left points
box_size = (10, 10) # Bounding box size
3.使用广播创建切片索引:
# Calculate bottom-right coordinates (inclusive)
bottomright_points = topleft_points + box_size
# Broadcast coordinates to match image dimensions
topleft_x = topleft_points[:, 0:1] # Add a dimension for broadcasting
topleft_y = topleft_points[:, 1:2]
bottomright_x = bottomright_points[:, 0:1]
bottomright_y = bottomright_points[:, 1:2]
# Create slicing indices
slices = np.concatenate([topleft_x, topleft_y, bottomright_x, bottomright_y], axis=1)
4。执行矢量化裁剪:
cropped_images = images[np.arange(images.shape[0])[:, None], slices[:, 0], slices[:, 1], slices[:, 2], slices[:, 3]]