Opencv中图像的DFT变换

直接给大家源码吧

void cvShiftDFT(CvArr* src_arr,CvArr* dst_arr)
{
	CvMat* tmp;
	CvMat  q1stub,q2stub;
	CvMat  q3stub,q4stub;
	CvMat  d1stub,d2stub;
	CvMat  d3stub,d4stub;
	CvMat* q1,*q2,*q3,*q4;
	CvMat* d1,*d2,*d3,*d4;

	CvSize size = cvGetSize(src_arr);
	CvSize dst_size = cvGetSize(dst_arr);
	int cy,cx;
	if(src_arr==dst_arr)
	{
		tmp = cvCreateMat(size.height/2,size.width/2,cvGetElemType(src_arr));
	}
	cx = size.width/2;
	cy = size.height/2;

	q1 = cvGetSubRect(src_arr,&q1stub,cvRect(0,0,cx,cy));
	q2 = cvGetSubRect(src_arr,&q2stub,cvRect(cx,0,cx,cy));
	q3 = cvGetSubRect(src_arr,&q3stub,cvRect(cx,cy,cx,cy));
	q4 = cvGetSubRect(src_arr,&q4stub,cvRect(0,cy,cx,cy));
	d1 = cvGetSubRect(src_arr,&d1stub,cvRect(0,0,cx,cy));
	d2 = cvGetSubRect(src_arr,&d2stub,cvRect(cx,0,cx,cy));
	d3 = cvGetSubRect(src_arr,&d3stub,cvRect(cx,cy,cx,cy));
	d4 = cvGetSubRect(src_arr,&d4stub,cvRect(0,cy,cx,cy));

	if(src_arr!=dst_arr)
	{
		cvCopy(q3,d1,0);
		cvCopy(q4,d2,0);
		cvCopy(q1,d3,0);
		cvCopy(q2,d4,0);
	}
	else
	{
		cvCopy(q3,tmp,0);
		cvCopy(q1,q3,0);
		cvCopy(tmp,q1,0);
		cvCopy(q4,tmp,0);
		cvCopy(q2,q4,0);
		cvCopy(tmp,q2,0);
	}
}
void MI_ImageHold::MI_FftImage()
{
	IplImage* im;
	IplImage* reallnput;
	IplImage* imageinaryInput;
	IplImage* complexInput;
	int dft_M,dft_N;
	CvMat* dft_A,tmp;
	IplImage* image_Re;
	IplImage* image_Im;
	IMAGE_double M,m;

	MI_Image* outputimage = cvCreateImage(cvSize(InputImage->width,InputImage->height),8,1);
	cvCvtColor(InputImage,outputimage,CV_BGR2GRAY);
	im = outputimage;
	reallnput = cvCreateImage(cvGetSize(im),IPL_DEPTH_64F,1);
	imageinaryInput = cvCreateImage(cvGetSize(im),64,1);
	complexInput = cvCreateImage(cvGetSize(im),64,2);

	cvScale(im,reallnput);
	cvZero(imageinaryInput);
	cvMerge(reallnput,imageinaryInput,NULL,NULL,complexInput);

	dft_M = cvGetOptimalDFTSize(im->height-1);
	dft_N = cvGetOptimalDFTSize(im->width-1);

	dft_A = cvCreateMat(dft_M,dft_N,CV_64FC2);
	image_Re = cvCreateImage(cvSize(dft_N,dft_M),64,1);
	image_Im = cvCreateImage(cvSize(dft_N,dft_M),64,1);

	cvGetSubRect(dft_A,&tmp,cvRect(0,0,im->width,im->height));
	cvCopy(complexInput,&tmp,NULL);
	if(dft_A->cols>im->width)
	{
		cvGetSubRect(dft_A,&tmp,cvRect(im->width,0,dft_A->cols-im->width,im->height));
	}
	else 
	{
		cvGetSubRect(dft_A,&tmp,cvRect(im->width,0,im->width-dft_A->cols,im->height));
	}
	//cvZero(&tmp);

	cvDFT(dft_A,dft_A,CV_DXT_FORWARD,complexInput->height);

	cvSplit(dft_A,image_Re,image_Im,0,0);
	cvPow(image_Re,image_Re,2.0);
	cvPow(image_Im,image_Im,2.0);
	cvAdd(image_Re,image_Im,image_Re,NULL);
	cvPow(image_Re,image_Re,0.5);
	cvAddS(image_Re,cvScalarAll(1.0),image_Re,NULL);
	cvLog(image_Re,image_Re);

	//MI_Image* outputimage = cvCreateImage(cvSize(InputImage->width,InputImage->height),8,InputImage->nChannels);
	cvShiftDFT(image_Re,image_Re);
	cvMinMaxLoc(image_Re,&m,&M,NULL,NULL,NULL);
	cvScale(image_Re,image_Re,1.0/(M-m),1.0*(-m)/(M-m));
	ResultImage = image_Re;
}

最后大家只需要显示ResultImage就是变换后的DFT图像

程序是《opencv基础篇》中的,不过源程序有问题,做了修改

希望对大家有帮助。


你可能感兴趣的:(Opencv中图像的DFT变换)