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:
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:
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
Signal Handling in Asm: Why am I Receiving Sigsegv When Invoking the Sys_Pause Syscall
What's the Difference Between ./Script.Sh and Bash Script.Sh
C Program Shows %Zu After Conversion to Windows
Read a File and Split Each Line into Multiple Variables
How to Set Process Group of a Shell Script
What Does 'Set -O Errtrace' Do in a Shell Script
Less Gets Keyboard Input from Stderr
Bash 'Swallowing' Sub-Shell Children Process When Executing a Single Command
Why Doing I/O in Linux Is Uninterruptible
Match a String That Contains a Newline Using Sed
Write-Only Mapping a O_Wronly Opened File Supposed to Work
How to Use Netcat for Windows to Send a Binary File to a Tcp Connection
Aws Lambda Permission Denied When Trying to Use Ffmpeg
Platform Independent Resource Management
Size() VS Ls -La VS Du -H Which One Is Correct Size
Can't Run Executable Linked with Libc
Posix Shared Memory and Semaphores Permissions Set Incorrectly by Open Calls