How to imshow with Invisible figure in MATLAB running on Linux

时光毁灭记忆、已成空白 提交于 2019-12-17 19:30:35

问题


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?


回答1:


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




回答2:


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



回答3:


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');


来源:https://stackoverflow.com/questions/10948881/how-to-imshow-with-invisible-figure-in-matlab-running-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!