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?

Expert 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!

Our Comprehensive Matlab Assignment Help Services

Personalized Tutoring:Our team of MATLAB experts offers one-on-one tutoring sessions tailored to your specific needs. Whether you’re struggling with basic concepts or advanced algorithms, we provide clear, step-by-step guidance to help you understand and master MATLAB.

Assignment Assistance:Facing tight deadlines or complex assignments? We’re here to help! From initial problem analysis to code development and debugging, we offer full-spectrum support to ensure your assignments meet the highest standards.

Project Development: Need help with a research project? Our specialists can assist you in designing and implementing robust MATLAB solutions. We cover everything from project planning and data collection to coding, simulation, and result analysis.

Coursework Support: We provide comprehensive support for your coursework, helping you understand lectures, complete lab exercises, and prepare for exams. Our goal is to ensure you grasp the core principles and practical applications of MATLAB.

Thesis and Dissertation Guidance:Writing a thesis or dissertation? Our experts can assist you in incorporating MATLAB for data analysis, modeling, and simulation. We help you develop a strong methodological framework and ensure your research stands out.

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com