Skip to content

Instantly share code, notes, and snippets.

@wlucas-DFT
Created March 9, 2015 19:10
Show Gist options
  • Save wlucas-DFT/7e21bbcdf2e6128639e3 to your computer and use it in GitHub Desktop.
Save wlucas-DFT/7e21bbcdf2e6128639e3 to your computer and use it in GitHub Desktop.
Does a simple percentage based center crop.
Mat centerCropMat(const cv::Mat& image, double cropPercentage)
{
cropPercentage = min(cropPercentage, 1.0);
int newRows = (int)floor(image.rows * cropPercentage + 0.5);
int newCols = (int)floor(image.cols * cropPercentage + 0.5);
Point center(image.cols / 2, image.rows / 2);
Rect roi(
center.x - (newCols / 2),
center.y - (newRows / 2),
newCols,
newRows
);
return image(roi).clone();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment