Open ooolky opened 10 months ago
步骤
cv::Mat GetSquareImage(const cv::Mat& img_origin, int target_width = 640)
{
int width = img_origin.cols,
height = img_origin.rows,
crop = 6;
//crop
cv::Mat img = img_origin(cv::Rect(width / crop, 0, width - width / crop, height));
//resize
cv::Mat square = cv::Mat::zeros(target_width, target_width, img.type());
int max_dim = (width >= height) ? width : height;
float scale = ((float)target_width) / max_dim;
cv::Rect roi;
if (width >= height)
{
roi.width = target_width;
roi.x = 0;
roi.height = height * scale;
roi.y = (target_width - roi.height) / 2;
}
else
{
roi.y = 0;
roi.height = target_width;
roi.width = width * scale;
roi.x = (target_width - roi.width) / 2;
}
cv::resize(img, square(roi), roi.size());
return square;
}
python
步骤