从图像高斯差分(DoG)金字塔了解上下采样API及高斯模糊。
CV_EXPORTS_W void pyrDown( InputArray src, OutputArray dst,
const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
CV_EXPORTS_W void pyrUp( InputArray src, OutputArray dst,
const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
#include
#include
using namespace std;
using namespace cv;
int main() {
Mat src, dst;
src = imread("girl6.jpg");
if (!src.data)
{
printf("Wrong with Loading img!\n");
return -1;
}
Mat dst_pyrup, dst_pyrdown;
pyrDown(src, dst_pyrdown, Size(src.cols / 2, src.rows / 2));
pyrUp(src, dst_pyrup, Size(src.cols * 2, src.rows * 2));
imshow("dst_pyrdown", dst_pyrdown);
imshow("src", src);
imshow("dst_pyrup", dst_pyrup);
waitKey(0);
return -1;
}
由于高斯滤波核按统计分布分配像素值,图像中低频部分在不同强度(σ)的滤波下变化不大,高频部分在不同的强度的滤波下表现出差异,所以可以根据
不同强度下的滤波结果得到图像的高频信息。
简单说高斯模糊就是根据邻域像素到目标像素的距离按照正态分布(可以调整方差)的权重求加权平均值作为目标像素值。
CV_EXPORTS_W void GaussianBlur( InputArray src, OutputArray dst, Size ksize,
double sigmaX, double sigmaY = 0,
int borderType = BORDER_DEFAULT );
cvtColor(src, g_src, COLOR_BGR2GRAY);
GaussianBlur(g_src, Gr_g1, Size(5, 5), 0, 0);
GaussianBlur(Gr_g1, Gr_g2, Size(5, 5), 0, 0);
不同强度下的高斯模糊结果之间的差可以描述图像中的高频信息,成DoG图像:
#include
#include
using namespace std;
using namespace cv;
int main() {
Mat src, dst;
src = imread("girl6.jpg");
if (!src.data)
{
printf("Wrong with Loading img!\n");
return -1;
}
Mat dst_pyrup, dst_pyrdown;
pyrDown(src, dst_pyrdown, Size(src.cols / 2, src.rows / 2));
pyrUp(src, dst_pyrup, Size(src.cols * 2, src.rows * 2));
Mat g_src, g_pyrup, g_pyrdown;
cvtColor(src, g_src, COLOR_BGR2GRAY);
cvtColor(dst_pyrup, g_pyrup, COLOR_BGR2GRAY);
cvtColor(dst_pyrdown, g_pyrdown, COLOR_BGR2GRAY);
Mat Gr_g1, Gr_g2, Gr_g_up, Gr_g_down;
GaussianBlur(g_src, Gr_g1, Size(5, 5), 0, 0);
GaussianBlur(Gr_g1, Gr_g2, Size(5, 5), 0, 0);//没有改变强度,只是单纯的两次滤波
subtract(Gr_g1, Gr_g2, dst,Mat());//两次图像求差
normalize(dst, dst, 255, 0, NORM_MINMAX);//归一化显示,DoG图像不太明显,做一个归一化显示
imshow("Gr_g1", Gr_g1);
imshow("g_src", g_src);
imshow("Gr_g2", Gr_g2);
imshow("DoG Image", dst);
GaussianBlur(g_pyrup, Gr_g_up, Size(3, 3), 0, 0);
GaussianBlur(g_pyrdown, Gr_g_down, Size(3, 3), 0, 0);
//imshow("dst_pyrdown", g_pyrup);
//imshow("dst_pyrup", g_pyrdown);
waitKey(0);
return -1;
}