Reduce padding around saved image

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?

回答(1 个)

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 个评论

To clarify, what exactly is img_edge_enhanced here? The variable assigned to imcontour?
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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by