How to insert image in MatLab

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!

Answers (3)

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])
DGM
DGM on 20 Feb 2023
Edited: DGM on 20 Feb 2023
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.

5 Comments

I'm using this in R2024a and I'm getting the following error:
Error using image
Color data must be an m-by-n-by-3 or m-by-n matrix.
What am I doing wrong? What can I do?
Whatever array you are trying to image() is not something-by-something or something-by-something-by-3 .
There are quite rare cases where imread() can return a something-by-something-by-2 array (if the image was grayscale plus alpha)
There are more common cases where imread() can return something-by-something-by-4 array (if the image was TIFF and was CMYK or malformed RGBA).
There are cases where geotiffread() can return something-by-something-by 4 or more, if the data was hyperspectral. And of course if you used hypercube then the data will be something-by-something-by 4 or more.
You need to examine the size() of your data, and figure out the original intent.
If it was the (quite rare) grayscale+alpha case then you would strip off the alpha layer and use (a scaled version of it) in the 'AlphaData' option.
If it was CMYK then you would follow the instructions at https://www.mathworks.com/matlabcentral/answers/101194-how-can-i-convert-cmyk-image-to-rgb to convert CMYK to RGB.
If it was hyperspectral then you could try colorize
Richard
Richard on 14 Nov 2024
Edited: Richard on 14 Nov 2024
I'm actually exporting a pictogram from powerpoint into a file. It's only black and has no background. I could use different file types. I would like to use .svg for saving it to get a vector graphic.
I don't know what or even if this is an array and what to do with it.
Check to see if it is something-by-something-by-2 . If it is, then likely it is grayscale + alpha.
Gray = Grayscale_plus_alpha(:,:,1);
Alpha = im2double(Grayscale_plus_alpha(:,:,2));
image(Gray, 'AlphaData', Alpha)
You might be able to save it as an SVG, but even with third-party SVG import tools, only a subset of the possible object types in an SVG file can be read.
As Walter points out, image() (which is what's used internally by imagesc() and imshow()) only accepts MxN arrays (of numeric or logical class) or MxNx3 arrays (of float or unsigned integer numeric class). It's possible to get MxNx4 or MxNx1xF arrays from imread() without any special syntax or options.
If you can attach the image file and/or the code used to replicate the issue, we can tell you why.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 3 Oct 2016

Commented:

DGM
on 15 Nov 2024

Community Treasure Hunt

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

Start Hunting!