Saving images in PNG format, as grayscale, without white-spaces.

Hello,
I want to save a grayscale image in png format. I am using
saveas(gcf, outputname1, 'png')
but I keep having two problems: 1. No matter what I do, the saved image always ends up with white space on the sides, as if fitting a predefined space. I have already set the preferences as "tight", both in the "preferences" menu, and also at the beginning of my script with
iptsetpref('ImshowBorder','tight')
Yet, no matter what I do, I keep getting the white space on the sides of the image, but not on the top. I figured it has something to do with how I processed the images before (I did spatial frequency filtering based fft on a rectangular image. I think the image is made "square" for the filtering via padding, but at a later step I undo such padding by resizing the image to its original dimensions, but this does not help to eliminate the white space on the sides.
I know I can crop it, but I would like to understand why is matlab doing this, and how can I prevent it.
2. Also, even though the original image is a grayscale, when I save using saveas, I always end up with a RGB image. I tried other commands (imwrite, hgwrite, print, etc.), and while they (do save the images in grayscale, often the results are very distorted (darker, sometimes sharper or with a higher contrast). I have found that saveas does save the image exactly as it is displayed in matlab (which is what I need), except for the fact that it ends up as an RGB image.
Thanks in advance to whomever can help me understand and fix this!
So far, neither the command documentation nor the information available on forums and help sites has provided me with an answer.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 11 Jun. 2014

4 Stimmen

Don't save the figure at all. Save the image itself with imwrite(). It will save the actual pixel values. If you displayed it stretched, for example with the [] in imshow, then if you recall it and display it without stretching, it will look reduced in contrast. But the digital values will be identical. If you want what is displayed, which may not be the actual image values but the displayed image values, then use getframe() followed by imwrite().

10 Kommentare

Thanks, that solves the first problem. However, I keep getting RGB images with imwrite, even though the original image is in grayscale. I tried to refer to the colormap from getframe, but i get an error saying that the colormap should have 3 columns.
z=getframe(gcf)
imwrite(z.cdata, z.colormap, outputname1, 'png')
I have also used gray as argument (instead of z.colormap) and I get no errors, but the resulting image is still RGB
If there is no color information that you need (arrows, text, etc), then cast to grayscale before saving
grayImage = rgb2gray(z.cdata);
imwrite(grayImage , z.colormap, outputname1)
By the way, the 'png' input argument is not needed since it figures out the format from the filename.
Worked perfectly, thank you very much!
Is that procedure will also work for to convert text data into image??
No. You can use the insertText() function http://www.mathworks.com/help/vision/ref/inserttext.html to burn text into an image, and then use imwrite().
No,no I mean to say I have an image which store into .txt file if I want to save it any extension(.jpg,.png) what procedure I have to follow??
Image Analyst
Image Analyst am 8 Okt. 2016
Bearbeitet: Image Analyst am 8 Okt. 2016
To save an image array into a .txt file, use fprintf(). To save it into a standard image file format like PNG, use imwrite(). Does that answer your question (because I'm not sure what your question is)?
I am implementing the following commands
im1=load('single_image_128_128_0.txt');
imwrite(im1,'image.jpg');
A=imread('image.jpg');
imshow(A);
By using these i did not get the appropriate image.
That's because you used a JPG format, which you should never do when doing image analysis. It works fine with PNG. I tested it. Here is my code:
fontSize = 15;
originalImage = load('single_image_128_128_0.txt');
subplot(1,2,1);
imshow(originalImage);
title('Original Image', 'FontSize', fontSize);
% Save to disk
outputBaseFileName = 'image.PNG';
imwrite(originalImage, outputBaseFileName);
% Recall from disk:
recalledImage = imread(outputBaseFileName);
subplot(1,2,2);
imshow(recalledImage);
title('Recalled Image', 'FontSize', fontSize);
% delete(outputBaseFileName);
Hi, Thanks alot. This is working.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Aasim
Aasim am 8 Apr. 2015

7 Stimmen

This is a better way to do this : Frame will give you a structure which has cdata, i.e,. your data and colormap.. so what you need is your image. so write that on your file and enjoy your image without white borders.. the code is : f=getframe; imwrite(f.cdata,'ImageName.png');

9 Kommentare

really help, thanks a lot Aasim.
Thanks Aasim It Worked
Thanks a lot Aasim. Problem solved.
Thanks Asim
Thanks Aasim
For some reason this captures only a part of my plot. I didn't imagine spending an hour searching just to save a Matlab plot :(
Really useful for me
The solution worked for me
Thanks a lot

Melden Sie sich an, um zu kommentieren.

sbstn
sbstn am 22 Jul. 2016

0 Stimmen

The above suggestions did not work for me on Mac with Matlab 2015a. There's still extra space around the image. Saving an image without borders should not be a research problem in 2016. Matlab...you can do it!

2 Kommentare

What exactly are you saving, and what function are you calling? Are you calling imwrite()? Are you saving a matrix, or a figure? How did you get the thing you are saving? If you want help, you'll have to provide more info. I never have imwrite() save extra space around the image if that space wasn't already there before saving.
sbstn you can also try imshow. imshow(image, 'Border', 'tight') After this save your image in eps or png whatever you like. I hope this will work for you.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Images finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by