How to specify the position of the displayed image?

8 views (last 30 days)
In MATLAB R2023b, I used the function "imread" to import an image file named cat.png, later, I wanted to show this image, is there any way to display the image at the designated position on the coordinate axis? For example, I want the coordinates for the upper left corner(or center point) of the image to be (1,3).
Can someone help me? Thanks very much!

Accepted Answer

Umar
Umar on 20 Aug 2025

Hi @Marvellous,

You can place an image at a specific position on the axes in MATLAB R2023b using the image() function with 'XData'and 'YData'. This allows you to set either the upper-left corner or center point at coordinates like (1,3). For example:

   % Upper-left corner at (1,3)
   image('XData', [1, 1 + width - 1], 'YData', [3, 3 + height - 1], 'CData', img);
   % Center at (1,3)
    image('XData', [1 - width/2, 1 + width/2], 'YData', [3 - height/2, 3 + height/2], 
    'CData', img);

This method gives precise control over the image placement while preserving axes coordinates and aspect ratio.

Reference:

<https://www.mathworks.com/help/matlab/ref/image.html Matlab Image Documentation >

The script I provided demonstrates:

1. Image placement by upper-left corner at (1,3). 2. Image placement by center at (1,3). 3. An overlay figure showing both placements with annotations and a legend for visual verification.

Implementation

   %% MATLAB Script: Display Image at a Designated Position with Annotations
   % Author: Umar
   % Date: 2025-08-19
   % Purpose: Show image at specific coordinates and annotate positions
   % Image path: /MATLAB Drive/Cat.png
   %% Step 1: Read the image
   imagePath = '/MATLAB Drive/Cat.png';
   img = imread(imagePath);
   [height, width, ~] = size(img);
   %% Step 2: Display image using upper-left corner
   upperLeftX = 1;
   upperLeftY = 3;
   figure('Name','Image Placement - Upper Left Corner','Color','w');
   image([upperLeftX, upperLeftX + width - 1], [upperLeftY, upperLeftY + height 
   - 1], img);
   set(gca, 'YDir', 'normal');
   axis equal;
  axis on;
  grid on;
  xlabel('X-axis');
  ylabel('Y-axis');
  title('Image placed using upper-left corner at (1,3)');
   % Annotate upper-left corner
   text(upperLeftX, upperLeftY, sprintf('  Upper-left corner (%.1f, %.1f)',   
   upperLeftX, upperLeftY), ...
    'Color','red','FontWeight','bold','FontSize',12,'VerticalAlignment','top');
   %% Step 3: Display image using center point
   centerX = 1;
   centerY = 3;
figure('Name','Image Placement - Center Point','Color','w');
image([centerX - width/2, centerX + width/2], [centerY - height/2, centerY +    
height/2], img);
set(gca, 'YDir', 'normal');
axis equal;
axis on;
grid on;
xlabel('X-axis');
ylabel('Y-axis');
title('Image placed using center point at (1,3)');
% Annotate center point
text(centerX, centerY, sprintf('  Center (%.1f, %.1f)', centerX, centerY), ...
  'Color','blue','FontWeight','bold','FontSize',12,'VerticalAlignment','middle');
%% Step 4: Optional overlay with annotations
figure('Name','Overlay Example with Annotations','Color','w');
hold on;
% Upper-left corner
image([upperLeftX, upperLeftX + width - 1], [upperLeftY, upperLeftY + height - 
1], img);
text(upperLeftX, upperLeftY, sprintf('  Upper-left (%.1f, %.1f)', upperLeftX,   
upperLeftY), ...
  'Color','red','FontWeight','bold','FontSize',12,'VerticalAlignment','top');
% Center point with transparency
alpha = 0.5;
imgDouble = im2double(img);
image([centerX - width/2, centerX + width/2], [centerY - height/2, centerY +   
height/2], imgDouble, 'AlphaData', alpha);
text(centerX, centerY, sprintf('  Center (%.1f, %.1f)', centerX, centerY), ...
  'Color','blue','FontWeight','bold','FontSize',12,'VerticalAlignment','middle');
% Set axes properties
set(gca, 'YDir', 'normal');
axis equal;
axis on;
grid on;
xlabel('X-axis');
ylabel('Y-axis');
title('Overlay: Upper-left and Center Placement with Annotations');
% Create dummy plot handles for legend to avoid warnings
h1 = plot(nan, nan, 's', 'MarkerFaceColor','red','MarkerEdgeColor','red');
h2 = plot(nan, nan, 's', 'MarkerFaceColor','blue','MarkerEdgeColor','blue');
legend([h1, h2], {'Upper-left corner','Center point'});
hold off;

Please see attached.

More Answers (1)

DGM
DGM on 19 Aug 2025
Both imshow() and image()/imagesc() allow you to specify the image extents in data coordinates directly.
% i assume you have something in the axes already
x = linspace(0,10,100);
y = 3*sin(x)+4;
plot(x,y); hold on
xlim([0 10])
ylim([0 8])
% and then you stick an image in it
% specify the extent of the image in data coordinates
% keep image aspect ratio in mind if necessary
inpict = imread('peppers.png');
imshow(inpict,'xdata',[1 6],'ydata',[3 7]) % corner at [1 3]
% undo the axis changes set by imshow()
axis on
grid on

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!