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!