sagar
sagar

Reputation: 201

Unable to write images of type 'double'

I have obtained Fourier spectrum of an image and want to save the result image into the folder as specified in my code.

but as result image is of type "double",i am unable save it.i am getting blank image at the given folder. what should i do?

Here is my code

clc;
clear all;
close all;
i=imread('D:\pendrive\test1.jpg');
i=rgb2gray(i);
subplot(2,2,1),imshow(i);title('Test Image');

f=fftn(i);
magnitude=abs(f);
phase=angle(f);

f1=log(1+fftshift(f));
subplot(2,2,2),imshow(f1,[]);title('Fourier spectrum');
a=strcat('C:\Users\Public\Pictures\Sample Pictures\results\Fourier spectrum\','.jpg');
imwrite(f1,a);

Upvotes: 1

Views: 232

Answers (1)

cifz
cifz

Reputation: 1078

If imwrite receive a double image it assume that the range is between 0 and 1. This means that if you have something less than 0, they're scaled up to 0 (black), but most importantly in your case everything that is more than 1 will be scaled down to 1 (white).
This happens because what imwrite do when receiving a double image is simply

 yourImage .* 255

So that it ends up with the classical range for images that use 8bit per pixel per channel.

You can solve your problem by either scaling your values such as they are in the range [0...1] before you call imwrite. This can be performed easily this way:

yourImageScaled = (yourImage-min(yourImage(:)))./(max(yourImage(:))- min(yourImage(:)));

Or you can try a conversion to uint8 by simply doing

uint8(yourImage)

Note that depending on your values the uint8 conversion may as well produce a non-desired result (e.g. if your FT magnitude is between 2 and 20 everything will look almost black). For this reason you better perform a scaling of the resulting values such that the final result is better observable.

Because of this last remark, I suggest you the first option I've proposed, the scaling in the [0...1] range.

Upvotes: 1

Related Questions