参考:https://blog.youkuaiyun.com/jizhidexiaoming/article/details/80306677
#include <opencv2/opencv.hpp>
using namespace cv;
Mat my_resize(const Mat &, int, int);
int main()
{
Mat img = imread("D:/Code/Image/classic.jpg", 0);
imshow("原图", img);
Mat new_img = my_resize(img, 200, 100); // (width, height),(col, row)
imshow("缩放后效果", new_img);
waitKey(0);
return 0;
}
Mat my_resize(const Mat &img, int width, int height)
{
// 1, 定义缩放后的图像大小,行列缩放比例
Mat output = Mat::zeros(Size(width, height), CV_8UC1);
float width_scale = (float)img.cols / width; // 列缩放比例,相对于算法前面讲的k1
float height_scale = (float)img.rows / height; // 行缩放比例,即k2
// 2, 采样
for (int i = 0; i < height; i++) // 注意i,j的范围, i < height * img.rows / height;
{
for (int j = 0; j < width; j++)
{
output.at<uchar>(i, j) = img.at<uchar>(round(i * height_scale), round(j * width_scale));
}
}
return output;
}