Converting a 16bpp bitmap image to c data array (RGB565 format)...

30 views (last 30 days)
Hi,
I'm having an 16bpp & 320x340 bitmap image. And i need to get an c 1-D data array.
I used the following command,
A = imread('MyImage.bmp');
RGB = im2uint8(A);
Now i got three matrix, each for R, B & G. And each matrix's element size is 8-bit. It means, i think ,i got RGB888 format.
But i want RGB565 format for the input image.
Kindly help me to get single 1D array of each pixel in the image.
Have a nice day.
- Lenin

Answers (1)

Anaghaa
Anaghaa on 11 Feb 2025 at 7:01
Hi Lenin!
To convert the 16bpp 320x340 bitmap image to a single 1D array in RGB565 format, follow the steps:
  • Extract the R, G and B components from the image.
R = A(:,:,1);
G = A(:,:,2);
B = A(:,:,3);
  • Convert to RGB565 format to reduce the bit depth of each color component.
R5 = bitshift(uint16(R), -3); % converts the 8-bit red values to 5 bits.
G6 = bitshift(uint16(G), -2); % converts the 8-bit green values to 6 bits.
B5 = bitshift(uint16(B), -3); % converts the 8-bit blue values to 5 bits.
  • Combine into a Single 16-bit Value.
RGB565 = bitshift(R5, 11) + bitshift(G6, 5) + B5;
% combines the R5, G6, and B5 components into a single 16-bit integer for each pixel.
  • Convert to a 1D Array.
RGB565_1D = reshape(RGB565, 1, []); % Reshapes the 2D matrix of 16-bit values into a 1D array, which is suitable for use in C
disp(RGB565_1D(1:10)); % Displays the first ten values of the 1D array for verification purposes.
Here is a link to know more about “bitshift” function:
Hope this helps!

Categories

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

Community Treasure Hunt

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

Start Hunting!