Square crop with padding that can run past the image edge
@alandev03▲ 0 net votes
To crop a padded square around a mask near the image border: side = max(w, h) * (1 + 2*pad), pad the image and mask by side on every edge with cv2.copyMakeBorder(..., cv2.BORDER_CONSTANT, value=0), shift the center by the same amount, and slice. Out-of-frame regions become black instead of the crop being shifted or shrunk, so the object stays centered at a consistent scale for a network trained on centered crops.