/**
* @brief test of fftw r2c transform
* @author maeda
* @date 2008/09/24
*/
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "fftw3.h"
 
#include "powerspectol.h"
 
#pragma comment( lib, "cv.lib" )
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "highgui.lib" )
#pragma comment( lib, "fftw3.lib" )
 
int main( void )
{
	IplImage *img     = NULL;
	IplImage *imgman  = NULL;
	IplImage *show    = NULL;
	IplImage *spectol = NULL;
	CvSize size;
	fftw_complex *out = NULL;
	fftw_plan plan    = NULL;
	int width,height;
	int i,j;
 
	// (1) load image (unsigned char)
	img = cvLoadImage( "lena.bmp", CV_LOAD_IMAGE_GRAYSCALE );
	if( !img ){
		printf("no image.\n");
		return false;
	}
 
	width  = img->width;
	height = img->height;
	size = cvSize( width/2+1, height );
 
	out     = (fftw_complex*)fftw_malloc( sizeof(fftw_complex) * (width/2 + 1) * height );
	spectol = cvCreateImage( size, IPL_DEPTH_64F, 1 );
	show    = cvCreateImage( size, IPL_DEPTH_8U, 1 );
	imgman  = cvCreateImage( cvSize( width, height ), IPL_DEPTH_64F, 1 );
 
	if( !imgman || !out || !spectol || !show ){
		printf("failed to allocate memory.\n");
		return false;
	}
 
	// (2) convert image (unsigned char->double)
	cvScale( img, imgman );
 
	plan = fftw_plan_dft_r2c_2d( height, width, (double*)imgman->imageData, out, FFTW_ESTIMATE );
	if( !plan ){
		printf("failed to create plan\n");
		return false;
	}
 
	// (3) fft (double->fftw_complex)
	fftw_execute(plan);
 
	// (4) get powerspectol (fftw_complex->double)
	maGetPowerSpectol2D( out, (double*)spectol->imageData, width/2+1, height );
 
	// (5) convert image (double->unsigned char)
	cvScale( spectol, show, 255 );
 
	// (6) show result
	cvNamedWindow( "window" );
	cvShowImage( "window", show );
	cvWaitKey(0);
	cvDestroyWindow( "window" );
 
	if( out     ) fftw_free( out );
	if( img     ) cvReleaseImage( &img );
	if( imgman  ) cvReleaseImage( &imgman );
	if( show    ) cvReleaseImage( &show );
	if( spectol ) cvReleaseImage( &spectol );
	if( plan    ) fftw_destroy_plan( plan );
 
	return true;
 
}
最終更新:2008年10月05日 14:12