How to write a Image with Alpha channel ?using imwrite matlab command?

50 views (last 30 days)
How to write a Image with Alpha channel ?using imwrite matlab command?
Example:
RGB=imread('football.jpg');
Alpha=?
imwrite(X,Y,X,X,)

Answers (1)

Voss
Voss on 8 Jul 2022
Edited: Voss on 8 Jul 2022
% original image
RGB=imread('football.jpg');
imshow(RGB)
Use .png format with Alpha parameter in imwrite
% Attempt to make the blue background green, using transparency/opacity
% parameter 'Alpha' and the 'Background' parameter, which is the color
% that shows "under" the places where the image is transparent.
% alpha is a matrix of uint8, based on a logical condition
alpha = uint8(255*(RGB(:,:,1) >= 100 | RGB(:,:,3) <= 50));
% the first pixel is fully transparent (no opacity)
alpha(1,1)
ans = uint8 0
% write the png file, with alpha and background
imwrite(RGB,'football.png','Alpha',alpha,'Background',[0 0.6 0])
% check the outcome
imshow(imread('football.png'))
% attempt to make the football transparent by
% using the opposite condition
alpha = uint8(255*(RGB(:,:,1) < 100 & RGB(:,:,3) > 50));
% the first pixel has no transparency (fully opaque)
alpha(1,1)
ans = uint8 255
% write the png file, with alpha and background
imwrite(RGB,'football.png','Alpha',alpha,'Background',[0 0.6 0])
% check the outcome
imshow(imread('football.png'))
  3 Comments
Voss
Voss on 9 Jul 2022
You're welcome!
"I want to save the Alpha channel"
The transparency information is saved in the image.
"if I read the Image it should show 4 channel"
The transparency matrix is returned as the third output from imread, separate from the m-by-n-by-3 RGB array of colors.
% read the original image, and get its alpha (which is empty)
[RGB,~,original_alpha] = imread('football.jpg');
whos RGB original_alpha
Name Size Bytes Class Attributes RGB 256x320x3 245760 uint8 original_alpha 0x0 0 double
% make up a new alpha
alpha = uint8(255*(RGB(:,:,1) >= 100 | RGB(:,:,3) <= 50));
% write the image (as png) with new alpha
imwrite(RGB,'football.png','Alpha',alpha,'Background',[0 0.6 0])
% get the RGB and alpha of the new image
[new_RGB,~,new_alpha] = imread('football.png');
whos new_*
Name Size Bytes Class Attributes new_RGB 256x320x3 245760 uint8 new_alpha 256x320 81920 uint8
% RGB didn't change
isequal(new_RGB,RGB)
ans = logical
1
% alpha from the new file is what I made up
isequal(new_alpha,alpha)
ans = logical
1
% alpha from the new file is different than alpha from the original file
isequal(new_alpha,original_alpha)
ans = logical
0
DGM
DGM on 9 Jul 2022
Edited: DGM on 9 Jul 2022
To pre-empt any confusion about what gets preserved, specifying both output arguments like this:
% get the RGB and alpha of the new image
[new_RGB,~,new_alpha] = imread('football.png');
will give you the original RGB content (of both the ball and blanket) and the new alpha as Voss demonstrates in the second example.
However, let's say you just want to discard the new alpha and get the original RGB content. Doing this:
% get the RGB and alpha of the new image
new_RGB = imread('football.png');
will not give you the expected RGB content anymore. As the first examples demonstrate, if no alpha is requested when reading an image which has alpha content, the image will interally be composited with a solid color field defined by the "BackgroundColor" field specified in the PNG file, or by the "BackgroundColor" option specified when calling imwrite() (default is black).
The lesson here is that if you want the actual content of the RGB channels, you need to request alpha if there's a possibility that the image may have alpha content -- even if you don't need it.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!