Reduce padding around saved image

24 vues (au cours des 30 derniers jours)
Aastha Sharma
Aastha Sharma le 20 Mar 2020
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?

Réponses (1)

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

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by