Data overlay on image
Show older comments
Hello everyone! I need help with data overlay with an image. In detail, I have experimental data that I rework and insert into a (t,x) graph. In order to understand if the data is ok, this graph must be precisely overlaid on an image containing the same type of graph. In addition, I must be able to manage the matlab graph, i.e. the image must be a background. How can I do it? Thanks.
Accepted Answer
More Answers (2)
Abhiram
on 20 Feb 2025
1 vote
To overlay a plot over an image in MATLAB, you can follow the given steps:
- The ‘hold’ functionality in MATLAB can be used to retain the current image when adding new plots.
- Rescale the x and y variables of the plot to the width and height of the image.
You can refer to the MATLAB Answers post given below which addresses a question similar to yours:
Adarsh
on 20 Feb 2025
1 vote
I understand that you are trying to verify if the graph object you have built matches the one in the image by overlaying it precisely on the image.
In addition to the above answer, to precisely overlay the nodes in the correct positions, the correct coordinates of nodes in the picture are needed, you can leverage the MATLAB interactive tools to get the coordinates by following the steps below:
- Open the figure in a figure window (Undock)
- Navigate to “tools” tab
- Select the “Data Tips” option.
Now you can get the coordinates of any pixel on the image by clicking at the position.
Once you have an array of coordinates for each node you can leverage the “XData” and “YData” attributes of the plot function to plot the graph with nodes on the exact same positions and compare the two plots.
Below is an example code demonstrating the usage of “XData” and “YData” to precisely overlay graph on the image:
% Load and display the image
img = imread('example_image');
imshow(img);
hold on;
% Edges in a Graph
s = [1 2]; % Source nodes
t = [2 3]; % Target nodes
G = graph(s, t);
% Define node positions
nodePositions = [
143, 235; % Node 1 position (x, y)
287, 179; % Node 2 position (x, y)
479, 210 % Node 3 position (x, y)
];
% Plot the graph with specified node positions
h = plot(G, 'XData', nodePositions(:, 1), 'YData', nodePositions(:, 2), ...
'NodeColor', 'b', 'EdgeColor', 'g', 'LineWidth', 2, 'MarkerSize', 6);
hold off;
For more information regarding “graph” and “plot”, you can refer to the following documentation links:
- https://www.mathworks.com/help/matlab/ref/graph.html
- https://www.mathworks.com/help/matlab/ref/graph.plot.html
Hope it helps.
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!