Save/Crop Figure (bmp) file without background

조회 수: 5 (최근 30일)
Trang Hu Jia
Trang Hu Jia 2021년 12월 29일
댓글: Image Analyst 2022년 1월 3일
Hello everyone,
I'm try to perform an Image-based analysis.
However when I save the image file as a bitmap (bmp) format. The background(white color) is appeared in my Figure below.
My picture size is 875 x 656 pixels.
I want to crop or save this Figure without background as shown the red-line in Figure below.
Please help me to obtained the result.
Thanks in advance.

채택된 답변

DGM
DGM 2021년 12월 29일
편집: DGM 2021년 12월 29일
If you have a raster image displayed in a figure, don't save it from the figure. Use imwrite().
Doing this:
A = imread('cameraman.tif');
imshow(A)
and then saving the figure is equivalent to taking a screenshot. The image will include padding and artifacts from display interpolation. The core image area will likely not be the original size (depending on the source image size, figure size, and preferences).
Instead, just do
imwrite(A,'myimage.bmp')
If you have other graphics objects in the same figure (objects plotted on top of an image), then that might be a different story. Generally, saving the entire figure will give you more problems with excess padding, while saving the axes (using the axes context menu) will give you less padding.
If you have a giant pile of such images and you just want to crop them all down (and you don't care about any alterations that may have already happened when the figure was saved), you can try one of these options:
  댓글 수: 1
Trang Hu Jia
Trang Hu Jia 2022년 1월 3일
Thank you ver much.
Your solution is help me a lot.

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

추가 답변 (2개)

yanqi liu
yanqi liu 2021년 12월 30일
yes,sir,if through figure to save the image file as a bitmap (bmp) format. may be use
f = getframe(gca);
f = frame2im(f);
imwrite(f, 'result.bmp');

Image Analyst
Image Analyst 2021년 12월 30일
Try exportgraphics() if you want the axes along with all the contents, like image, any graphical overlays (like lines, arrows, etc.).
exportgraphics(gca, fullFileName); % gca is the last axes that you did anything with.
gca is the last axes control that you did anything with. You do not have to replace that with anything or explicitly assign gca to something. It's a built in variable that will automatically have the axes handle to the last axes you touched.
Otherwise, if you just want the image alone, use imwrite(yourImage, fileName) to save just the image alone with no graphics or axis labels.
imwrite(yourImage, fullFileName);
  댓글 수: 2
Trang Hu Jia
Trang Hu Jia 2022년 1월 3일
Thank you very much.
Image Analyst
Image Analyst 2022년 1월 3일
You're welcome. I should also add that exportgraphics() is basically a screenshot, which can vary insize depending on how much you enlarge the figure window. So the saved image size will not match your image's resolution. If you want the saved image to have the same number of rows and columns as the image in the axes, you'll have to use imwrite. You can burn graphics into the image before writing if you want with functions like insertText(), etc.

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

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by