# include
# include
using namespace cv;
// 近邻算法
void xresize(Mat &src, Mat &des, Size size){
des.create(size, src.type());
// 映射的原图坐标
int sx, sy = 0;
// 比例
float fx = (float)src.cols / des.cols;
float fy = (float)src.rows / des.rows;
for(int x = 0; x < des.cols; x++){
sx = fx * x + 0.5; // x为目标图的坐标
for(int y = 0; y < des.rows; y++){
sy = fy * y + 0.5; // y目标图的坐标
des.at(y, x) = src.at(sy, sx);
}
}
}
int main(int argc, char *argv[]){
Mat src = imread("./test1.jpg");
Mat img256;
Mat des256;
xresize(src, img256, Size(256, 256)); // 自定义函数
resize(src, des256, Size(256, 256), 0, 0, INTER_NEAREST); // OpenCV自定义函数, 后两个0表示fx,fy,当Size为None时使用fx,fx进行resize
namedWindow("src");
namedWindow("img256");
namedWindow("des256");
imshow("src", src);
imshow("img256", img256);
imshow("des256", des256);
waitKey(0);
printf("All Done!\n");
return 0;
}
# include
# include
using namespace cv;
int main(int argc, char *argv[]){
Mat src = imread("./test1.jpg");
Mat img1024;
Mat des1024;
resize(src, img1024, Size(1024, 1024), 0, 0, INTER_NEAREST); // OpenCV近邻算法自定义函数, 后两个0表示fx,fy,当Size为None时使用fx,fx进行resize
resize(src, des1024, Size(1024, 1024), 0, 0, INTER_LINEAR); // OpenCV双线性插值自定义函数
namedWindow("src");
namedWindow("img1024");
namedWindow("des1024");
imshow("src", src);
imshow("img1024", img1024);
imshow("des1024", des1024);
waitKey(0);
printf("All Done!\n");
return 0;
}
原理分析:双线性插值实质上是使用两次单线性插值操作进行数据的处理,原理如下(字丑莫怪)(参考):
结果分析:双线性插值的结果更平滑,分析原理可知利用邻近的四个像素点进行处理,类似一个滤波的操作;
①高斯金字塔:用于向下采样;
②拉普拉斯金字塔:用来从金字塔底层图像重建上层未采样图像;
③原理:参考
代码:
# include
# include
using namespace cv;
int main(int argc, char *argv[]){
Mat src = imread("./test1.jpg");
Mat gsrc;
Mat lsrc;
pyrDown(src, gsrc);
pyrUp(src, lsrc);
namedWindow("src");
namedWindow("gsrc");
namedWindow("lsrc");
imshow("src", src);
imshow("gsrc", gsrc);
imshow("lsrc", lsrc);
waitKey(0);
printf("All Done!\n");
return 0;
}
结果: