Problem with saveas imshow and imwrite

12 次查看(过去 30 天)
Hi all,
I have a problem trying to save my images. I have a 512x512 tif figure. When I use this code(Image is my 512x512 uint8 image)
f = figure;set(f,'Visible','off');
imshow(Image,[]);
imwrite(Image,sprintf('image-background%d.tif',i));
the image that is saved is different from that shown which is logical because I ask the code to save the Image not the figure. However the saved image is 512x512. If I use this code
f = figure;set(f,'Visible','off');
imshow(Image,[]);
saveas(f,sprintf('image-background%d.tif',i));
the image is what I want but the dimensions are 900x1200x3. I want to combine the two solutions in a way that I will get the f figure as a 512x512 tif image. Is there a way to do so?
  8 个评论
Adam
Adam 2016-9-29
There is a builtin function called 'image'. Matlab will distinguish 'Image' separately from 'image' so your variable will not actually hide the function of that name as it would if you named it 'image', but you should still not name variables the same as functions, irrespective of capitalisation. Matlab is very variable on when it takes capitalisation into account and when it doesn't!
Image Analyst
Image Analyst 2016-9-29
If you use [], MATLAB scales your image to put the min at 0 and the max at 255.
If you don't use that, MATLAB will assume your floating point variable is in the range of 0-1. Anything less than or equal to 0 will show up as black, and anything 1 or more will show up as white, and things in the 0-1 range will be in gray scale.
If you have floating point numbers in the range 0-255 or whatever, then chances are lots of your pixels are above 1 or below 0 and so your image appears binary even though it is not.
If you have a floating point image and want to retain all the values, save it into a .mat file with save().
If you want to save it as an integer copy so that you can use a standard image format like .PNG, then use mat2gray() to scale it.
image8bit = uint8(255 * mat2gray(floatingPointImage));
imwrite('whatever.PNG', image8bit);

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2016-9-19
imwrite() saves the image variable, while saveas() saves a screenshot, which may not be the same number of pixels as the underlying image.

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by