Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 539
NewImageUser Avatar asked Aug 26 '10 18:08

NewImageUser


People also ask

How do you change an image from RGB to grayscale in Matlab?

I = rgb2gray( RGB ) converts the truecolor image RGB to the grayscale image I . The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.

How do I convert to RGB in Matlab?

RGB = ind2rgb( X , map ) converts the indexed image X and corresponding colormap map to RGB (truecolor) format.


1 Answers

You first have to read your data from the text file into a matrix in MATLAB. Since I don't know what format your text file is in, I can only suggest that you will probably need to use the function fscanf to read in all of your values (probably of type uint16), then you will likely have to reshape the values into an N-by-M image matrix using the function reshape.

Let's assume you've done all that, and you now have an N-by-M matrix img of unsigned 16-bit integers. First, you can use the function bitand to extract the bits for the red, green, and blue components, whose positions in the 16-bit integer are illustrated here:

alt text

Next, you can use the function bitshift and multiplication by a scale factor to scale the red, green, and blue values to a range of 0 to 255, then convert them to an unsigned 8-bit integer using the function uint8. This will give you three color component matrices 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 function cat to put the three color components into an N-by-M-by-3 RGB image matrix, then save the image to an RGB24 bitmap file using the function imwrite:

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 100-by-100 matrix of uint16 values and applying the above conversions, 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

like image 130
gnovice Avatar answered Sep 22 '22 22:09

gnovice