How do I convert a 24 bit depth image file to an 8 bit depth image file?

34 views (last 30 days)
I have a 24 bit depth image that needs to be used in a different program. This program however needs only an image of 8 bit depth. I want to be able to change the bit depth at will when generating a TIFF file using MATLAB.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
To achieve the bit depth conversion, use the RGB2IND function. The following code sample illustrates this:
RGB = imread('myimage.tif')
[X,map] = rgb2ind(RGB, 256);
imwrite(X,map,'myindexedimage.tif');
Now myindexedimage.tif has a bit depth of 8.
Note IM2UINT8 and the IM2UINT16 functions, are also useful when attempting to convert between different bit depths. The following code sample illustrates the conversion from a 8 bit depth image to a 24 bit depth image. 'trees.tif' is an image shipped with the Image processing toolbox that has a bit depth of 8.
[Xtrees,maptrees] = imread('trees.tif');
XtreesRGB = ind2rgb(Xtrees,maptrees);
XtreesRGB = im2uint8(XtreesRGB);
imwrite(Xtreesind,'myindexedtrees.tiff')
imfinfo('myindexedtrees.tiff')
ans =
Filename: 'myindexedtrees.tiff'
FileModDate: '08-May-2008 09:48:27'
FileSize: 259354
Format: 'tif'
FormatVersion: []
Width: 350
Height: 258
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: [73 73 42 0]
ByteOrder: 'little-endian'
NewSubFileType: 0
BitsPerSample: [8 8 8]
Compression: 'PackBits'
PhotometricInterpretation: 'RGB'
StripOffsets: [37x1 double]
SamplesPerPixel: 3
RowsPerStrip: 7
StripByteCounts: [37x1 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]
MinSampleValue: 0
Thresholding: 1

More Answers (0)

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!