原理
- 图像金字塔的概念:
如上图所示:一系列图像的集合,最底层是原始图像,然后长宽各缩小 1 2 \frac{1}{2} 21(即缩小4倍),循环下去,直到最后只有一个像素。
两种常见的图像金字塔:
- Gaussian pyramid: 通常用来进行下采样图像(zoom out)
- Laplacian pyramid: 通常用来进行上采样图像(zoom in)
- 高斯金字塔(本教程使用的):
1. 缩小分为两个步骤:
(1) 用高斯核对图像进行卷积操作:
![](https://i-blog.csdnimg.cn/blog_migrate/c568e7878412adb96298026eb2b89767.png)
(2) 删除偶数行和列
2. 怎么进行放大?:
(1) 先将图像偶数行和列进行0填充
(2) 再用高斯核进行卷积模糊操作
代码
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
const char* window_name = "pyramids demo";
int main(void)
{
const char* filename = "../res/beauty.jpg";
Mat src = imread(filename);
if(src.empty())
{
cout << "can't load the image" << endl;
}
for(;;)
{
imshow(window_name,src);
char c = (char) waitKey(0);
if(c == 27)
break;
if(c == 'i')
{
cv::pyrUp(src,src,Size(src.cols*2,src.rows*2));
}
if(c == 'o')
{
cv::pyrDown(src,src,Size(src.cols/2,src.rows/2));
}
}
return 0;
};
结果:
按下 i : 放大图片2倍
按下 o : 缩小图像2倍
OpenCV API
图像下采样,由大图像到小图像 1. void cv::pyrDown
(
InputArray src, //输入图像
OutputArray dst, //输出图像
const Size & dstsize = Size(), //输出图像的大小
int borderType = BORDER_DEFAULT //处理边缘的方法
)
图像上采样,由小图像到大图像
2. void cv::pyrUp
(
InputArray src, //输入图像
OutputArray dst, //输出图像
const Size & dstsize = Size(), //输出图像大小
int borderType = BORDER_DEFAULT //处理边缘的方法
)