I am trying to crop square patches from images based on center coordinates and scale, using basic NumPy slicing (the exact same as 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))
However, the extracted patches are often not square at all, as evidenced by the print statements:
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)
The min and max coordinates are calculated correctly, so clearly my way of slicing must be wrong? What am I missing here?