English 中文(简体)
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?

最佳回答

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

问题回答

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

Using bitget and bitset you may convert your data.

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





相关问题
Using QCView and iSight to capture image

I have a QCView that loads a Quartz file which gives you iSights feedback (basically like a QTCaptureView) Everything displays fine The button simply takes a snapshot using the following simple ...

Taking picture with QTCaptureView

Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...

Transform rectangular image into trapezoid

In .NET how can I transform an image into a trapezoid. The Matrix class supports rotation, shear, etc, but I can t see a trapezoidal transformation. I m using the usual System.Drawing.* API, but I m ...

Rotate image through Y-axis on web page

What are my options for rotating an image on a web page through the Y-axis? I m not sure if I even have the terminology right. Is this called a rotate or a transform. Most of the searches that I ve ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

Convert from 32-BPP to 8-BPP Indexed (C#)

I need to take a full color JPG Image and remap it s colors to a Indexed palette. The palette will consist of specific colors populated from a database. I need to map each color of the image to it s "...

热门标签