Navdeep
Navdeep

Reputation: 863

Finding the Discrete Fourier Transform of a 64 * 64 image

Following is the code to find DFT of a 64 * 64 image. It is a 3 channel image, so I am extracting Green channel out of it and working on it. I think the program is not giving the desired output. Please check it and let me know whether I am doing right or wrong?

This is the image i am using

This is the image i am using

    #include "stdafx.h"
    #include "opencv\highgui.h"
    #include "opencv\cv.h"

    using namespace cv;
    #define _USE_MATH_DEFINES
    #include<math.h>
    #include<iostream>
    #include<complex>


    int main()
    {
      Mat im = imread("joker.jpg");
        Mat im1(64, 64, CV_8UC1);
        Mat im2(64, 64, CV_64FC1);
        int from_to[] = {1,0};

        mixChannels(&im, 1, &im1, 1, from_to, 1);

        for (int u = 0; u < 64; u++)
        {
            for (int v = 0; v <64; v++)
            {
                double sum1 = 0, sum2 = 0;

                for (int x = 0; x < 64; x++)
                {

                    for (int y = 0; y < 64; y++)
                    {
    auto c = std::complex<double>(cos(2 * M_PI*(u*x / 64.0 + v*y / 64.0)), -sin(2 * M_PI*(u*x / 64.0 + v*y / 64.0)));
                        double temp2 = std::real(c);
                        double temp3 = std::imag(c);
                        sum1 = sum1 + im1.at<uchar>(x,y) * temp2;
                        sum2 = sum2 + im1.at<uchar>(x,y) * temp3;
                    }

                }
/* Finding the Frquency spectrum */
                double t = round(sqrt(pow(sum1, 2) + pow(sum2, 2)));
                im2.at<double>(u, v) = t;

           }

        }

        double min, max;
        minMaxIdx(im2, &min, &max);

    /*Scaling to a range between 0 - 255 */

        for (int i = 0; i < 64; i++)
        {
            for (int j = 0; j < 64; j++)
            {
                im2.at<double>(i, j) = 255 / (max - min)*(im2.at<double>(i, j)-min);
            }
        }
        namedWindow("output2");
        imshow("output2", im2);

waitkey(0);

}

Upvotes: 0

Views: 181

Answers (1)

berak
berak

Reputation: 39806

replacing your final conversion code with a (total unscientific):

Mat im3;
im2.convertTo(im3, CV_8U, .04);
imshow("output2", im3);

gives:

enter image description here

also, since it's a grayscale img, you could simplify the input :

Mat im1 = imread("joker.jpg", 0); // read as 1chan grayscale

and skip the mixChannels part


for the fun of it, let's try opencv's builtin dct ;)

Mat im = imread("joker.jpg", 0);
im.convertTo(im1, CV_64F, 1.0/255); // [0..1]

Mat im2;
dct(im1,im2);

Mat im3;
im2.convertTo(im3, CV_8U, 255); //[0..255]
imshow("output2", im3);

enter image description here

Upvotes: 1

Related Questions