1 | initial version |
You could simple try. But the right answer is NO, you should pre scale image before input:
cv::Mat cropAndResizeFromCenter(const cv::Mat &input, const cv::Size size)
{
cv::Rect roiRect(0,0,0,0);
if( (float)input.cols/input.rows > (float)size.width/size.height) {
roiRect.height = input.rows;
roiRect.width = input.rows * (float)size.width/size.height;
roiRect.x = (input.cols - roiRect.width)/2.0;
} else {
roiRect.width = input.cols;
roiRect.height = input.cols * (float)size.height/size.width;
roiRect.y = (input.rows - roiRect.height)/2.0;
}
roiRect &= cv::Rect(0, 0, input.cols, input.rows);
cv::Mat output;
if(roiRect.area() > 0) {
cv::Mat croppedImg(input, roiRect);
int interpolationMethod = 0;
if(size.area() > roiRect.area())
interpolationMethod = CV_INTER_CUBIC;
else
interpolationMethod = CV_INTER_AREA;
cv::resize(croppedImg, output, size, 0, 0, interpolationMethod);
}
return output;
}