首页 > 解决方案 > 为什么使用基本 NumPy 切片无法正确裁剪我的图像?

问题描述

我正在尝试使用基本的 NumPy 切片(与此问题完全相同)根据中心坐标和比例从图像中裁剪方形补丁:

label_index, photo_id, ratio_x, ratio_y = line.split(",")
label = LABELS[int(label_index)]
bgr = cv2.imread(DIR_DATASET + "images/" + folder + "/" + photo_id + ".jpg")
height, width = bgr.shape[:2]
center_x, center_y = int(width * float(ratio_x)), int(height * float(ratio_y))
print("center: " + str(center_x) + ", " + str(center_y))
patch_radius = int((min(width, height) * 0.23) / 2)
print("patch radius: " + str(patch_radius))
min_x = center_x - patch_radius
min_y = center_y - patch_radius
max_x = center_x + patch_radius
max_y = center_y + patch_radius
if min_x < 0 or min_y < 0 or max_x > width or max_y > height: continue
print("coords: min: " + str(min_x) + ", " + str(min_y) + "; max: " + str(max_x) + ", " + str(max_y))
patch = bgr[min_x:max_x,min_y:max_y,:] # bgr[min_x:max_x,min_y:max_y] gives the same results
print("patch shape: " + str(patch.shape))

然而,提取的补丁通常根本不是正方形的,正如打印语句所证明的那样:

NEWLINE
center: 2002, 598
patch radius: 230
coords: min: 1772, 368; max: 2232, 828
patch shape: (228, 460, 3)
NEWLINE
center: 2375, 727
patch radius: 230
coords: min: 2145, 497; max: 2605, 957
patch shape: (0, 460, 3)
NEWLINE
center: 2566, 820
patch radius: 230
coords: min: 2336, 590; max: 2796, 1050
patch shape: (0, 460, 3)
NEWLINE
center: 2195, 603
patch radius: 230
coords: min: 1965, 373; max: 2425, 833
patch shape: (35, 460, 3)

最小和最大坐标计算正确,所以很明显我的切片方式一定是错误的?我在这里想念什么?

标签: pythonnumpyopencvindexingslice

解决方案


你切片不正确。语法是sub_image = full_image[y_start: y_end, x_start:x_end].

在您的代码中,拼接中的第一个坐标限制是 x 坐标,而应该是 y 坐标。也就是说,您应该将拼接线更正为

patch = bgr[min_y : max_y, min_x : max_x] 

推荐阅读