Luc*_*iaX 6 python deep-learning tensorflow
我有一批形状正确的图像[batchsize,h,w,channel]
。以及一批左上点的形状[batchsize,2]
。
我需要使用边界框(假设大小为 [10,10])及其相应的左上角点来裁剪每个图像。
除了循环批量大小之外,有人知道如何做到这一点吗?
已经检查过的事情:
- tf.image.resize_image_with_crop_or_pad
Run Code Online (Sandbox Code Playgroud)
但是图像必须位于[h,w,channel]
- tf.slice
Run Code Online (Sandbox Code Playgroud)
但它的起始位置必须是 int
小智 0
1.导入库:
import numpy as np
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
4. 执行矢量化裁剪:
cropped_images = images[np.arange(images.shape[0])[:, None], slices[:, 0], slices[:, 1], slices[:, 2], slices[:, 3]]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
571 次 |
最近记录: |