縮放圖像


一、主要函數介紹


1.1 Resize
函數功能:圖像大小變換

函數原型:
void cvResize( 

        const CvArr* src, 

        CvArr* dst, 

        int interpolation=CV_INTER_LINEAR 

);

參數說明:
src
    輸入圖像.
dst
    輸出圖像.
interpolation
    差值方法:
    • CV_INTER_NN - 最近鄰差值,
    • CV_INTER_LINEAR - 雙線性差值 (缺省使用)
    • CV_INTER_AREA - 使用象素關係重採樣。當圖像縮小時候,該方法可以避免波紋出現。當圖像放大時,類似於 CV_INTER_NN 方法..
    • CV_INTER_CUBIC - 立方差值.
函數 cvResize 將圖像 src 改變尺寸得到與 dst 同樣大小。若設定 ROI,函數將按常規支持 ROI.


二、示例程序代碼

#include "opencv/cv.h"
#include "opencv/highgui.h"

int main(int argc,char** argv)
{

	double fScale = 0.314;		//縮放比例 
	CvSize dst_imageSize;	//目標圖像尺寸	
	
	//讀取文件
	IplImage* src_image = cvLoadImage(argv[1]);
	IplImage* dst_image = NULL;
	
	//計算目標圖像的大小
	dst_imageSize.width = src_image->width * fScale;
	dst_imageSize.height = src_image->height * fScale;	

	//創建圖像並縮放
	dst_image = cvCreateImage(dst_imageSize,src_image->depth,src_image->nChannels);
	cvResize(src_image,dst_image,CV_INTER_AREA);
	
	//創建窗口
	cvNamedWindow("src_image",1);
	cvNamedWindow("dst_image",1);

	//在指定窗口顯示圖像
	cvShowImage("src_image",src_image);
	cvShowImage("dst_image",dst_image);
	
	//等待ESC按鍵事件
	while((cvWaitKey(0) != 27)){
	}
	
	//保存圖像
	cvSaveImage("dst_image.jpg",dst_image);

	//摧毀窗口並釋放內存
	cvDestroyWindow("src_image");
	cvDestroyWindow("dst_image");
	cvReleaseImage(&src_image);
	cvReleaseImage(&dst_image);

	return 0;
}


效果:





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章