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 ?

Looking for reliable MATLAB assignment help? Our expert MATLAB tutors deliver high-quality, easy-to-understand solutions tailored to your academic needs. Whether you're studying at Monash University, the University of Sydney, UNSW, or the University of Melbourne, we provide trusted MATLAB assistance to help you excel. Contact us today for the best MATLAB solutions online and achieve academic success!

MATLAB Assignment Help Services

Personalized Tutoring: Get one-on-one guidance from our MATLAB experts. Whether you're tackling basic concepts or advanced algorithms, we provide clear, step-by-step explanations to help you master MATLAB with confidence.

Assignment Assistance: Struggling with tight deadlines or complex assignments? Our team offers end-to-end support, from problem analysis to code development and debugging, ensuring your assignments meet the highest academic standards.

Project Development: Need expert help with your MATLAB research project? We assist in designing and implementing robust solutions, covering project planning, data collection, coding, simulation, and result analysis.

Coursework Support: Enhance your understanding of MATLAB with our comprehensive coursework assistance. We help you grasp lecture concepts, complete lab exercises, and prepare effectively for exams.

Thesis and Dissertation Guidance: Incorporate MATLAB seamlessly into your thesis or dissertation. Our experts provide support for data analysis, modeling, and simulation, ensuring your research is methodologically sound and impactful.

Contact us on WhatsApp for MATLAB help

Contact us on Telegram for MATLAB solutions