Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Why am I getting a blank image as my output?

Date: 2023-01-10 13:42:10

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?

Answer:

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.


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com