Problem with saveas imshow and imwrite
Show older comments
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 Comments
Geoff Hayes
on 19 Sep 2016
Efstathios - this question seems to be a continuation of http://www.mathworks.com/matlabcentral/answers/303037-problem-with-size-of-images. Please clarify what you mean by 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. The image is saved as a 512x512 array (which is expected and correct) but what is the data type? What do you want the data type to be? Since you are using im2bw to convert your RGB image to black and white, then this converted image will be of the logical data type. Do you want the data type to be 8-bit unsigned integer (which is the same data type as when you save the figure)?
Efstathios Kontolatis
on 20 Sep 2016
Adam
on 20 Sep 2016
Why can't you save the processed image using imwrite? What processing have you done on it? imwrite can save any image data if you paramaterise it in the right way.
Geoff Hayes
on 20 Sep 2016
Efstathios - please show us how you have obtained the variable Image. Include all of the code and the input file so that we can get a better idea of what is happening.
As for the two images that you have shown above, is the first one generated from the saved tif file when you use imwrite?
Image Analyst
on 20 Sep 2016
Second time Geoff has asked, and I was wondering too. imwrite() is what you want, but we need to figure out why (the very badly-named) "Image" variable is a logical when you call imwrite(). This is what you need to fix. If you want and expect Image to be a gray scale image with a range of gray levels, and not a logical variable with only two (pure black and pure white) gray levels, then you need to investigate why/how Image got to be logical in the first place.
Efstathios Kontolatis
on 29 Sep 2016
Adam
on 29 Sep 2016
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
on 29 Sep 2016
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);
Answers (1)
Image Analyst
on 19 Sep 2016
1 vote
imwrite() saves the image variable, while saveas() saves a screenshot, which may not be the same number of pixels as the underlying image.
1 Comment
Efstathios Kontolatis
on 20 Sep 2016
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
