3
votes

My goal is to:

  1. Create an invisible figure
  2. Using subplots, plot images on it, and then
  3. Save it without having it to open.

Thus, I am running the following code:

f = figure('Visible', 'off');
subplot(2, 2, 1), imshow(image1);
subplot(2, 2, 2), imshow(image2);
subplot(2, 2, 3), imshow(image3);
subplot(2, 2, 4), imshow(image4);
saveas(f, 'filename');

But I get the error:

Error using imshow (line xxx)
IMSHOW unable to display image.

This means that imshow is trying to display image. Is there a way to have imshow display image in the invisible figure and not try to pop up?

3
for what it's worth, the above runs just fine on my Windows machine..Amro
Thanks Amro. Searching through various forums, I knew this would work on Windows, but I am really interested to run this on Linux.Vinayak Agarwal
I guess you could file a bug report at MathWorks..Amro
What version of MATLAB are you using? Your code also runs fine on my Linux machine.reve_etrange
Hi, I am using MATLAB R2011b.Vinayak Agarwal

3 Answers

1
votes

This would work,

f = figure('Visible', 'off');
subplot(2, 2, 1), image(image1);
subplot(2, 2, 2), image(image2);
subplot(2, 2, 3), image(image3);
subplot(2, 2, 4), image(image4);
saveas(f, 'filename');

In case of gray scale images

f = figure('Visible', 'off');
subplot(2, 2, 1), image(image1),colormap(gray);
subplot(2, 2, 2), image(image2),colormap(gray);
subplot(2, 2, 3), image(image3),colormap(gray);
subplot(2, 2, 4), image(image4),colormap(gray);
saveas(f, 'filename');

imagesc() also can be used instead of image() function

0
votes

I get the same error when I run Matlab in nodisplay mode. My workaround was to draw a surface mesh with the image as texture mapping:

function varargout = imshow_nodisp(im)
% An IMSHOW implementation that works even when Matlab runs -nodisplay.
%
% Only we don't scale the figure window to reflect the image size. Consequently
% the ugly pixel interpolation is directly apparent. IMSHOW has it too, but it
% tries to hide it by scaling the figure window at once.
%
% Input arguments:
%  IM  HxWxD image.
%
% Output arguments:
%  HND  Handle to the drawn image (optional).
%
  [h,w,~] = size(im);

  x = [0 w; 0 w] + 0.5;
  y = [0 0; h h] + 0.5;
  z = [0 0; 0 0];

  hnd = surf(x, y, z, flipud(im), 'FaceColor','texturemap', 'EdgeColor','none');

  view(2);
  axis equal tight off;

  if nargout > 0
    varargout = hnd;
  end
end
0
votes

For anyone who lands here. After struggling with this, I managed to get support for this from mathworks. The solution is simple. You also need to set the axes visibility to off.

E.g.

f = figure('Visible', 'off');
a = axes('Visible','off');  ### <-- added this line of code
subplot(2, 2, 1), imshow(image1);
subplot(2, 2, 2), imshow(image2);
subplot(2, 2, 3), imshow(image3);
subplot(2, 2, 4), imshow(image4);
saveas(f, 'filename');