1
$\begingroup$

I am beginer in image processing. Any signal whether it is 1D,2D or any multidimensional signal can be represented using combination of number of sine and cosine wavesforms (harmonics). Similerly any image can be termed as a function of sinusoidal signals.

I want to see individual pattern for the number of waveforms/harmonics present in an image.for example the following link contains few of them http://www.cs.toronto.edu/~rfm/factored/filters_out.png . So is there any code in Matlab/ OpenCV or results to understand easily these components(sine and cosine functions(harmonics)) present in any image?

$\endgroup$
2
  • $\begingroup$ I wouldn't say that the Fourier transform was "vital" to image processing. It's one technique, which is useful for certain things. $\endgroup$
    – Simon B
    Apr 23, 2015 at 14:10
  • $\begingroup$ @Simon B ok,You may be right but this thing is not really important in the question. :-) $\endgroup$
    – devraj
    Apr 23, 2015 at 14:39

2 Answers 2

4
$\begingroup$

this questions is asked fairly often. If you don't have a grasp of fft in 1d, higher dimensions may be difficult to grasph. But logically it makes a lot of sense once you see it What does frequency domain denote in case of images?

basically sharp changes (high contast noise) usually has high frequencies. and smoother gradients have low frequencies. just as a quick crash course

edit

sorry i thought the other post had code, its just code agnostic conceptual stuff. anyway ill tell you matlab (since your tag says matlab in it) and it is more familiar to me than openCV

function fft_im = createFFTIm(grayscale_im)
    fft_im = fft2(grayscale_im);

    figure()
    subplot(2,2,1:2);imshow(grayscale_im); title('original');
    %fftshift puts the 0 frequency in the middle of the plotting area, normally
    %it would be split, part of it in the 4 corners of the image
    %we add 1 because log(0) is -inf, which we dont want
    %we scale it jsut to show the colors better
    subplot(2,2,3);imagesc(log(1+abs(fftshift(fft_im)))); colormap(gray); 
    title('magnitude spectrum');

    subplot(2,2,4);imagesc(angle(fft_im));  colormap(gray);
    title('phase spectrum');
end

edit #2 i slightly modified the code

and here are two examples enter image description here

enter image description here notice with the chess example, there are visible peaks in the magnitude spectrum. Just like the regular fft, these peaks correspond to a sing wave of a specific frequency. Since out image is periodic there is are strong sines at the corresponding frequencies. You also notice since the image is near symmetric, the phase is also periodic. The phase is responsible for modeling the fact that an image is not symmetric

edit #3 this decomposes the figure into individual elements. In the code I limited it to a 4x4 subblock of pixels (the upper left portion of the figure) but you can change this all the way up to the size of the image (though for any large image this would be incredibly slow)

As in my comment, I processed the image one pixel at a time, and did an FFT2 on each of those 1 pixel images. The images themselves are still the same size as the original, it just has only one pixel value populated

function fft_im = decomposedcreateFFTIm(grayscale_im)
    %i only did a small 16 pixel sub block of the image, 4x4 pixels from 
    %the upper left corner of the image
    max_rows_to_process = 4;
    max_cols_to_process = 4;

    [m,n] = size(grayscale_im);

    %this is used to isolate pixels, for individual analysis
    zer_array = zeros(m,n);

    figure(1);title('magnitude')
    figure(2);title('phase')

    %i only did a small 16 pixel sub block of the image, 4x4 pixels from 
    %the upper right corner of the image
    m=4;n=4;

    %for all rows
    for ii=1:1:max_rows_to_process
        %for all columns
        for jj=1:1:max_cols_to_process
            curr_idx = (ii-1)*max_cols_to_process + jj;

            %creates an image with only one pixel
            indiv_pix_pic = zer_array;
            indiv_pix_pic(ii,jj) = grayscale_im(ii,jj);

            %does fft
            fft_im = fft2(indiv_pix_pic);

            %fftshift puts the 0 frequency in the middle of the plotting area, normally
            %it would be split, part of it in the 4 corners of the image
            %we add 1 because log(0) is -inf, which we dont want
            %we scale it jsut to show the colors better
            figure(2)
            subplot(max_rows_to_process,max_cols_to_process,curr_idx);
            imagesc(angle(fft_im));  colormap(gray);

            figure(1)
            subplot(max_rows_to_process,max_cols_to_process,curr_idx);
            imagesc(log(1+abs(fftshift(fft_im)))); colormap(gray); 
        end
    end
end

magnitude enter image description here

phase enter image description here

$\endgroup$
11
  • 1
    $\begingroup$ thank you sir for providing the link of answer but it doesn't totally fulfills requirements of my question .Especially the 2nd part of my question. I want to manually see the results for different images using the code $\endgroup$
    – devraj
    Apr 23, 2015 at 13:09
  • $\begingroup$ there's your code $\endgroup$
    – andrew
    Apr 23, 2015 at 16:38
  • $\begingroup$ as a side note, it DOES fulfill your requirements, you asked for matlab/ OpenCV code or results that solution had results jsut as requested. if you want code AND results, that is a different matter $\endgroup$
    – andrew
    Apr 23, 2015 at 16:42
  • $\begingroup$ @andrew can u add few results of your code outputs for others? $\endgroup$
    – sagar
    Apr 23, 2015 at 18:49
  • $\begingroup$ @andrew sir i have edited my question.Can add some results also? $\endgroup$
    – devraj
    Apr 23, 2015 at 19:00
-1
$\begingroup$

If you want to see the results of a Fourier transform, then it's not that difficult.

The FFT of an MxN image is an MxN array of complex numbers. Each complex value represents the magnitude and phase of a sine wave at one particular frequency - or more precisely one pair of frequencies (horizontal and vertical), since it's a 2D FFT. (Aside: a cosine wave is just a sine wave with a different phase, so we can treat the result as just being a sum of sine waves)

To see the magnitudes, take the absolute value of those complex numbers. Then re-scale the results to either 0 .. 255 or 0.0 ... 1.0 (the latter is probably better under Matlab). You now have an MxN array of numbers, which is just the same as an image. So display it as if it were an image.

If the results are pretty much black with a few white spots, you may need to apply some form of contrast enhancement (such as histogram equalisation) to see anything else.

The resulting "image" will be symmetrical top-to-bottom, and left-to-right. The value in the centre is the "DC" or zero frequency component. As you move away from the centre, you're looking at higher frequency sine waves. The ones at the edges are the highest possible frequencies - corresponding to alternating black and white pixels in the original image.

For most real-world images, the FFT result viewed as an image will be a largely featureless grey mush. That just tells you that the image contained a mixture of many different frequencies, or in other words that the objects in the image were of a wide range of different sizes and orientations.

$\endgroup$
5
  • 1
    $\begingroup$ I think this can't be answer for above question. The person is asking to find the wave component present in any respective image and see the each wave components separately with use of opencv or matlab code results (or outputs) $\endgroup$
    – sagar
    Apr 23, 2015 at 15:41
  • $\begingroup$ To be honest, it's more of an answer to the comment below Andrew's answer. I'm not entirely sure what the question really is. $\endgroup$
    – Simon B
    Apr 23, 2015 at 16:05
  • $\begingroup$ the person may want waveform plots for each basis functions as a output matlab/opencv code $\endgroup$
    – sagar
    Apr 23, 2015 at 18:31
  • $\begingroup$ @SimonB sir thank your for your feedback.But what I really wanted from the code output results is few of individual waves shown in the link $\endgroup$
    – devraj
    Apr 24, 2015 at 17:16
  • $\begingroup$ @SimonB cs.toronto.edu/~rfm/factored/filters_out.png $\endgroup$
    – devraj
    Apr 24, 2015 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.