OpenCv学习笔记(一):图像金字塔之初步理解

来源:互联网 发布:做数据分析工作知乎 编辑:程序博客网 时间:2024/04/30 15:51

      所谓图像金字塔,其实说起来很简单,只不过被一些所谓的玩弄学术的人弄的比较学术了而已,其实就是一句话,就是在不同的图像尺寸下对图像进行处理。比如图像本身是1024*768的,先在原图像上进行处理,再把图像缩小到512*384,处理,再降成256*192的,再处理。依次类推。而且在OpenCv中有现成的函数pyrDown供使用,如果提升的话可以用pyrUp,由于比较简单,不解释。不过理解图像金塔绝对是理解人眼特性,从事机器学习的入门级任务!现提供示例源代码如下:

#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// Global variables
Mat src, dst, tmp;

char* window_name = "Pyramids Demo";


/**
* @function main
*/
int main( int argc, char** argv )
{
 /// General instructions
 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" );

 /// Test image - Make sure it s divisible by 2^{n}
 src = imread( "E:\\OPENCV\\opencv\\samples\\cpp\\tutorial_code\\images\\chicky_512.png" );
 cvtColor(src,src,CV_RGB2YCrCb);
 if( !src.data )
 { printf(" No data! -- Exiting the program \n");
 return -1; }
 vector<Mat> mv;
 split(src,mv);
 //tmp = src; 
 //dst = tmp;
 tmp=mv[0];
 dst=tmp;
 /// Create window
 namedWindow( window_name, CV_WINDOW_AUTOSIZE );
 //imshow( window_name, dst );
 imshow( window_name, mv[0] );
 waitKey();
 for(int i=0;i<3;i++)
 {
  pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
  tmp = dst;
  printf( "** Zoom Out: Image / 2 \n" );
 }
 imshow( window_name, dst );
 waitKey();
 for(int i=0;i<3;i++)
 { pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
 tmp = dst;
 printf( "** Zoom In: Image x 2 \n" );
 }
 dst=mv[0]-tmp;
 //cvtColor(dst,dst,CV_RGB2GRAY);
 //normalize(dst,dst,0,1);
    imshow( window_name, dst);
 waitKey(0);
    return 0;
}