載入一幅圖像分別使用cvResize() 和 cvPyrDown()進行圖片的三次縮放,比較結果;
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <stdio.h>
#include <cstdlib>
using namespace std;
using namespace cv;
IplImage * doPryDown(IplImage * in,int filter=CV_GAUSSIAN_5x5)
{
//assert(in->width%2==0 && in->height%2==0);
IplImage * out=cvCreateImage(cvSize(in->width/2,in->height/2),in->depth,in->nChannels);
cvPyrDown(in,out,CV_GAUSSIAN_5x5);//filter=7 目前只支持CV_GAUSSIAN_5x5
return (out);
}
int main(int argc, const char * argv[]) {
/*1.載入圖像,並轉化爲灰度圖像*/
const char filename[] = "/Users/linwang/Downloads/30.png";
IplImage * Img = cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE);
cvShowImage("Gray-Lena", Img);
/*2. 將這幅圖像縮小三次,並顯示結果*/
double fscale = 0.5;
CvSize czSize;
IplImage * dst = 0;
for(int i = 0;i<3;i++)
{
czSize.width = Img->width * pow(fscale, i+1);
czSize.height = Img->height * pow(fscale, i+1);
dst = cvCreateImage(czSize, Img->depth, Img->nChannels);
cvResize(Img, dst);
}
cvShowImage("Gray-Lena", dst);
/*3.利用cvPryDown()函數在原始圖像上進行三次降採樣操作*/
int count = 3;
while (count>0) {
IplImage * out = doPryDown(Img);
Img = cvCloneImage(out);
count--;
}
cvShowImage("PyrDown", Img);
cvWaitKey(0);
return 1;
}
Resize():將圖像精確的轉換爲目標圖像的尺寸,需要用到一些插值方法;
cvPyrdown會失真。