Data overlay on image

8 visualizaciones (últimos 30 días)
Me
Me el 18 de Feb. de 2025
Respondida: Karan Singh el 20 de Feb. de 2025
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.

Respuesta aceptada

Karan Singh
Karan Singh el 20 de Feb. de 2025
Hi @Me,
You can try overlaying your experimental data on a background image in MATLAB by displaying the image with the proper coordinate system and then plotting your data on top. First, read in your image using "imread". When displaying the image with "imshow", use the 'XData' and 'YData' properties to assign the correct axis limits. This ensures that the image aligns with your (t,x) coordinate system. After displaying the image, use the hold on command so that your plot isn’t erased. Then, plot your experimental data on top of the image. Here is a dummy data code for the same-
clear; close all; clc;
%% 1. Load/Create Background Image
bgImage = rand(300,400,3);
% Define the coordinate limits that match your (t,x) graph
xmin = 0;
xmax = 10;
ymin = 0;
ymax = 5;
%% 2. Display the Background Image with Correct Axes
figure;
imshow(bgImage, 'XData', [xmin xmax], 'YData', [ymin ymax]);
axis on; % Show axis lines and ticks
set(gca, 'YDir', 'normal'); % Ensure that the y-axis increases upward
title('Data Overlay on Background Image');
xlabel('Time (s)');
ylabel('Measurement Value');
%% 3. Generate Dummy Experimental Data
% Create a time vector and generate a sine wave with noise as dummy data
t = linspace(xmin, xmax, 100);
dummyData = 2.5 + sin(2*pi*t/10) + 0.2*randn(1,100); % Example experimental data
%% 4. Overlay the Dummy Data on the Background Image
hold on;
plot(t, dummyData, 'r-', 'LineWidth', 2); % Plot the data in red with a thicker line
hold off;
Karan

Más respuestas (2)

Abhiram
Abhiram el 20 de Feb. de 2025
Hi @Me,
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
Adarsh el 20 de Feb. de 2025
Hi @Me,
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:
  1. Open the figure in a figure window (Undock)
  2. Navigate to “tools” tab
  3. 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:
  1. https://www.mathworks.com/help/matlab/ref/graph.html
  2. https://www.mathworks.com/help/matlab/ref/graph.plot.html
Hope it helps.

Categorías

Más información sobre Graph and Network Algorithms en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by