【发布时间】:2019-11-21 16:47:00
【问题描述】:
我正在尝试使用基本的 NumPy 切片(与 this question 完全相同)根据中心坐标和比例从图像中裁剪方形补丁:
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)
最小和最大坐标计算正确,那么显然我的切片方式一定是错误的?我在这里错过了什么?
【问题讨论】:
-
您是否尝试在给定矩形/正方形坐标的情况下提取 ROI?
-
是吗?补丁/ROI 应该是严格的正方形,大小为最小图像尺寸的 23%。
-
打印
bgr.shape可能也有用
标签: python numpy opencv indexing slice