필터 지우기
필터 지우기

How can I save a line plot as an image with specific pixel size?

조회 수: 38 (최근 30일)
FsC
FsC 2022년 12월 8일
댓글: FsC 2022년 12월 12일
Hello,
I am trying to create a program to compare images. One is a hand drawn image and the other is an original (see attached). I am able to plot the hand drawn image points using the following code but am not sure how to save it as a .png without axis labels/box and specify the axis lengths as the pixel dimensions (1024x768). I looked on the matlab website and it only gives directions on how to save image quality and size in inches. I need to save both plots as binary images so that they can be compared as below. Also I am hoping to export the images to another software to edit them.
How would I go about automatically saving the line plots as png 768x1025 pixels?
Here is my current code and it also loads the original image to show how they need to overlap for comparison.
%load drawing data
load('drawData.mat')
% plot picture (want it to be saved as 768/1024 px)
figure
plot(drawData(:,1),drawData(:,2),'-k',"linewidth",4)
xlim([0 768])
ylim([0 1024])
set(gca,'XTick',[], 'YTick', [])
% for example, load original image
y = imread("yoda.png");
[a b] = find(flipud(y(:,:,1) ==0));
%create plot of drawing and original image (need to eventually convert both
%to png from line graph to compare
figure
plot(drawData(:,1),drawData(:,2),'-k',"linewidth",4)
hold on
plot(b,a,'r.')
xlim([0 768])
ylim([0 1024])
set(gca,'XTick',[], 'YTick', [])
Thank you for your help!

채택된 답변

Daniel Vieira
Daniel Vieira 2022년 12월 8일
to save a matlab figure as a png image use the print function:
figure;
plot(x,y)
fig=gcf;
print(fig,'filename.png','-dpng')
you can configure things like resolution with Property-Value of this function
  댓글 수: 3
Daniel Vieira
Daniel Vieira 2022년 12월 8일
편집: Daniel Vieira 2022년 12월 8일
the print function adds a sort of margin to the picture, I don't know precisely how it does. what you can do is sizing your figure to what you want and then crop or resize the final image file:
fig=gcf;
fig.Position(3:4)=[1024 768];
print(fig,'filename.png','-dpng') % this will create a file that is actually 1600 by 1200
% when loading the file:
I=imread('filename.png');
% option 1: crop
win = centerCropWindow2d(size(I,1:2),[1024 768]);
I=imcrop(I,win);
% option 2: resizing
I=imresize(I,[1024 768]);
FsC
FsC 2022년 12월 12일
That works, thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by