1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| void speedy_convolution(const IplImage* A, double sigma, IplImage* C )
{
int dft_M = cvGetOptimalDFTSize( A->height+-1 );
int dft_N = cvGetOptimalDFTSize( A->width-1 );
IplImage * complexInput= cvCreateImage( cvGetSize(A), IPL_DEPTH_32F, 2);
IplImage * realInput= cvCreateImage( cvGetSize(A), IPL_DEPTH_32F, 1);
IplImage * imaginaryInput= cvCreateImage( cvGetSize(A), IPL_DEPTH_32F, 1);
IplImage* dft_A = cvCreateImage( cvSize(dft_M, dft_N), A->depth,2);
float* G1_ptr=NULL;
CvMat tmp;
// copy A to dft_A and pad dft_A with zeros
//
cvScale(A, realInput, 1.0, 0.0);
cvZero(imaginaryInput);
cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
cvGetSubRect( dft_A, &tmp, cvRect(0,0,A->width,A->height));
//cvCopy( A, &tmp );
cvCopy( complexInput, &tmp, NULL );
//cvGetSubRect(dft_A,&tmp,cvRect( A->width, 0, dft_A->width-A->width, A->height ));
//cvZero( &tmp );
if( dft_A->width > A->width )
{
cvGetSubRect( dft_A, &tmp, cvRect(A->width,0, dft_A->width - A->width, A->height));
cvZero( &tmp );
}
// no need to pad bottom part of dft_A with zeros because of
// use nonzero_rows parameter in cvDFT() call below
//
cvDFT( dft_A, dft_A, CV_DXT_FORWARD , complexInput->height );
IplImage* Kernel_real= cvCreateImage(cvSize(dft_A->width,dft_A->height), IPL_DEPTH_32F,1);
IplImage* Kernel_imaginary= cvCreateImage(cvSize(dft_A->width,dft_A->height), IPL_DEPTH_32F,1);
IplImage* Kernel_complex= cvCreateImage(cvSize(dft_A->width,dft_A->height), IPL_DEPTH_32F,2);
float X=0.0;
float Y=0.0;
int Xm=dft_A->height/2;
int Ym=dft_A->width/2;
for (int y = 0; y < dft_A->width; y++)
{
G1_ptr = (float *) (Kernel_real->imageData + (Kernel_real->widthStep *y));
for (int x = 0; x <dft_A->height; x++)
{
X=x-Xm;
Y=y-Ym;
(*G1_ptr) =exp(-1*(X*X+Y*Y)/(2*sigma*sigma)); //Verifier formule
G1_ptr++;
}
}
cvZero(Kernel_imaginary);
cvMerge(Kernel_imaginary,Kernel_real,NULL, NULL,Kernel_complex);
cvDFT( Kernel_complex, Kernel_complex, CV_DXT_FORWARD , complexInput->height );
cvMulSpectrums(dft_A, Kernel_complex, dft_A, 0 );
// calculate only the top part
cvDFT( dft_A, dft_A, CV_DXT_INV_SCALE, C->height );
cvSplit(dft_A,realInput, imaginaryInput, 0, 0 );
cvNamedWindow("realInput", 0);
cvShowImage("realInput",realInput);
} |
Partager