收入囊中
- 高斯金字塔
- 拉普拉斯金字塔
對一張圖像不斷的模糊之後向下採樣,得到不同分辨率的圖像,同時每次得到的新的圖像寬與高是原來圖像的1/2, 最常見就是基於高斯的模糊之後採樣,得到的
一系列圖像稱爲高斯金字塔。
原圖來自http://blog.csdn.net/jia20003/article/details/9116931
- 高斯模糊[用如下卷積,注意要除以256,用上之前的知識,這是一個可分離的濾波,所以只需要2K次計算,不需要K^2]
1 4 6 4 1 4 16 24 16 4 6 24 36 24 6 4 16 24 16 4 1 4 6 4 1 - 去掉所有的偶數行和偶數列
- 每個維度都擴大兩倍並插入0,也就是f(2i,2j) = f(i,j) , f(2i+1,2j+1) = 0
- 再用相同的高斯核進行高斯卷積,結果再乘4
- C++: void pyrDown(InputArray src, OutputArray dst, const Size& dstsize=Size(), int borderType=BORDER_DEFAULT )
-
- src – 輸入圖像
- dst – 輸出圖像
- dstsize – 輸出圖像的size
- C++: void pyrUp(InputArray src, OutputArray dst, const Size& dstsize=Size(), int borderType=BORDER_DEFAULT )
-
- src – 輸入圖像
- dst – 輸出圖像
- dstsize – 輸出圖像的size
-
- C++: void buildPyramid(InputArray src, OutputArrayOfArrays dst, int maxlevel, int borderType=BORDER_DEFAULT )
-
- src – 輸入圖像
- dst – 這是一個存儲(maxlevel+1)的向量,dst[0]和源圖像一樣,dst[1]是第一次下采樣
- maxlevel – 指定採樣級數,必須爲非負數
內部實現是不斷call pyrDown()
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
Mat src, dst, tmp;
const char* window_name = "Pyramids Demo";
int main(int argc,char **argv)
{
printf( "\n Zoom In-Out demo \n " );
printf( "------------------ \n" );
printf( " * [u] -> Zoom in \n" );
printf( " * [d] -> Zoom out \n" );
printf( " * [ESC] -> Close program \n \n" );
src = imread(argv[1]);
if( !src.data )
{ printf(" No data! -- Exiting the program \n");
return -1; }
tmp = src;
dst = tmp;
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
imshow( window_name, dst );
for(;;)
{
int c;
c = waitKey(10);
if( (char)c == 27 )
{ break; }
if( (char)c == 'u' )
{ pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
printf( "** Zoom In: Image x 2 \n" );
}
else if( (char)c == 'd' )
{ pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
printf( "** Zoom Out: Image / 2 \n" );
}
imshow( window_name, dst );
tmp = dst;
}
return 0;
}
下面我們就用buildPyramid和PyrUp來建立DOG(拉普拉斯金字塔)
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <sstream>
using namespace cv;
using namespace std;
Mat src,tmp;
vector<Mat>gaussPyrs;
vector<Mat>DOG;
int maxLevel = 3;
int main(int argc,char **argv)
{
src = imread(argv[1]);
if( !src.data ){
printf(" No data! -- Exiting the program \n");
return -1;
}
buildPyramid(src, gaussPyrs, maxLevel);
for(int i = 0;i < maxLevel;i++){
Size ss;
pyrUp( gaussPyrs[i+1], tmp, ss);
Mat dst = gaussPyrs[i] - tmp;
DOG.push_back(dst);
}
for(int i = 0;i < maxLevel;i++){
stringstream ss;
ss << i;
string s;
ss >> s;
namedWindow( s, CV_WINDOW_AUTOSIZE );
imshow( s, DOG[i] );
}
waitKey(0);
return 0;
}