Reduce padding around saved image

조회 수: 8 (최근 30일)
Aastha Sharma
Aastha Sharma 2020년 3월 20일
댓글: Bjorn Gustavsson 2020년 3월 20일
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개)

Bjorn Gustavsson
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
Aastha Sharma
Aastha Sharma 2020년 3월 20일
To clarify, what exactly is img_edge_enhanced here? The variable assigned to imcontour?
Bjorn Gustavsson
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

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

Community Treasure Hunt

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

Start Hunting!

Translated by