MATLAB Answers

Saved Matrix as Image does not give me the right pixels

6 views (last 30 days)
Hakan Östen
Hakan Östen on 27 May 2020
Commented: Subhadeep Koley on 28 May 2020
Hi there,
currently I am trying to save a (100 x 100) matrix in one .tif image.
When opening this image, it occurs that it has 1200 x 900 pixels eventhough it should be 100 x 100.
Nsub = 100
Q = reshape(Isub(:,:,is)(:,j),[Nsub,Nsub]); % Q has the size of 100 x 100.
% Output data as a figure
%set_x_axis = [ -Nsub Nsub];
%set_y_axis = set_x_axis
newmap = contrast(Q); % Change into greyscale
set(gca,'XTick',[], 'YTick', []);
save_in_folder = "C:/Users/TestImages";
file = sprintf("%s/%dmm/tif/%03d.tif", save_in_folder, List_of_foldernames(is),k);
saveas(imagesc(Q), file);
Has anyone an idea why?


Sign in to comment.

Answers (1)

Subhadeep Koley
Subhadeep Koley on 27 May 2020
Hakan, you are getting a 1200-by-900 image because you're saving the entire figure window in the tif file. If you only need to save the image, refer the code below,
nSub = 100;
% Creating a random image
img = rand(nSub);
% Visualize the image
newMap = contrast(img);
% Write the image and its associated colormap
imwrite(im2uint8(img), newMap, 'yourFile.tif')


Show 1 older comment
Subhadeep Koley
Subhadeep Koley on 28 May 2020
@ Hakan Östen If im2uint8 is not working then you can use the code below,
imwrite(rescale(img, 0, 255), newMap, 'yourFile.tif')
Hakan Östen
Hakan Östen on 28 May 2020
When Q is outputted as a figure in a popup, it works fine.
It seems that imwrite does not work properly. The image is stored empty. No information seems to get out.
Edited: I found the issue. The matrix did not have a scale of 0 255 pixels. I used
to fix it.
Thanks Subhadeep.
Subhadeep Koley
Subhadeep Koley on 28 May 2020
@ Hakan Östen You tried this?
imwrite(rescale(img, 0, 255), newMap, 'yourFile.tif')

Sign in to comment.


Translated by