How can I convert between image formats RGB565 and RGB24 in MATLAB?

I get an RGB matrix from a microprocessor that outputs an image in RGB565 format. I want to read this in MATLAB, convert it to RGB24 format and output the image. How to do it?

+6
image matlab rgb image-manipulation image-formats
source share
2 answers

You must first read your data from a text file into a matrix in MATLAB. Since I don’t know what format your text file is in, I can only assume that you probably need to use the fscanf function to read all of your values ​​(probably like uint16 ), then you probably have to change the values ​​to N -M using the reshape function.

Suppose you did all this and now you have the N-by-M img matrix of unsigned 16-bit integers. First, you can use the bitand function to extract bits for the red, green, and blue components whose positions are in a 16-bit integer:

alt text

You can then use the bitshift function and multiply by a scaling factor to scale the red, green, and blue values ​​to a range from 0 to 255, then convert them to an unsigned 8-bit integer using the uint8 function. This will give you three color component matrices of the same size as img :

 imgR = uint8((255/31).*bitshift(bitand(img, 63488), -11)); % Red component imgG = uint8((255/63).*bitshift(bitand(img, 2016), -5)); % Green component imgB = uint8((255/31).*bitand(img, 31)); % Blue component 

Now you can use the cat function to place the three color components in the N-by-M-by-3 RGB bit, then save the image in the RGB24 raster file using the imwrite function:

 imgRGB = cat(3, imgR, imgG, imgB); % Concatenate along the third dimension imwrite(imgRGB, 'myImage.bmp'); % Output the image to a file 

Example:

Using a randomly generated matrix of 100 to 100 uint16 values ​​and applying the above transforms, here are the results:

 img = randi([0 65535], 100, 100, 'uint16'); % Perform the above conversions to get imgRGB subplot(1, 2, 1); imshow(img); title('Random uint16 image'); subplot(1, 2, 2); imshow(imgRGB); title('Corresponding RGB image'); 

alt text

+8
source share

RGB565 means 5-bit red, 6-bit green, and 5-bit blue. The RGB24 is made of 8-bit red, 8-bit green, and 8-bit blue.

Using bitgit and bits, you can convert your data.

http://www.mathworks.de/access/helpdesk/help/techdoc/ref/bitget.html

+2
source share

All Articles