- https://www.mathworks.com/help/matlab/ref/getframe.html
- https://in.mathworks.com/help/matlab/ref/set.html
Plotting Data on Figure Changes Aspect Ratio
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have an image which I import to then plot over. I plot over it, and then export it. The original image is 2410*2512 and the resulting image is 2951*1549.
How do I keep the image in it's original aspect ratio/the same pixel dimensions as it is originally? The image that I plot over becomes stretched.
% Assign figure to plot over
figure();
imagesc(tolland_img);
xlabel(x_label);
ylabel(y_label);
title("E1P1 ")
colormap(color_map);
% Plot path on figure
hold on;
plot([E1P1_yes_belt_struct.average_x], [E1P1_yes_belt_struct.average_y],'b-*','linewidth',.5);
plot([E1P1_no_belt_struct.average_x], [E1P1_no_belt_struct.average_y],'yellow*','linewidth',.5);
% plot([E1P1_yes_belt_struct.tag_1_x], [E1P1_yes_belt_struct.tag_1_y],'r-*','linewidth',.5);
% plot([E1P1_yes_belt_struct.tag_2_x], [E1P1_yes_belt_struct.tag_2_y],'y-*','linewidth',.5);
dim = [0.2 0.5 0.3 0.3];
str = {'Blue = With Belt', 'Yellow = Without Belt, Red = Intended Path'};
annotation('textbox',dim,'String',str,'FitBoxToText','on');
plotbrowser("on");
cd("C:\Work\MATLAB\FPL\Visualization of Paths");
saveas(gcf,["E1P1 Yes Belt - Participant " + participant_number + ".png"]);
cd("C:\Work\MATLAB\FPL");
0 comentarios
Respuestas (1)
Lokesh
el 26 de Oct. de 2023
Hi Spencer,
I understand that you're facing an issue with the aspect ratio of the exported image compared to the original image.
The reason for exported image to have different aspect ratio from original image is because the “saveas” function saves the entire figure window, not just the contents of the axes (which is where image is displayed).
To resolve this issue, you can use "getframe" function instead of "saveas". By properly specifying the properties of the figure and axes, you can generate an output image that matches the dimensions of the original image.
Here is an example code to export image using “getframe” function:
I = imread('peppers.png'); %# Load a sample image
imshow(I); %# Display it
[r,c,d] = size(I); %# Get the image size
set(gca,'Units','normalized','Position',[0 0 1 1]); %# Modify axes size
set(gcf,'Units','pixels','Position',[200 200 c r]); %# Modify figure size
hold on;
plot(100,100,'r*'); %# Plot something over the image
f = getframe(gcf); %# Capture the current window
imwrite(f.cdata,'image2.jpg'); %# Save the frame data
The output image “image2.jpg” should have a red asterisk on it and should have the same aspect ratio as the input image.
Please refer to the following MATLAB documentation link to know more about the usage of “getframe” and “set” function:
I hope you find this helpful.
Best Regards,
Lokesh
0 comentarios
Ver también
Categorías
Más información sobre Printing and Saving 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!