图像插值总结
最近邻插值
若将原图像缩小,得到目标图像
- 给定缩小的比例,如: x方向缩小比例为0.5,y方向缩小为0.5
- 分辨率四舍五入(分辨率不能为小数)
由于存在四舍五入,因为误差较大,缩小失真,放大有马赛克。
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
//输入图像及缩放比例
Mat imgReduction(Mat &srcImage, float kx, float ky)
{
//获取图片的分辨率
int nRows = cvRound(srcImage.rows * kx);
int nCols = cvRound(srcImage.cols * ky);
//定义输出图像并初始化
Mat dstImage(nRows, nCols, srcImage.type());
for (int i = 0; i < nRows; i++)
{
//缩放后进行四舍五入
int x = static_cast<int>((i + 1) / kx + 0.5) - 1;
for (int j = 0; j < nCols; j++)
{
int y = static_cast<int>((j + 1) / ky + 0.5) - 1;
dstImage.at<Vec3b>(i, j) = srcImage.at<Vec3b>(x, y);
}
}
return dstImage;
}
int main()
{
Mat srcImage = imread("d:/opencv/test_img/morph_demo.png");
if (!srcImage.data)
{
printf("image could not load \n");
return -1;
}
Mat dstImage = imgReduction(srcImage, 9, 9);
imshow("srcImage", srcImage);
imshow("dstImage", dstImage);
waitKey(0);
return 0;
}