3

I am using imagesc to get an integral image. However, I only manage to display it and then I have to save it by hand, I can't find a way to save the image from the script with imwrite or imsave. Is it possible at all?

The code:

image='C:\image.jpg';
in1= imread((image));
in=rgb2gray(in1);
in_in= cumsum(cumsum(double(in)), 2);
figure, imagesc(in_in); 
George
  • 769
  • 4
  • 11
  • 31

3 Answers3

6

You can also use the print command. For instance if you are running over multiple images and want to serialize them and save them, you can do something like:

% Create a new figure
figure (fig_ct)
% Plot your figure

% save the figure to your working directory
eval(['print -djpeg99 '  num2str(fig_ct)]);
% increment the counter for the next figure
fig_ct = fig_ct+1;

where fig_ct is just a counter. If you are interested in saving it in another format different than jpeg take a look at the documentation, you can do tiff, eps, and many more.

Hope this helps

tmpearce
  • 12,523
  • 4
  • 42
  • 60
Malife
  • 303
  • 2
  • 6
4

I believe your problem may be with the fact that you are saving a double matrix that is not on the range of [0 1]. If you read the documentation, you'll see that

If the input array is of class double, and the image is a grayscale or RGB color image, imwrite assumes the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values.

You can convert it yourself to a supported type (that's logical, uint8, uint16, or double) or get it in the range [0 1] by, for example, dividing it by the max:

imwrite (in_in / max (in_in(:)), 'out.jpg');

You may still want to further increase the dynamic range of the image you saved. For example, subtract the mininum before dividing by the max.

in_in = in_in - min (in_in(:));
in_in = in_in / max (in_in(:));
imwrite (in_in, 'out.jpg');

If you want exactly what imagesc displays

The imagesc function scales image data to the full range of the current colormap.

I don't know what exactly does it mean exactly but call imagesc requesting with 1 variable, and inspect the image handle to see the colormap and pass it to imwrite().

majkel.mk
  • 428
  • 4
  • 13
carandraug
  • 12,938
  • 1
  • 26
  • 38
1

I'm a very new programmer, so apologies in advance if this isn't very helpful, but I just had the same problem and managed to figure it out. I used uint8 to convert it like this:

imwrite(uint8(in_in), 'in_in.jpg', 'jpg');
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Serena
  • 11
  • 2