图像金字塔:高斯金字塔、拉普拉斯金字塔
高斯金字塔
图像下采样
void pyrDown( InputArray src, OutputArray dst,
const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
构建图像的高斯金字塔是解决尺度不确定性的一种常用方法。高斯金字塔是指通过下采样不断的将图像的尺寸缩小,进而在金字塔中包含多个尺度的图像,高斯金字塔的形式如图所示,一般情况下,高斯金字塔的最底层为图像的原图,每上一层就会通过下采样缩小一次图像的尺寸,通常情况尺寸会缩小为原来的一半,但是如果有特殊需求,缩小的尺寸也可以根据实际情况进行调整。由于每次图像的尺寸都缩小为原来的一半,图像尺缩小的速度非常快,因此常见高斯金字塔的层数为3到6层。
该函数首先将原图像与内核矩阵进行卷积,内核矩阵如式所示,之后通过不使用偶数行和列的方式对图像进行下采样,最终实现尺寸缩小的下采样图像。
拉普拉斯金字塔
图像上采样
void pyrUp( InputArray src, OutputArray dst,
const Size& dstsize = Size(), int borderType = BORDER_DEFAULT );
示例代码:
//
// Created by smallflyfly on 2021/6/10.
//
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main() {
// circle
// Mat im = imread("test.jpg");
Mat im;
VideoCapture cap(0);
if (!cap.isOpened()) {
cout << "camera read error" << endl;
}
cap >> im;
cap.release();
vector<Mat> gauss, laps;
int level = 2;
gauss.push_back(im);
for (int i = 0; i < level; ++i) {
Mat gas;
pyrDown(gauss[i], gas);
gauss.push_back(gas);
}
// construct lap pyramid
for (int i = gauss.size() - 1; i > 0 ; i--) {
Mat lap, upGauss;
if (i == gauss.size() - 1) {
// top level gauss down -> up
Mat down;
pyrDown(gauss[i], down);
pyrUp(down, upGauss);
} else {
pyrUp(gauss[i+1], upGauss);
}
cout << gauss[i].size << endl;
cout << upGauss.size << endl;
subtract(gauss[i], upGauss, lap);
laps.push_back(lap);
}
int i = 0;
for (i = 0; i < gauss.size()-1; ++i) {
imshow("gauss"+to_string(i), gauss[i]);
imshow("lap"+to_string(i), laps[i]);
}
imshow("gauss"+to_string(i), gauss[i]);
waitKey(0);
destroyAllWindows();
return 0;
}
发现一个问题 如果采样下去不是2的倍数 再上采样 尺寸就会变小 导致做差值的时候尺寸不一致错误