How to save a visualized image and the ROI ?

Hello, I process a raw data and get a visualized image. After drawing 2 square ROIs, I wish to save the image and the ROIs as a png file with imwrite function. please see an example below:
However, I get a black-white image with the imwrite function instead.
Please see my code below:
slice = 9;
figure; imshow(Maps.kidney.t1(:,:,slice),[0 3000]); colormap('jet');
s = drawrectangle('Position',[49.44415243 81.4586071 3 3]), wait(s);
mask = createMask(s);
kidney_left_mask(:,:,slice) = mask;
map = Maps.kidney.t1(:,:,slice);
val_mean = mean(map(mask==1))
val_std = std(map(mask==1));
val_sum = sum(map(mask==1))
number_of_Voxel = val_sum / val_mean
kidney_left_t1_Mean_ROI1(slice) = val_mean
kidney_left_t1_std_ROI1(slice) = val_std;
kidney_left_t1_sum_ROI1(slice) = val_sum
kidney_left_t1_No_of_Voxel_ROI1(slice) = number_of_Voxel
%ROI 2
s2 = drawrectangle('Position',[46.07095926 85.70039422 3 3]), wait(s2);
mask = createMask(s2);
kidney_left_mask(:,:,slice) = mask;
map = Maps.kidney.t1(:,:,slice);
val_mean = mean(map(mask==1))
val_std = std(map(mask==1));
val_sum = sum(map(mask==1))
kidney_left_t1_Mean_ROI2(slice) = val_mean
kidney_left_t1_std_ROI2(slice) = val_std;
kidney_left_t1_sum_ROI2(slice) = val_sum
% save the visualized image?
filename = 'C:\Users\Public\MRI\SZ_SD_1st\M1\Kidney_ROIs_9.png'
imwrite(map,filename)
My questions are:
1) How to save both image and the ROIs?
2) I need to change the slice number (1st row) each time to visualize an image at different height.
How to modify my code (2nd to last raw) so that each time I change the slice number, the saved filename will also be changed correspondingly? e.g., when I change to slice = 8, the filename would be Kidney_ROIs_8.png. I guess by scalar?
Thank you!

 Accepted Answer

Rik
Rik on 14 Dec 2022
If you want to save what you see in a figure, you should first capture the contents with getframe.
If you want to change the file name based on a variable, you should use sprintf to generate the file name. I would suggest putting the hard-coded path at the top of the code and using fullfile to put the path and file name together.

14 Comments

Thank you for your reply. I will google how to do it and maybe come back asking your help again. I'm a novice in matlab/programming.
The Matlab documentation is very good. I would suggest you read the documentation for each of the functions I mentioned. They contain examples you can edit to your needs.
Thank you very much! The getframe works well. With your help, I figure out the 2nd question using sprintf like below:
F = getframe(gcf)
imagefilename = sprintf('SZ_SD_1st_M1_kidney_day1_pre-%d.png', slice)
imwrite(F.cdata, imagefilename)
Actually, I don't need to save it to a specific path for now.Local folder is fine. But just in case I wish to do so in the future, what do you mean by "putting the hard-coded path at the top of the code and using fullfile to put the path and file name together"? hard-coded path means the path of specific folder that don't need to change? Should I use fullfile fuction like this? Thanks again.
fullname = fullflie('path','imagefilename')
imwrite(F.cdata, fullname)
In addition, when I visualize the image, the window is very small. Every time I have to manualy click to full screen then process the ROIs. Do you know how to automaticly display the window to full screen?
What I mean is something like this:
output_dir = 'C:\Users\Public\MRI\SZ_SD_1st\M1\';
%%
% the rest of your code goes here
%%
imagefilename = sprintf('kidney_day1_pre-%d.png', slice)
fullname = fullflie(output_dir,imagefilename)
imwrite(F.CData, fullname)
That way the only place you need to change values when you go to a different computer will be at the top. This also makes it easier to convert your script to a function (which you should do for anything you plan to reuse), as all important variables a user might want to change are already at the top.
To maximize the window you can use this tool I wrote: maximize.
Thank you! Could you correct my translation on each line?
imagefilename = sprintf('kidney_day1_pre-%d.png', slice) % format slice, a variable, into a string, specificly at %, in decimal integer.
fullname = fullflie(output_dir,imagefilename) % combine string A, and string B to a new string
imwrite(F.CData, fullname)% write. F is the variable which stores the captured image, But why it appends ".CData",to get the color? I though F already has the color.
I'm using R2020b, how can I set up this windowstate property? it's somewhere in the menu or still a code?
@Shuyang Zhang to make the displayed image bigger you can use the 'InitialMagnification' option in imshow
imshow(yourImage, 'InitialMagnification', 1000); % Blow up by a factor of 10 (1000 percent).
To maximize the figure window you can do
g = gcf;
g.WindowState = 'maximized';
You have copied my typo: it should be fullfile.
Note that a char vector and a string may be the same thing in many programming languages, but in Matlab they are not. This code is using char vectors.
The result of getframe contains more than just the image: it contains CData (the color data) and the colormap. The reason why these are stored separately are mostly historical and don't really matter for you. The non-historic part is that you can use movie to play back captured frames.
Other than that, your description looks fine to me.
I figure it out:
slice = 8;
figure; imshow(Maps.kidney.t1(:,:,slice),[0 3000]); colormap('jet');
set(gcf,'WindowState','maximized')% why gcf here? why not 'figure' which I thought is the target of this code?
Why gcf here?
Thank you very much!
One more issue:
now the window is maximized, but the image is still small. The square ROI is tiny as well. is there anyway we can enlarge the area of the image as well? My screen is 13' 3200x1800. Not sure if the resolution matters. please see the screen cut below:
slice = 8;
figure;
imshow(Maps.kidney.t1(:,:,slice),[0 3000]);
colormap('jet');
set(gcf,'WindowState','maximized')% why gcf here? why not 'figure' which I thought is the target of this code?
You can't use figure because figure is a function that creates another/new figure. set() takes the handle to a figure, not a function call. You might be able to do
slice = 8;
handleToFig = figure;
imshow(Maps.kidney.t1(:,:,slice), [0 3000], 'InitialMagnification', 500);
colormap('jet');
set(handleToFig,'WindowState','maximized')% why gcf here? why not 'figure' which I thought is the target of this code?
if you'd rather.
Thank you@Rik for the explanation. Thank you @Image Analyst, this 'InitialMagnification', 1000 helps a lot!
Any suggestion on how to learn the Matlab? I learn something by doing my project and ask questions here. Any material/way to learn more systematically? Like reading the Matlab documentation? What's your experience? Thanks!
To learn MATLAB in the beginning, invest 2 hours of your time here:
To further master it, see this link:

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Asked:

on 14 Dec 2022

Commented:

on 16 Dec 2022

Community Treasure Hunt

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

Start Hunting!