How to remove the grey margin from the imshow image

13 ビュー (過去 30 日間)
Shuyang Zhang
Shuyang Zhang 2022 年 12 月 5 日
コメント済み: Shuyang Zhang 2022 年 12 月 7 日
I'm analysing the MRI data with the imshow function. But every time the image pop out in a small window with a large grey (or white if I save it) margin around it. I have to zoom in the image in order to select the ROI.
Is there a code that can remove or reduce the margin as well as show the image in a larger window so that I don't have to double click it to full screen every single time?
Please see my code below. Thank you very much!
%% Draw kidney left t1 ROIs
slice =4 ;
figure; imshow(Maps.kidney.t1(:,:,slice),[0 1500]); colormap('jet');
r = drawrectangle('Position',[32 84 3 3]), wait(r);

回答 (1 件)

Image Analyst
Image Analyst 2022 年 12 月 6 日
You're probably saving it with exportgraphics or saveas instead of imwrite. Use imwrite instead when you save your image.
  9 件のコメント
DGM
DGM 2022 年 12 月 7 日
編集済み: DGM 2022 年 12 月 7 日
That's my point. If you need to include objects other than the image itself, you can't trivially use imwrite() to do it. You need to somehow capture the figure or axes.
If you rely on print(), saveas() or the "save" buttons in the figure or axes toolbars, you'll lose control over the image scaling. You'll just get something that's dependent on the figure geometry.
If you use getframe(), you have a bit more control, but only if the image is scaled correctly.
% an image
inpict = imread('cameraman.tif'); % 256x256
% display the image and create an ROI object
% by default, imshow() uses loose borders, which does nothing but waste space
% i have my preferences set to use tight borders, so i'm explicitly using defaults here.
% set this to 'tight' in preferences if you never want to deal with that again
imshow(inpict,'colormap',hot,'border','loose','initialmagnification','fit'); % DEFAULT BEHAVIOR
ROI = drawrectangle(gca,'position',[75 75 100 100]);
% try to force the saved image to have the same geometry as the original
% this won't work if the figure is docked
truesize(gcf,size(inpict))
% take a screenshot of the axes and save it
screenshot = frame2im(getframe(gca)); % should also be 256x256
imwrite(screenshot,'myscreenshot.png') % save nothing as JPG
That has no white borders. The image size has not changed, so it's not been damaged by interpolation.
% do the same thing using saveas()/print
saveas(gcf,'screenshotfig.png')
saveas(gca,'screenshotax.png')
print(gcf,'printoutfig.png','-dpng')
All of those include white padding. All of those are rescaled to some uncontrolled geometry.
Use truesize()
Don't move the view (or just restore it after you do)
Use getframe() instead of saveas().
Since it looks like you have a tiny image, you may not be able to save the image at the original size and still have the resolution to render the ROI objects. If that's the case, you can scale the output size in the call to truesize()
truesize(gcf,size(inpict)*2) % scale by some integer
Can exportgraphics() help? Maybe. I don't have it, so I can't test that.
Of course, if you're manually panning and zooming the image all over the place before saving it, all bets are off. Even with getframe(), you'll have to deal with rescaling and (sometimes) a few pixels of white padding if things get moved.
Shuyang Zhang
Shuyang Zhang 2022 年 12 月 7 日
The image with a small white padding is good enough for me. Thank you very much for your help!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeExplore and Edit Images with Image Viewer App についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by