How to choose a ROI of an image and make rest transparent, then save it?

2 ビュー (過去 30 日間)
I have two images of the same object: 1) bright-field, 2) a luminescence intensity. To create an overlay, I would like to define a ROI of the image 2), and then overlay this to the image 1). The area outside of ROI in image 2) should be transparent.
I got the ROI working and it also displays as the region outside of the ROI is transparent, but then exporting/saving fails
I=imread('cameraman.tif');
I=double(I); %convert to my typical form of input
% Define the Region of Interest
c = [100 200 200 100];
r = [100 100 200 200];
% Creates a Mask
BW = roipoly(I,c,r);
BW = double(BW);
BW(BW==0) = NaN; %set elements outside to NaN
filter_1 = I.*BW; % multipliy to get old intensity
image(filter_1);
transparent=image(I, 'AlphaData', filter_1);
imwrite(I, 'test.png', 'Alpha', filter_1) %doesn't work

採用された回答

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2019 年 12 月 15 日
To write the image I has to be either a double normalized to 1 or a uint8 with values from 0 to 255, in your case you had double values from 0 to 255. Alpha also has to be between 0 and 1. Changing just the last line of your code produces the results that you want
imwrite(uint8(I), 'test.png', 'Alpha', filter_1/max(filter_1(:)))

その他の回答 (1 件)

Pascal Gschwend
Pascal Gschwend 2019 年 12 月 15 日
Thank you Thiago, that works. I could even shorten the code a bit. But this brings me to my next problem: I would like to save the image with a colormap, but this always give me the following error message:
Error using writepng (line 35)
Cannot specify alpha channel with an indexed image.
Here's my updated code
I=imread('cameraman.tif');
I=double(I); %convert to my typical form of input
% Define the Region of Interest
c = [100 200 200 100];
r = [100 100 200 200];
% Creates a Mask
BW = roipoly(I,c,r);
BW2=double(BW);
I_norm=I/255; %normalize to values between 0 and 1
imwrite(I_norm, 'test.png', 'Alpha', BW2)
imwrite(I_norm, jet,'test.png', 'Alpha', BW2) % does not work
  3 件のコメント
Image Analyst
Image Analyst 2019 年 12 月 15 日
You can convert your indexed image to an RGB image before saving, but you don't have to. You can save indexed images along with their colormaps. See the code below:
grayImage = imread('moon.tif');
cmap = hsv(256);
imshow(grayImage, 'Colormap', cmap);
colorbar
% Save indexed image along with it's color map to disk.
imwrite(grayImage, cmap, 'deleteme.tif');
% Read it back in and apply the colormap and observe it looks the same.
figure
[grayImage2, cmap2] = imread('deleteme.tif');
imshow(grayImage2, 'Colormap', cmap2); % Looks exactly the same
Pascal Gschwend
Pascal Gschwend 2019 年 12 月 15 日
@Thiago: Thank you, that fixed the problem.
@Image Analyst: Thanks for your comment. Are you sure though that this also works using the transparency (alpha)?

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

カテゴリ

Help Center および File ExchangeBlue についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by