Is it possible to save a 32bit BMP image (with alpha)

Is it possible to save a 32bit BMP image. i.e. image size (imheight x imwidth x 4) where the 4.th channel consist of alpha values?

 채택된 답변

Walter Roberson
Walter Roberson 2011년 5월 10일

0 개 추천

imwrite does not support alpha in BMP. See http://www.mathworks.com/matlabcentral/answers/6335-writing-32-bit-images for information on what can be used.

댓글 수: 8

Slimane
Slimane 2011년 5월 11일
이동: DGM 2024년 7월 15일
thank you Walter for your relpy..
I tried also using TIFF and RAS, where i add alpha layer but the size of the new image remains 24BPP as input image.. here is a code sample:
%%%%%%%%%%%%%%%%%%%%%
%Input image (376x240x3)
InpIm = imread('inputImage.bmp'); % InpIm is of uint8 type
[rows,cols,chan] = size(InpIm);
%Build alpha layer
InpIm(:,:,4) = 0.5*ones(rows,cols,'uint8');
%Save as Tiff image
imwrite(InpIm,'New.tiff','TIFF'); %New.tiff is also 24BPP !!
%%%%%%%%%%%%%%%%%%%%%
Does any one have an example where one can save an image as 32BPP (RGBA)??
Walter Roberson
Walter Roberson 2011년 5월 11일
이동: DGM 2024년 7월 15일
uint8(0.5*uint8(1)) is uint8(0.5) which is 1, so the alpha value you would be writing would be 1 out of 256. You should be writing 0.5 * intmax('uint8') if you want alpha of 1/2
Anyhow, if you have a new enough MATLAB, use the Tiff class, http://www.mathworks.com/help/techdoc/ref/tiffclass.html
Slimane
Slimane 2011년 5월 11일
이동: DGM 2024년 7월 15일
I am actually more concerned by the final size of the image. the resulted image is always 24BPP and not as expected 32BPP, (since i added an extra layer i.e. InpIm(:,:,4))
Walter Roberson
Walter Roberson 2011년 5월 11일
이동: DGM 2024년 7월 15일
I just tried,
foo = uint8(rand(64,64,4) * 256);
imwrite(foo,'New.tiff','TIFF');
imfinfo('New.tiff')
ans =
Filename: 'New.tiff'
FileModDate: '11-May-2011 13:50:06'
FileSize: 16734
Format: 'tif'
FormatVersion: []
Width: 64
Height: 64
BitDepth: 32
ColorType: 'CMYK'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: [8 8 8 8]
Compression: 'PackBits'
PhotometricInterpretation: 'CMYK'
StripOffsets: [2x1 double]
SamplesPerPixel: 4
RowsPerStrip: 32
StripByteCounts: [2x1 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.01
MaxSampleValue: [255 255 255 255]
MinSampleValue: 0
Thresholding: 1
What does imfinfo() tell you about your file?
Slimane
Slimane 2011년 5월 11일
이동: DGM 2024년 7월 15일
Yes i can see that bitdepth is 32, but when i open the saved image in e.g. 'irfanView' it says the image size is 240x376x24BPP. Here is the imfinfo i receive:
ans =
Filename: 'New.tiff'
FileModDate: '11-May-2011 21:52:55'
FileSize: 364374
Format: 'tif'
FormatVersion: []
Width: 376
Height: 240
BitDepth: 32
ColorType: 'unknown'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: [8 8 8 8]
Compression: 'PackBits'
PhotometricInterpretation: 'CMYK'
StripOffsets: [48x1 double]
SamplesPerPixel: 4
RowsPerStrip: 5
StripByteCounts: [48x1 double]
XResolution: 72
YResolution: 72
ResolutionUnit: 'Inch'
Colormap: []
PlanarConfiguration: 'Chunky'
TileWidth: []
TileLength: []
TileOffsets: []
TileByteCounts: []
Orientation: 1
FillOrder: 1
GrayResponseUnit: 0.0100
MaxSampleValue: [255 255 255 255]
MinSampleValue: 0
Thresholding: 1
Walter Roberson
Walter Roberson 2011년 5월 11일
이동: DGM 2024년 7월 15일
It might be an irfanView issue. I don't have an image processing program around that I can cross-check with.
Slimane
Slimane 2011년 5월 12일
이동: DGM 2024년 7월 15일
thanks a lot Walter, i guess i will just use c++ it is much easier :)
DGM
DGM 2024년 7월 15일
This problem is occurring because imwrite() interprets a 4-channel RGBA image as CMYK (regardless of any asserted colorspace parameter). So anything that decodes the file will likely wind up treating it as CMYK and converting it back into an opaque 24b RGB equivalent.
As far as I know, writing an RGBA TIFF this way isn't an option with imwrite(). It should be possible with the tiff class.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John
John 2011년 5월 12일

2 개 추천

Here's a demo I did a while back on how to create a TIFF with an alpha layer.
% This image is included with MATLAB
D = imread('street1.jpg');
image(D); title('RGB image without alpha layer.');
h = size(D,1);
w = size(D,2);
% Create a gradually increasing transparency mask
% from left-to-right across the image.
T = zeros(h,w,'uint8');
for j=1:w
T(:,j) = min(255,j) * ones(h,1);
end
% The dimensions of our image are h x w x 3 (remember, it's RGB). If
% we wish to add an alpha component, we need to give the image data a 4th
% component.
rgbaData = cat(3,D,T);
% Let's create our new image.
t = Tiff('alpha.tif','w');
% The photometric interpretation remains RGB, even though we will be
% adding an extra sample.
t.setTag('Photometric',Tiff.Photometric.RGB);
% These three tags define the dimensions of the image in the file.
t.setTag('ImageWidth',w);
t.setTag('ImageLength',h);
t.setTag('BitsPerSample',8);
t.setTag('SamplesPerPixel',4);
% We will make the image consist of 64x64 tiles.
t.setTag('TileWidth',64);
t.setTag('TileLength',64);
t.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
% A photometric interpretation of RGB means that the TIFF file is expecting
% three components. We have to tell it that there will really be four
% components (the alpha layer is the 4th).
t.setTag('ExtraSamples',Tiff.ExtraSamples.UnassociatedAlpha);
t.write(rgbaData);
t.close();

카테고리

질문:

2011년 5월 10일

댓글:

DGM
2024년 7월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by