Skip to content

Instantly share code, notes, and snippets.

@prabindh
Last active October 22, 2018 14:55
Show Gist options
  • Save prabindh/852787042c1f0246132ffa4b17f4e220 to your computer and use it in GitHub Desktop.
Save prabindh/852787042c1f0246132ffa4b17f4e220 to your computer and use it in GitHub Desktop.
// Call like below
// boxedMat will contain resized image
// boxedMat = __LetterBoxResize(floatMat, net->w, net->h);
//
// Taken mostly from https://jdhao.github.io/2017/11/06/resize-image-to-square-with-padding/
cv::Mat ArapahoV2::__LetterBoxResize(cv::Mat img, int w, int h)
{
cv::Mat intermediateImg, outputImg;
int delta_w, delta_h, top, left, bottom, right;
int new_w = img.size().width;
int new_h = img.size().height;
if (((float)w / img.size().width) < ((float)h / img.size().height)) {
new_w = w;
new_h = (img.size().height * w) / img.size().width;
}
else {
new_h = h;
new_w = (img.size().width * h) / img.size().height;
}
cv::resize(img, intermediateImg, cv::Size(new_w, new_h));
delta_w = w - new_w;
delta_h = h - new_h;
top = floor(delta_h / 2);
bottom = delta_h - floor(delta_h/2);
left = floor(delta_w / 2);
right = delta_w - floor(delta_w/2);
cv::copyMakeBorder(intermediateImg, outputImg, top, bottom, left, right, cv::BORDER_CONSTANT, (0,0,0));
return outputImg;
}
@ooobelix
Copy link

Can you update the code since my last pull request on fixArapahoLibDarknetUsed, I will test it. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment