# FFT at 3D Mat

Hello, I want to computing FFT at 3D matin C++: I have n images that I want to calculate FFT at z axe for each pixel(x,y), and as a result the n images of amplitude and phase... but I have no idea how to make that please help me.

```
cv::Mat phase;
cv::Mat magI;
cv::Mat I;
cv::Mat padded;
cv::Mat output;
cv::Mat ampIm;
I = imread(liste[0].toStdString().c_str(), CV_LOAD_IMAGE_GRAYSCALE);
int rows=I.rows;
int cols=I.cols;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
for(int k=0;k<liste.size();k++)
{
I = imread(liste[k].toStdString().c_str(), CV_LOAD_IMAGE_GRAYSCALE);
Vec3b color = I.at<Vec3b>(Point(i,j));
output.at<Vec3b>(Point(j+i*(rows-1),k))=color;
}
}
}
int m = cv::getOptimalDFTSize(output.rows );
int n = getOptimalDFTSize( output.cols ); // on the border add zero values
cv::copyMakeBorder(output, padded, 0, m - output.rows, 0, n - output.cols, BORDER_CONSTANT, Scalar::all(0));
cv::Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
cv::Mat complexI;
merge(planes, 2, complexI); // Add to the expanded another plane with zeros
dft(complexI, complexI,cv::DFT_ROWS|cv::DFT_COMPLEX_OUTPUT); // this way the result may fit in the source matrix
split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
cv::cartToPolar (planes[0],planes[1],magI,phase,true);
magI += Scalar::all(1); // switch to logarithmic scale
log(magI, magI);
for(int k=0;k<liste.size();k++){
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
Vec3b colorF =magI.at<Vec3b>(Point(j+i*(rows-1),k));
ampIm.at<Vec3b>(Point(i,j))=colorF;
}
}
//save ampIm;
}
```

Seriously, you need to understand what a FFT means before trying to compute things that make nonsense

I read a lot fo tutorials but they always show how to compute FFT at 1 image "mat" i didn't find FFT of 3D and what about FFT means I have Idea

That is the idea: FFT is for a single image. What you can do is to compute the FFT for each image and then compare them.

In other words, DFT "converts a finite list of equally spaced samples of a function into the list of coefficients of a finite combination of complex sinusoids, ordered by their frequencies, that has those same sample values. It can be said to convert the sampled function from its original domain (often time or position along a line) to the frequency domain." Does this fit your approach? Does it make sense what you want to do?

Maybe this could be interesting. Another idea that might help is to reformat the information in the manner that each position (x,y) is changing in time, so having a vector of size N, you can apply your FFT on it and have the modifications in time

YES thdrksdfthmn IT's probably near to my goal to applicate FFT on vector of size N (number of frame) at each (x,y) is changing in time but how in c++.

dft accepts vectors as input and output, so, just try it like that. I suppose you have seen this tutorial

I get the best idea to express may self and I try to compute the Fourier transform with respect to the time coordinate. I try to reshape my 3D cube into a 2D matrix, where the vertical coordinate is the pixel index (in the range [0, MN-1]) and the horizontal coordinate the time (range [0, T-1]), i.e. each row contains the values of one pixel in the sequence.and then apply the DFT routine along each row with the cv::dft() function and the flag CV_DFT_ROWS.in which, M x N is the size of each frame and T is the total number of frames . but the code didn't run I suupose because of big size of mat output. How can I resolve this thdrksdfthmn???

This is a nice challenge! How big gets your matrix? How are your matrix, aren't they sparse?

I definite my matrix like that and that change from sequence to other

I am asking about the values of M, N, and how many images you have? How bis is your matrix: M*N x T?