matlab读取RGB888或RGB565像素文件并绘图

来源:互联网 发布:淘宝订单怎么取消订单 编辑:程序博客网 时间:2024/06/05 17:22

1、matlab下,通过读取txt文件中的RGB888格式的像素文件绘图,并将其转换为RGB565格式后再绘图。代码如下:

clear all;
close all;
a=textread('E:\matlab\rst.txt','%6s')';%以字符形式打开文件 
Images_Dec=hex2dec(a)'; %16进制转化为10进制数,存入Images_Dec矩阵 
for i=1:576
    for j=1:1440
        for k=1:3
            A(i,j,k)=Images_Dec((i-1)*1440*3+(j-1)*3+k);
        end
    end
end


fr=A(:,:,1);
fg=A(:,:,2);
fb=A(:,:,3);
ar=fr*(31/255);
ag=fg*(63/255);
ab=fb*(31/255);
imgRGB565 = cat(3,ar,ag,ab);  %# Concatenate along the third dimension
%imwrite(imgRGB565,'myImage.bmp');   %# Output the image to a file
subplot(2,1,1);
imshow(A);
subplot(2,1,2);
imshow(imgRGB565);

rst.txt中文件内容如下图所示:



2、matlab下,通过读取txt文件中的RGB565格式的像素文件,从中解析出RGB888像素后绘图,并将其转换为RGB565格式后再绘图。代码如下:

clear all;
close all;
a=textread('E:\matlab\a.txt','%4s')';%以字符形式打开文件 
Images_Dec=hex2dec(a)'; %16进制转化为10进制数,存入Images_Dec矩阵 


A=zeros(576,720);
for i=1:576
    for j=1:720
        A(i,j)=Images_Dec((i-1)*720+j);
    end
end


imgR = bitshift(bitand(A,63488),-8);  %# Red component
imgG = bitshift(bitand(A,2016),-3);   %# Green component
imgB = bitshift(bitand(A,31),3);      %# Blue component
im888 = cat(3,imgR,imgG,imgB); 


ar=imgR*(31/255);
ag=imgG*(63/255);
ab=imgB*(31/255);
imgRGB565 = cat(3,ar,ag,ab);  %# Concatenate along the third dimension


subplot(2,1,1);
imshow(imgRGB565);
subplot(2,1,2);
imshow(im888);

a.txt中文件的内容如下: