DFT for 2D image using C++ - bad result












1














I try write DFT in C++ Qt. I use version with exp function. I tried many versions and always result is the same. First DFT for rows, and next for columns. Image 400 x 400 RGB. Is it correct? If not, what is wrong?



using namespace std;
complex<double> *tab;
tab = new complex<double>[640000];
complex<double> i(0.0, 1.0);

for (int j = 0; j < 400; ++j) {
for (int k = 0; k < 400; ++k) {
complex<double> sum(0.0, 0.0);

for (int n = 0; n < 400; ++n) {
int t = (j * 400 + n) << 2;
complex<double> arg = -i * ((2.0 * M_PI * k * n) / 400);
complex<double> val(tmp[t] / 255.0, 0.0); //tmp - unsigned char, values R G B

sum += val * exp(arg);
}
int t = (j * 400 + k) << 2;

tab[t] = sum;
tab[t + 1] = sum;
tab[t + 2] = sum;
}
}

for (int j = 0; j < 400; ++j) {
for (int k = 0; k < 400; ++k) {
complex<double> sum(0.0, 0.0);

for (int n = 0; n < 400; ++n) {
int t = (n * 400 + j) << 2;
complex<double> arg = -i * ((2.0 * M_PI * k * n) / 400);
sum += tab[t] * exp(arg);
}

int t = (k * 400 + j) << 2;
unsigned char p;

p = static_cast<unsigned char>(static_cast<int>(round(abs(sum))) % 256);

buf[t] = p;
buf[t + 1] = p;
buf[t + 2] = p;
}
}

delete tab;


I use image:



enter image description here



Result:



enter image description here










share|improve this question
























  • Did you scale the FFT properly?
    – Matthieu Brucher
    Nov 10 at 17:28






  • 2




    You may already know that to acheive "optical" image FFT (produced by lens) you'll need to swap quadrants (1st with 3rd, 2nd with 4th), so the 0th (yours brightest) term lays in the middle. Also I'd advise to use logarithmic scale for viewing as generally your result looks alright however since FFT may produce a wide range of values with high ones being orders of magnitude larger than low ones.
    – michelson
    Nov 10 at 18:14






  • 1




    To check your program, did you try to perform DFT then IDFT and compare the result with the input ?
    – Damien
    Nov 10 at 18:38


















1














I try write DFT in C++ Qt. I use version with exp function. I tried many versions and always result is the same. First DFT for rows, and next for columns. Image 400 x 400 RGB. Is it correct? If not, what is wrong?



using namespace std;
complex<double> *tab;
tab = new complex<double>[640000];
complex<double> i(0.0, 1.0);

for (int j = 0; j < 400; ++j) {
for (int k = 0; k < 400; ++k) {
complex<double> sum(0.0, 0.0);

for (int n = 0; n < 400; ++n) {
int t = (j * 400 + n) << 2;
complex<double> arg = -i * ((2.0 * M_PI * k * n) / 400);
complex<double> val(tmp[t] / 255.0, 0.0); //tmp - unsigned char, values R G B

sum += val * exp(arg);
}
int t = (j * 400 + k) << 2;

tab[t] = sum;
tab[t + 1] = sum;
tab[t + 2] = sum;
}
}

for (int j = 0; j < 400; ++j) {
for (int k = 0; k < 400; ++k) {
complex<double> sum(0.0, 0.0);

for (int n = 0; n < 400; ++n) {
int t = (n * 400 + j) << 2;
complex<double> arg = -i * ((2.0 * M_PI * k * n) / 400);
sum += tab[t] * exp(arg);
}

int t = (k * 400 + j) << 2;
unsigned char p;

p = static_cast<unsigned char>(static_cast<int>(round(abs(sum))) % 256);

buf[t] = p;
buf[t + 1] = p;
buf[t + 2] = p;
}
}

delete tab;


I use image:



enter image description here



Result:



enter image description here










share|improve this question
























  • Did you scale the FFT properly?
    – Matthieu Brucher
    Nov 10 at 17:28






  • 2




    You may already know that to acheive "optical" image FFT (produced by lens) you'll need to swap quadrants (1st with 3rd, 2nd with 4th), so the 0th (yours brightest) term lays in the middle. Also I'd advise to use logarithmic scale for viewing as generally your result looks alright however since FFT may produce a wide range of values with high ones being orders of magnitude larger than low ones.
    – michelson
    Nov 10 at 18:14






  • 1




    To check your program, did you try to perform DFT then IDFT and compare the result with the input ?
    – Damien
    Nov 10 at 18:38
















1












1








1







I try write DFT in C++ Qt. I use version with exp function. I tried many versions and always result is the same. First DFT for rows, and next for columns. Image 400 x 400 RGB. Is it correct? If not, what is wrong?



using namespace std;
complex<double> *tab;
tab = new complex<double>[640000];
complex<double> i(0.0, 1.0);

for (int j = 0; j < 400; ++j) {
for (int k = 0; k < 400; ++k) {
complex<double> sum(0.0, 0.0);

for (int n = 0; n < 400; ++n) {
int t = (j * 400 + n) << 2;
complex<double> arg = -i * ((2.0 * M_PI * k * n) / 400);
complex<double> val(tmp[t] / 255.0, 0.0); //tmp - unsigned char, values R G B

sum += val * exp(arg);
}
int t = (j * 400 + k) << 2;

tab[t] = sum;
tab[t + 1] = sum;
tab[t + 2] = sum;
}
}

for (int j = 0; j < 400; ++j) {
for (int k = 0; k < 400; ++k) {
complex<double> sum(0.0, 0.0);

for (int n = 0; n < 400; ++n) {
int t = (n * 400 + j) << 2;
complex<double> arg = -i * ((2.0 * M_PI * k * n) / 400);
sum += tab[t] * exp(arg);
}

int t = (k * 400 + j) << 2;
unsigned char p;

p = static_cast<unsigned char>(static_cast<int>(round(abs(sum))) % 256);

buf[t] = p;
buf[t + 1] = p;
buf[t + 2] = p;
}
}

delete tab;


I use image:



enter image description here



Result:



enter image description here










share|improve this question















I try write DFT in C++ Qt. I use version with exp function. I tried many versions and always result is the same. First DFT for rows, and next for columns. Image 400 x 400 RGB. Is it correct? If not, what is wrong?



using namespace std;
complex<double> *tab;
tab = new complex<double>[640000];
complex<double> i(0.0, 1.0);

for (int j = 0; j < 400; ++j) {
for (int k = 0; k < 400; ++k) {
complex<double> sum(0.0, 0.0);

for (int n = 0; n < 400; ++n) {
int t = (j * 400 + n) << 2;
complex<double> arg = -i * ((2.0 * M_PI * k * n) / 400);
complex<double> val(tmp[t] / 255.0, 0.0); //tmp - unsigned char, values R G B

sum += val * exp(arg);
}
int t = (j * 400 + k) << 2;

tab[t] = sum;
tab[t + 1] = sum;
tab[t + 2] = sum;
}
}

for (int j = 0; j < 400; ++j) {
for (int k = 0; k < 400; ++k) {
complex<double> sum(0.0, 0.0);

for (int n = 0; n < 400; ++n) {
int t = (n * 400 + j) << 2;
complex<double> arg = -i * ((2.0 * M_PI * k * n) / 400);
sum += tab[t] * exp(arg);
}

int t = (k * 400 + j) << 2;
unsigned char p;

p = static_cast<unsigned char>(static_cast<int>(round(abs(sum))) % 256);

buf[t] = p;
buf[t + 1] = p;
buf[t + 2] = p;
}
}

delete tab;


I use image:



enter image description here



Result:



enter image description here







c++ image-processing fft dft






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 17:58









eyllanesc

72.5k93054




72.5k93054










asked Nov 10 at 17:13









Przemysław Lewandowski

61




61












  • Did you scale the FFT properly?
    – Matthieu Brucher
    Nov 10 at 17:28






  • 2




    You may already know that to acheive "optical" image FFT (produced by lens) you'll need to swap quadrants (1st with 3rd, 2nd with 4th), so the 0th (yours brightest) term lays in the middle. Also I'd advise to use logarithmic scale for viewing as generally your result looks alright however since FFT may produce a wide range of values with high ones being orders of magnitude larger than low ones.
    – michelson
    Nov 10 at 18:14






  • 1




    To check your program, did you try to perform DFT then IDFT and compare the result with the input ?
    – Damien
    Nov 10 at 18:38




















  • Did you scale the FFT properly?
    – Matthieu Brucher
    Nov 10 at 17:28






  • 2




    You may already know that to acheive "optical" image FFT (produced by lens) you'll need to swap quadrants (1st with 3rd, 2nd with 4th), so the 0th (yours brightest) term lays in the middle. Also I'd advise to use logarithmic scale for viewing as generally your result looks alright however since FFT may produce a wide range of values with high ones being orders of magnitude larger than low ones.
    – michelson
    Nov 10 at 18:14






  • 1




    To check your program, did you try to perform DFT then IDFT and compare the result with the input ?
    – Damien
    Nov 10 at 18:38


















Did you scale the FFT properly?
– Matthieu Brucher
Nov 10 at 17:28




Did you scale the FFT properly?
– Matthieu Brucher
Nov 10 at 17:28




2




2




You may already know that to acheive "optical" image FFT (produced by lens) you'll need to swap quadrants (1st with 3rd, 2nd with 4th), so the 0th (yours brightest) term lays in the middle. Also I'd advise to use logarithmic scale for viewing as generally your result looks alright however since FFT may produce a wide range of values with high ones being orders of magnitude larger than low ones.
– michelson
Nov 10 at 18:14




You may already know that to acheive "optical" image FFT (produced by lens) you'll need to swap quadrants (1st with 3rd, 2nd with 4th), so the 0th (yours brightest) term lays in the middle. Also I'd advise to use logarithmic scale for viewing as generally your result looks alright however since FFT may produce a wide range of values with high ones being orders of magnitude larger than low ones.
– michelson
Nov 10 at 18:14




1




1




To check your program, did you try to perform DFT then IDFT and compare the result with the input ?
– Damien
Nov 10 at 18:38






To check your program, did you try to perform DFT then IDFT and compare the result with the input ?
– Damien
Nov 10 at 18:38



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241411%2fdft-for-2d-image-using-c-bad-result%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241411%2fdft-for-2d-image-using-c-bad-result%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings