Problem with saveas imshow and imwrite

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

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)?
I have a grayscale .tif image in which I want to make some processing. That's the first one of the two. This is 512x512 double. When I use the first code I save it as you see so I cannot save the processing I've made.
The second picture is what I take when I save the same image using the second code. BUT, when I try to make further process to THAT image that's 900x1200x3. So my question is how to save the second image(which is the correct one) as a 512x512 image.
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.
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?
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.
Guys thanks a lot for help I found what's going on. The problem is that when I used imshow(Image,[]) MATLAB showed the same image with different visualization. BUT the image remained the same. Unfortunately, I was given some tif images in which I try to remove the noise and then threshold them but I don't know why. Anyway, I'll have to give it a try again. I saved it using
Image=Image-min(Image(:)); % shift data such that the smallest element of A is 0
Image=Image/max(Image(:));
imwrite(Image,sprintf('image-background%d.tif',i))
Image Analyst thanks I will change the name. Why is it bad?
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!
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);

Sign in to comment.

Answers (1)

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

Please look above at the wuestion I've just edited it.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 19 Sep 2016

Commented:

on 29 Sep 2016

Community Treasure Hunt

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

Start Hunting!