フィルターのクリア

How to insert image in MatLab

385 ビュー (過去 30 日間)
Emily Smith
Emily Smith 2016 年 10 月 3 日
編集済み: DGM 2023 年 2 月 20 日
I want to insert an image at (0,0) on a plot. How would I do this and how can I determine the size? And also do I need to convert the image to a png?
Thanks!

回答 (3 件)

Jakub Rysanek
Jakub Rysanek 2016 年 10 月 4 日
imread() can import whole bunch of graphical data types, including .jpg and .png. Conversion to .png, as you ask, is not necessary.
img = imread('filename.png');
image(img);
To alter the size/positioning of the image within your figure, you can touch the underlying axes object:
x = 0;
y = 0;
width = 0.5;% measured relative to the figure width
height = 0.5;% measured relative to the figure height
set(gca,'units','normalized','position',[x y width height])

Gareth Thomas
Gareth Thomas 2016 年 10 月 3 日
  1 件のコメント
Dr. C V Chandrashekara
Dr. C V Chandrashekara 2020 年 11 月 13 日
Thank you Mr.
Jakub Rysanek

サインインしてコメントする。


DGM
DGM 2023 年 2 月 20 日
編集済み: DGM 2023 年 2 月 20 日
There are a lot of views on this vague question, so maybe I should put an answer here that might be more complete.
% some data in a plot
t = linspace(0,pi,100);
y = sin(2*t);
plot(t,y); hold on; grid on
% an image
FG = imread('cameraman.tif');
% i'm going to expand the image to make sure it's RGB
% this avoids issues dealing with colormapped objects in the same axes
if size(FG,3) == 1
FG = repmat(FG,[1 1 3]);
end
% the origin of an image is the NW corner
% so in order for both the plot and image to appear right-side up, flip the image
FG = flipud(FG);
% insert an image in a particular region in the current axes
% this uses image(); similar can be done with imshow()/imagesc()
image(FG,'xdata',[0 pi/4],'ydata',[0 pi/4])
axis equal
To find the size of an image, use size(), or see this thread.
To save an image, use imwrite(). To save a screenshot of a figure as an image, use saveas(), exportgraphics(), or a combination of getframe(), frame2im(), and imwrite(). Again, if the image you want to save is already in the workspace, save it using imwrite(), not by taking a screenshot of it.
% save a screenshot of the plot with overlaid image
saveas(gcf,'myplot.png')
% save a copy of the modified foreground image
imwrite(FG,'mycman.png')
Avoid using JPG for anything, especially saving figures.

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by