python
def _resize_image(im, width, height):
w, h = im.shape[1], im.shape[0]
r = min(width / w, height / h)
# print(w, h, r)
new_w, new_h = int(w * r), int(h * r)
# print(new_w, new_h)
im = cv2.resize(im, (new_w, new_h))
pw = (width - new_w) // 2
ph = (height - new_h) // 2
top, bottom = ph, ph
left, right = pw, pw
if top + bottom + new_h < height:
bottom += 1
if left + right + new_w < width:
right += 1
im = cv2.copyMakeBorder(im, top, bottom, left, right, borderType=cv2.BORDER_CONSTANT, value=114)
C++
float r = std::min(INPUT_W / (img.cols * 1.0), INPUT_H / (img.rows * 1.0));
int unpad_w = r * img.cols;
int unpad_h = r * img.rows;
cv::Mat re(unpad_h, unpad_w, CV_8UC3);
cv::resize(img, re, re.size());
cv::Mat out(INPUT_W, INPUT_H, CV_8UC3, cv::Scalar(114, 114, 114));
re.copyTo(out(cv::Rect(0, 0, re.cols, re.rows)));
|