【OpenCV】图像金字塔

来源:互联网 发布:怎么开店淘宝店 编辑:程序博客网 时间:2024/06/13 11:08

Pyramid

   通过上采样upsample和下采样downsample实现图像金字塔。OpenCV相应函数为pyrUp()和pyrDown()。

void cv::pyrUp      (   InputArray      src,                        OutputArray     dst,                        const Size &    dstsize = Size(),                        int             borderType = BORDER_DEFAULT )   void cv::pyrDown    (   InputArray      src,                        OutputArray     dst,                        const Size &    dstsize = Size(),                        int             borderType = BORDER_DEFAULT )   


   常用的图像金字塔有高斯金字塔和拉普拉斯金字塔。
   本节介绍高斯金字塔:
   下采样:第1步、用高斯掩模进行卷积运算,第2步、删除偶数行和列。
   上采样:第1步、将图像的长宽放大为原来的两倍,用0值填充。第2步、用相同高斯掩模(再乘以4)进行卷积,以近似原先删除掉的值。


代码示例

#include "iostream"#include "opencv2/imgproc.hpp"#include "opencv2/imgcodecs.hpp"#include "opencv2/highgui.hpp"using namespace std;using namespace cv;char* window_name = "Pyramids Demo";int main(int argc, char** argv){    //cout << "[i] -> Zoom in \n[o] -> Zoom out \n[ESC] -> Close\n" << endl;    char* filename = "../data/lena.jpg";    Mat src = imread(filename);    if (src.empty()) { return -1; }    while(1)    {        imshow(window_name, src);        char c = (char)waitKey(0);        if (c == 27)            break;        else if (c == 'i')        {            pyrUp(src, src, Size(src.cols * 2, src.rows * 2));            printf("Zoom In : Image x 2 \n");        }        else if (c == 'o')        {            pyrDown(src, src, Size(src.cols / 2, src.rows / 2));            printf("Zoom Out : Image / 2 \n");        }    }    waitKey(0);    return 0;}


运行结果




原创粉丝点击