How can I save a line plot as an image with specific pixel size?
36 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
採用された回答
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
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]);
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!