圖像縮放

高深的搞不動了搞些簡單的

// Gauss.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "cv.h"
#include "highgui.h"
#include <iostream>;
#include <ctime>
using namespace std;
void resizeImage(IplImage * src,IplImage *dst,bool largescale);
int _tmain(int argc, _TCHAR* argv[])
{

	clock_t clkbegin;
	clock_t clkend;
	IplImage * src =cvLoadImage("D:/lena.jpg");

	 IplImage*dst= cvCreateImage(cvSize(src->width/2,src->height/2),src->depth,3);
	 clkbegin=clock();
	  resizeImage(src,dst,0);
	  clkend=clock();
	  cout<<clkend-clkbegin<<endl;
    cvNamedWindow("src");
	cvShowImage("src",dst);

	cvWaitKey(0);
	return 0;
}


void resizeImage(IplImage * src,IplImage *dst,bool largescale)
{
	int rows;rows = src->height;
	int cols;cols = src->width;
#define Imr(ROW,COL) ((uchar*)(src->imageData+(ROW)*src->widthStep))[(COL)*src->nChannels+0]
#define Img(ROW,COL) ((uchar*)(src->imageData+(ROW)*src->widthStep))[(COL)*src->nChannels+1]
#define Imb(ROW,COL) ((uchar*)(src->imageData+(ROW)*src->widthStep))[(COL)*src->nChannels+2]
#define newImr(ROW,COL) ((uchar*)(dst->imageData+(ROW)*dst->widthStep))[(COL)*dst->nChannels+0]
#define newImg(ROW,COL) ((uchar*)(dst->imageData+(ROW)*dst->widthStep))[(COL)*dst->nChannels+1]
#define newImb(ROW,COL) ((uchar*)(dst->imageData+(ROW)*dst->widthStep))[(COL)*dst->nChannels+2]
	if(!largescale)
	{
	  //降採樣;
		cout<<"縮小圖像"<<endl;
		for(int i=0;i<rows/2;i++)
		{
		 for (int j=0;j<cols/2;j++)
		 {
			 newImr(i,j)=Imr(i*2,j*2);
			 newImg(i,j)=Img(i*2,j*2);
			 newImb(i,j)=Imb(i*2,j*2);
		 }
		}


	}
	else
	{
	  //線性內插;
		cout<<"放大圖像"<<endl;
		
		//原始值的點;
		for (int i=0;i<rows*2;i++)
		{
			for(int j=0;j<cols*2;j++)
			{
				newImr(i,j)=Imr(i/2,j/2);
				newImg(i,j)=Img(i/2,j/2);
				newImb(i,j)=Imb(i/2,j/2);
			}
		}
		//插值;
		for (int i=0;i<rows*2;i+=2)
		{
			for (int j=1;j<cols*2;j+=2)
			{
				newImr(i,j)=0.5*(Imr(i/2,j/2)+Imr(i/2,j/2+1));
				newImg(i,j)=0.5*(Img(i/2,j/2)+Img(i/2,j/2+1));
				newImb(i,j)=0.5*(Imb(i/2,j/2)+Imb(i/2,j/2+1));
			}
		}
		for (int i=1;i<rows*2;i+=2)
		{
			for (int j=0;j<cols*2;j+=2)
			{
				newImr(i,j)=0.5*(Imr(i/2,j/2)+Imr(i/2+1,j/2));
				newImg(i,j)=0.5*(Img(i/2,j/2)+Img(i/2+1,j/2));
				newImb(i,j)=0.5*(Imb(i/2,j/2)+Imb(i/2+1,j/2));
			}
		}
		for (int i=1;i<rows*2;i+=2)
		{
			for (int j=1;j<cols*2;j+=2)
			{
				newImr(i,j)=0.25*(Imr(i/2-1,j/2-1)+Imr(i/2+1,j/2+1)+Imr(i/2-1,j/2+1)+Imr(i/2+1,j/2-1));
				newImg(i,j)=0.25*(Img(i/2-1,j/2-1)+Img(i/2+1,j/2+1)+Img(i/2-1,j/2+1)+Img(i/2+1,j/2-1));
				newImb(i,j)=0.25*(Imb(i/2-1,j/2-1)+Imb(i/2+1,j/2+1)+Imb(i/2-1,j/2+1)+Imb(i/2+1,j/2-1));
			}
		}

	}

	//dst =cvCreateImage(,)
   
}


發佈了52 篇原創文章 · 獲贊 18 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章