Data overlay on image
显示 更早的评论
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.
采纳的回答
更多回答(2 个)
Abhiram
2025-2-20
1 个投票
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
2025-2-20
1 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
