MATLAB: Write compared images not figures?!

comparing imagesimwritesubplot

Hi. How can I write as images not figures?
I mean imagine I have image1, processed it and now it is image2. Now I want to show both of them in the same figure (or image). I use subplot or imshowpair to do so, but the problem is here:
If I want to save this final compared image, I have to use printf or saveas which both only save as JPEG and also changes the image size and info. What I want is saving it as an image (like the original ones.) I cannot use imwrite now! it gives empty figure!
Can anybody help me? Thanks so much! Steven

Best Answer

You can specify the format in saveas(); see http://www.mathworks.com/help/matlab/ref/saveas.html
saveas(FigureNumber, 'Filename.bmp', 'bmp')
for example.
You can print() to a graphics file; see the list in http://www.mathworks.com/help/matlab/ref/print.html . For JPEG, TIFF and PNG, you can also use the -r flag to specify the resolution. For example,
print(FigureNumber, '-dpng', 'Filename.png', '-r0')
Also search down in the documentation for the section "Printing Figures at Screen Size"
In order to imwrite() you need a single data matrix to write. You could construct that matrix by using, for example,
[firstImage, SecondImage]
or
[h, w, p] = size(firstImage);
if isdouble(firstImage)
fillval = 1;
else
fillval = 255;
end
spacer = ones(h, 50, p) * fillval; %a buffer of 50 pixels whitespace between the two
NewMatrix = [firstImage, spacer, SecondImage];
imwrite(NewMatrix, 'filename.png', 'png');