MATLAB显示串口接收的rgb565图像

来源:互联网 发布:linux启动卡在进度条 编辑:程序博客网 时间:2024/05/16 12:19

程序一(感觉有问题)难过

clc
clear all;
close all;
a=textread('C:\Users\Administrator\Desktop\11.txt','%s')';%以字符形式打开文件 
Images_Dec=hex2dec(a)'; %16进制转化为10进制数,存入Images_Dec矩阵
i=1;j=0;str=[];
while i<=length(Images_Dec)
    j=j+1;
    str(j)=Images_Dec(i+1)+Images_Dec(i)*256;
    i=i+2;
end
A=zeros(320,240);
pic=uint8(zeros(320,240,3));
for i=1:320
    for j=1:240
        A(i,j)=str((i-1)*240+j);
        pic(i,j,1)=uint8(bitshift(A(i,j),-11));
        pic(i,j,2)=uint8(bitshift(A(i,j),-5));
        pic(i,j,3)=uint8(bitshift(A(i,j),0));
    end
end
R = bitshift(bitand(A,63488),-8);  %# Red component
G = bitshift(bitand(A,2016),-3);   %# Green component
B = bitshift(bitand(A,31),3);      %# Blue component
im888 = cat(3,R,G,B); 
r=R*(31/255);
g=G*(63/255);
b=B*(31/255);
RGB565 = cat(3,r,g,b);  %# Concatenate along the third dimension
figure(1);
imshow(RGB565);
figure(2);
imshow(im888);
figure(3);
imshow(pic);

程序二(能显示,不正常)难过

clc
clear all;
close all;
length=240;width=320;
in_name='C:\Users\Administrator\Desktop\44.txt';
out_name='C:\Users\Administrator\Desktop\dd.bmp';
pic=uint8(zeros(length,width,3));


in=textread(in_name,'%s');
dec_data=hex2dec(in);
% dlmwrite('dec.txt',dec_data,'','');
% dec_test=dec_data(1:320);
k=1;
for i=1:length
    for j=1:width
        H=dec_data(k,1);
        if(k+1>width*length*2)
            k=k-1;
        else
            k=k;
        end
        L=dec_data(k+1,1);
        k=k+2;
        pic(i,j,1)=(H/8)*8;
        pic(i,j,2)=(mod(H,8)*8+L/32)*4;
        pic(i,j,3)=(mod(L,32))*8; 
    end
end
figure,imshow(pic);
imwrite(pic,out_name);

弄了一天都没完全解决问题。。。。。哭

我的串口发送部分为:

for(i=0;i<240;i++)
{
for(j=0;j<320;j++)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//循环发送,直到发送完毕  
USART_SendData(USART1,rgb_buf[i][j]%256); 
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//循环发送,直到发送完毕  
USART_SendData(USART1,rgb_buf[i][j]/256); 
}
}


原创粉丝点击