How to save an image without axis or white space?
125 views (last 30 days)
Show older comments
I am currently trying to save an image adn then reopen that image in MATLAB. I am running into an issue that when I save that image it is saving with the axis. This then causes me to reopen the image and have the image be a different diemnsion due to the axis saving. I have then turned off the axis as peopel have recommended from readings other comments on similar problems. However, when I turn off the axis, then the space where the axis used to be saves as white space on my image. I want to open the image and save the image as the same size. I have attached a few pictures of what the image saving looks like. I have attahced the orgional image called square, the working image which is the image without after saved and reopened in MATLAB, and then the same thing hapening wiht the axis which is the screen shot.
Attached below is also my current code for this section of my code.
set(gca, 'Visible', 'off');
saveas(gcf,'working_image.jpg');
close all
img = imread('working_image.jpg'); % Replace with your image file
6 Comments
Umar
on 11 Jul 2024
Hi Alexandra,
Thanks for clarifying your issues in more details. Now, I have a complete picture of your objective, let me help you by resolving your issue by sharing my code as an example which will help resolve your problem and then you can reply if this is what you were looking for.
% Read the original image
originalImage = imread('original_image.jpg');
% Display the original image with a rectangle drawn on it
imshow(originalImage);
hold on;
rectangle('Position', [x, y, width, height], 'EdgeColor', 'r', 'LineWidth', 2);
hold off;
% Save the image without the axis
set(gca, 'Visible', 'off');
set(gcf, 'Units', 'pixels', 'Position', get(gcf, 'Position'));
saveas(gcf, 'image_with_rectangle.png');
close;
% Reopen the saved image without the axis
savedImage = imread('image_with_rectangle.png');
% Display the saved image with the same size as the original image
figure('Units', 'pixels', 'Position', get(0, 'Screensize'));
imshow(savedImage);
hold on;
% Draw more rectangles or apply stored points on the new image
rectangle('Position', [new_x, new_y, new_width, new_height], 'EdgeColor', 'b', 'LineWidth', 2);
hold off;
So, let me explain the above code, it reads the original image and displays it with a red rectangle drawn on it.It then saves the image without the axis by turning off the axis visibility and saving the figure without extra white space. The saved image is reopened and displayed with the same size as the original image by setting the figure's position to the screen size.Additional rectangles or stored points can be applied to the new image as needed.
Now, I will wait for your response.
Answers (2)
DGM
on 12 Jul 2024
Edited: DGM
on 12 Jul 2024
First things first, unless you need to include other graphical objects (lines, markers, annotations) in the image, save images uing imwrite(), not by taking screenshots of the figure.
If you do need to include other things in the output, then you can try to get the output geometry to match the input geometry using truesize(). There are limitations, and it's not always exact, but it's a lot simpler than trying to just use saveas(). If you try using saveas() without a ton of extra brittle work, you can expect the output size to essentially be random. Trying to use something like truesize() to force figure/axes geometry won't work with saveas().
% an image (256x256)
inpict = imread('cameraman.tif');
% an image object with other objects that need to be captured
figure % use a new figure
imshow(inpict); hold on
plot([100 200 150 100],[50 130 200 50],'linewidth',2)
% this simply doesn't work easily
saveas(gca,'alwayswrongsize.png') % NO JPG
% verify the output geometry
S = imfinfo('alwayswrongsize.png');
[S.Height S.Width]
% try to use truesize() with saveas()!
drawnow
truesize(gcf,size(inpict,1:2))
saveas(gca,'stillswrongsize.png') % NO JPG
% verify the output geometry
S = imfinfo('stillswrongsize.png');
[S.Height S.Width]
Instead, you can try to use truesize() with getframe()/imwrite() to save the image, though depending on the version or circumstance, I wouldn't guarantee it to always be exact.
% an image (256x256)
inpict = imread('cameraman.tif');
% an image object with other objects that need to be captured
figure % use a new figure
imshow(inpict); hold on
plot([100 200 150 100],[50 130 200 50],'linewidth',2)
% _try_ to force figure geometry to match the image
drawnow
truesize(gcf,size(inpict,1:2))
% save the image using getframe() and imwrite()
screenshot = frame2im(getframe(gca));
imwrite(screenshot,'screenshot.png') % NO JPG
% verify the output geometry
S = imfinfo('screenshot.png');
[S.Height S.Width]
Truesize() does require there to be an image object in the figure. The attached function captureaxes() does not, and seems to be at least as consistent.
% an image (256x256)
inpict = imread('cameraman.tif');
% an image object with other objects that need to be captured
figure % use a new figure
imshow(inpict); hold on
plot([100 200 150 100],[50 130 200 50],'linewidth',2)
% save the image using captureaxes() and imwrite()
screenshot = captureaxes(gca,size(inpict,1:2));
imwrite(screenshot,'screenshotca.png') % NO JPG
% verify the output geometry
S = imfinfo('screenshotca.png');
[S.Height S.Width]
There are other tools like exportgraphics(), but I don't have that, so I can't trust that it consistently produces output that's actually on-size.
0 Comments
Image Analyst
on 12 Jul 2024
You can use imwrite() to save any matrix as an image. It won't save any axis labels or graphics in the overlay.
You can use getimage() to extract the original image from an axes control, like if one function put an image into the axes but a second function wanted to save that image but was not passed the original image from the other function.
You can use getframe() to get, basically, a screenshot of the displayed image. It will give you the image at whatever resolution it is displayed at, which may not be the original size - it depends on how you sized the figure, like from dragging a corner of the figure window to resize it. It will give you the graphics in the overlay as "burned into" the RGB image that getframe gives you. You can then use imwrite to save that screenshot image (+ overlay) to disk. It does not save axis tick labels. Example:
img = imread('moon.tif');
imshow(img);
impixelinfo;
hold on
% Put rectangle into overlay.
rectangle('Position', [10, 10, 200 200], 'EdgeColor', 'r', 'LineWidth', 2);
strScreenshot = getframe(gca); % Get screenshot
rgbImage = strScreenshot.cdata;
figure
imshow(rgbImage)
axis off;
impixelinfo;
imwrite(rgbImage, 'Screenshot.png');
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!