New*_*ser 7 rgb matlab image-manipulation image image-formats
我从微处理器获得RGB矩阵,输出RGB565格式的图像.我想把它读入MATLAB,将其转换为RGB24格式,然后输出图像.我该怎么做呢?
首先必须将文本文件中的数据读入MATLAB中的矩阵.由于我不知道你的文本文件是什么格式,我只能建议您可能需要使用该函数fscanf来读入所有值(可能是类型uint16),然后您可能需要将值重新整形为使用该函数的N×M图像矩阵reshape.
假设你已经完成了所有这些,现在你有了一个N-by-M矩阵img的无符号16位整数.首先,您可以使用该函数bitand提取红色,绿色和蓝色分量的位,其位置在16位整数中,如下所示:

接下来,您可以使用函数bitshift和乘以比例因子将红色,绿色和蓝色值缩放到0到255的范围,然后使用该函数将它们转换为无符号的8位整数uint8.这将为您提供三个相同大小的颜色分量矩阵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
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用此功能cat将三个颜色分量放入N×by-M×3 RGB图像矩阵中,然后使用以下函数将图像保存为RGB24位图文件imwrite:
imgRGB = cat(3, imgR, imgG, imgB); % Concatenate along the third dimension
imwrite(imgRGB, 'myImage.bmp'); % Output the image to a file
Run Code Online (Sandbox Code Playgroud)
例:
使用随机生成的100×100矩阵的uint16值并应用上述转换,结果如下:
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');
Run Code Online (Sandbox Code Playgroud)
