问题
My goal is to:
- Create an invisible figure
- Using subplots, plot images on it, and then
- 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