Here's some code I wrote to display the magnitude spectrum of an image:
orig_imdata = imread('Original_Image.png');
spec_orig = fft2(double(orig_imdata));
spec_orig2 = abs(spec_orig);
spec_img = fftshift(spec_orig2);
imshow(spec_img);
When I comment out the use of abs
and just fftshift
the image, I get an image, albeit with the phase and the magnitude. If I apply the abs
function right after using fftshift
, I get a blank result from imshow
. I need an image of the magnitude of the spectral analysis of my image.
Does anyone know what's going wrong here?
The image isn't "blank" at all. imshow
is designed so that any double
precision values that are less than 0 are displayed as black and any values larger than 1 are displayed as white. When computing the magnitude components of the image, you are producing double
precision images in your code.
Therefore, I highly suspect that because most of your components (if not all) are greater than 1, this gives you a visualization of an entirely white and therefore "blank" image. Now that we've found the problem, however you're not out of the woods yet. Simply scaling your components so that they fit in the range of [0,1]
won't help either. If you do this, the magnitude spectrum's DC component will probably be so large that it's overwhelming the rest of the magnitude components in your image. Therefore, you'll only see one white dot in the middle and the rest of the image will be black.
A common practice is to apply a log
operation to the magnitude spectrum for display, then rescaling the values so that they fit in the range of [0,1]
:
%// Your code
orig_imdata = imread('Original_Image.png');
spec_orig = fft2(double(orig_imdata));
spec_orig2 = abs(spec_orig);
spec_img = fftshift(spec_orig2);
%// New code
spec_img_log = log(1 + spec_img);
imshow(spec_img_log,[]);
In the log
operation, the huge dynamic range of values gets compressed to a smaller range, especially when the values get larger, and so naturally scaling to [0,1]
of this compressed range will give you better visual results.
The reason why you add 1 to every component then take the log
is to avoid the log(0)
operation. Should any magnitude components be zero, this will evaluate to log(1)
, which will become 0. Once you do this, you can use imshow(...,[])
to rescale the display so that the smallest magnitude component goes to 0 while the largest magnitude component goes to 1 of the log spectrum. Take note that I did not modify the original spectrum so you can do your processing on that. Also, the rescaling done by imshow
is completely done internally to the call. It does not rescale the data in anyway - it does this only for display and that's that.
Give that a go and see how it goes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With