Reduce padding around saved image
5 次查看(过去 30 天)
显示 更早的评论
I'm trying to save the contours of an image. I want the contours in my image to be white, on a black background.
grayImage = imread('bench.png');
grayImage = imcomplement(grayImage);
he=imcontour(grayImage, 2, '-w');
set(gca,'XColor', 'none','YColor','none', 'Color','k');
set(gcf, 'InvertHardCopy', 'off');
saveas(gca,'benchcontour.png');
The saved image has extra space around it, which creates issues in what I want to do afterwards.
Original image:

Saved image:

How can I get rid of the padding around the plot?
0 个评论
回答(1 个)
Bjorn Gustavsson
2020-3-20
If you save your edge-enhanced image with something like
print -dpng your-image.png
you can increase the fraction of your image to fill the entire figure, something like this:
set(gca,'position',[0 0 1 1])
That should remove the padding.
Another way to go about this is to write the edge-enhanced image to an image directly:
imagesc(img_edge_enhanced)
imwrite(img_edge_enhanced,'your-clean-image.png')
That should produce a BW-image if your img_edge_enhanced is scaled between 0 and 1.
HTH
2 个评论
Bjorn Gustavsson
2020-3-20
Ah, when you use imcontour, I'd go with the first suggestion. Just add the set(gc,'position',[0 0 1 1]) to your script before your saveas-call. The second suggestion was for the case where you'd made a binary image with some edge-detection-method, such a BW-image you could write directly to an image-file using imwrite. Since you plot an acurla contour-plot of your image you'd be better off using the first option.
HTH
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surfaces, Volumes, and Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!