How to Imshow with Invisible Figure in Matlab Running on Linux

How can I change figure edges of a figure created with imshow?

By default, saved figures have a white background. Ensure that the colors of the saved figure match the colors on the display by setting the InvertHardcopy property of the figure to 'off'.

Example:

A = rand(300, 300) > 0.1;

f = figure();
f.InvertHardcopy = 'off';
imshow(A);
title('Binary Image threshold 0.9');
saveas(f, 'test.png');

gives:

Sample Image

Alternatively, it is possible to set the visibility of the axes in the imshow and make the ticks empty:

A = rand(300, 300) > 0.1;

f = figure();
iptsetpref('ImshowAxesVisible', 'on');
imshow(A);
xticks({});
yticks({});
title('Binary Image threshold 0.9');
saveas(f, 'test.png');

that gives:

Sample Image

Source: Matlab Documentation

In MATLAB, how do I plot to an image and save the result without displaying it?

When you create the figure you set the Visibile property to Off.

f = figure('visible','off')

Which in your case would be

im = imread('image.tif');
f = figure('visible','off'), imshow(im, 'Border', 'tight');
rectangle('Position', [100, 100, 10, 10]);
print(f, '-r80', '-dtiff', 'image2.tif');

And if you want to view it again you can do

set(f,'visible','on')

MATLAB: Invisible figure handles behave differently on Windows and Linux?

I was unable to reproduce on either r2011a or r2012b, with Sun JAVA.

One workaround might be to filter based on visibility:

visibleChildren = findobj(get(h,'children'),'HandleVisibility','on')

Sounds like something specific to your install.

Matlab imshow update the displayed image

The trick is to specify the 'Parent' property in subsequent calls to imshow. For instance, if you have an image variable called 'myimg', and the handle to the axes in which the image is currently displayed is in a variable called 'imgaxes', the call would be:

imshow(myimg, 'Parent', imgaxes);

The thing you may need to figure out is the handle to the axes currently containing the image. If you capture the handle to the 'image' object when you call imshow the first time, this is easy. That would look like:

h = imshow(myimg);
imshow(myimg2, 'Parent', h.Parent);

The second call to imshow will place the second image into the same axes as the first.

how to plot the figure as improfile shows

The first issue is your x and y variables, they should hold the the coordinates of each point on you're line. However, you're specifying the same coordinate twice so it's not a line but a point which explains why M is only a 1x1x3 variable.

You're variable M holds a row vector for each of the colors if you're using RGB images. To get an exact plot like improfile does you therefore need to plot three lines instead of a single 3D line. Furthermore this requires the location on the line of each interpolated value, which you can probably get from improfile as well, just look at the source and help of the function for that.

im = imread( './sample.jpeg' );
imshow( im, [] );
x = [ 50, 80 ];
y = [ 50, 80 ];
[ M ] = improfile( im, x, y );

D = 1 : length( M );
plot( ...
D, M( :, :, 1 ), 'r' ...
, D, M( :, :, 2 ), 'g' ...
, D, M( :, :, 3 ), 'b' ...
);

How can I modify an image without showing it?

You can make a figure invisible with:

figure('Visible', 'off');

And then just write it out as Matlab fig via:

saveas(gcf, 'path/to/filename');

or using the print command to png is this case

print('-dpng', 'path/to/filename');

Similar question with good answers and explanations else where on stackoverflow

Update

Thanks to Steve for pointing to this undocumented matlab function

function so;
close all;
im = imread('cameraman.tif');
hfig = figure('Visible', 'off'), imshow(im, 'Border', 'tight');
for n=1:2
rectangle('Position', [20*n, 20*n, 50, 50], 'EdgeColor', 'g', 'LineWidth', 2)
hold on;
end

F = im2frame(zbuffer_cdata(gcf));
imwrite(F.cdata, 'test.png');

% Function copied from
% http://www.mathworks.com/support/solutions/en/data/1-3NMHJ5/?solution=1
% -3NMHJ5
%
function cdata = zbuffer_cdata(hfig)
% Get CDATA from hardcopy using zbuffer
% Need to have PaperPositionMode be auto
orig_mode = get(hfig, 'PaperPositionMode');
set(hfig, 'PaperPositionMode', 'auto');
cdata = hardcopy(hfig, '-Dzbuffer', '-r0');
% Restore figure to original state
set(hfig, 'PaperPositionMode', orig_mode);

How to save plotted image in MATLAB?

You can use saveas and use gcf to get the current figure:

saveas(gcf,'image.png');


Related Topics



Leave a reply



Submit