OpenCV2.2のcv::gpu::dftを使って4x4の複素行列をフーリエ変換、逆フーリエ変換をする例です。なかなかネット上にサンプルコードがなくて少し使い方に苦労したので、同じようにOpenCVのdftをGPU機能つきで使用したい方の参考になればと思います。
This is a sample code to apply fourier transform and inverse fourier transform with cv::gpu::dft function in OpenCV 2.2.
Source code
Result
This is a sample code to apply fourier transform and inverse fourier transform with cv::gpu::dft function in OpenCV 2.2.
Source code
#include<opencv2/gpu/gpu.hpp>
void cvGpuTest_DFT_IDFT(){
int w=4, h=4;
int len=w*h*2;
cv::Mat dst;
cv::gpu::GpuMat gpuMat;
//** make source data -----
float *srcData;
srcData=new float[len];
for(int i=0; i<len/2; i++){
srcData[i*2]=i;
srcData[i*2+1]=0;
}
cv::Mat srcMat(w, h, CV_32FC2, srcData);
//** Print Source Data ----
printf("SRC------------\n");
for(int y=0; y<h; y++){
for(int x=0; x<w; x++){
printf("%d %d: %f %f\n", x, y, srcMat.at<float>(y, x*2), srcMat.at<float>(y, x*2+1));
}
}
//** DFT ------------------
gpuMat = srcMat;
cv::gpu::dft(gpuMat, gpuMat, cv::Size(w, h));
dst=gpuMat;
//** Print DFT Result ------
printf("DFT------------\n");
for(int y=0; y<h; y++){
for(int x=0; x<w; x++){
printf("%d %d: %f %f\n", x, y, dst.at<float>(y,x*2), dst.at<float>(y, x*2+1));
}
}
//** IDFT -----------------
gpuMat=dst;
cv::gpu::dft(gpuMat, gpuMat, cv::Size(w, h), cv::DFT_INVERSE);
dst=gpuMat;
//** Print IDFT Result -----
printf("IDFT------------\n");
for(int y=0; y<h; y++){
for(int x=0; x<w; x++){
printf("%d %d: %f %f\n", x, y, dst.at<float>(y,x*2)/(w*h), dst.at<float>(y, x*2+1));
}
}
}
int main()
{
cvGpuTest_DFT_IDFT();
return 0;
}
Result
SRC------------ 0 0: 0.000000 0.000000 1 0: 1.000000 0.000000 2 0: 2.000000 0.000000 3 0: 3.000000 0.000000 0 1: 4.000000 0.000000 1 1: 5.000000 0.000000 2 1: 6.000000 0.000000 3 1: 7.000000 0.000000 0 2: 8.000000 0.000000 1 2: 9.000000 0.000000 2 2: 10.000000 0.000000 3 2: 11.000000 0.000000 0 3: 12.000000 0.000000 1 3: 13.000000 0.000000 2 3: 14.000000 0.000000 3 3: 15.000000 0.000000 DFT------------ 0 0: 120.000000 0.000000 1 0: -8.000000 8.000000 2 0: -8.000000 0.000000 3 0: -8.000000 -8.000000 0 1: -32.000000 32.000000 1 1: 0.000000 0.000000 2 1: 0.000000 0.000000 3 1: 0.000000 0.000000 0 2: -32.000000 0.000000 1 2: 0.000000 0.000000 2 2: 0.000000 0.000000 3 2: 0.000000 0.000000 0 3: -32.000000 -32.000000 1 3: 0.000000 0.000000 2 3: 0.000000 0.000000 3 3: 0.000000 0.000000 IDFT------------ 0 0: 0.000000 0.000000 1 0: 1.000000 -0.000000 2 0: 2.000000 0.000000 3 0: 3.000000 0.000000 0 1: 4.000000 0.000000 1 1: 5.000000 -0.000000 2 1: 6.000000 0.000000 3 1: 7.000000 0.000000 0 2: 8.000000 0.000000 1 2: 9.000000 -0.000000 2 2: 10.000000 0.000000 3 2: 11.000000 0.000000 0 3: 12.000000 0.000000 1 3: 13.000000 -0.000000 2 3: 14.000000 0.000000 3 3: 15.000000 0.000000