How to save a visualized image and the ROI ?

15 views (last 30 days)
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

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!