博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用MATLAB对数据进行位操作以及RGB图片显示

Posted on 2017-02-17 10:21  沉默改良者  阅读(1963)  评论(0编辑  收藏  举报
fd = fopen('video_test_pattern_avalon_generator_source_data.txt');              //打开像素源文件
A = fscanf(fd,'%d');   //读入文件中的数据
fclose(fd);
%先申明向量用于存储数据 B = zeros(224,224); C = zeros(224,224); R1 = zeros(50176,1); R2 = zeros(50176,1); R3 = zeros(224,224); G1 = zeros(50176,1); G2 = zeros(50176,1); G3 = zeros(224,224); B1 = zeros(50176,1); B2 = zeros(224,224);
for j = 1:224 for i = 1:224 B(j,i) = A((j-1)*224+i); end end
% 24-Bit RGB数据结构 % R---1111 1111 --- 0000 0000 --- 0000 0000 = 16711680 % G---0000 0000 --- 1111 1111 --- 0000 0000 = 65280 % B---0000 0000 --- 0000 0000 --- 1111 1111 = 255 %---R for i = 1:50176 R1(i) = bitand(A(i),16711680); //与操作,得到数据的高8位 end for i = 1:50176 R2(i) = bitshift(R1(i),-16); //移位操作,将数据向右移动16位 end for j = 1:224 for i = 1:224 R3(j,i) = R2((j-1)*224+i); //将数据还原成二维矩阵224X224格式 end end %---G for i = 1:50176 G1(i) = bitand(A(i),65280); end for i = 1:50176 G2(i) = bitshift(G1(i),-8); end for j = 1:224 for i = 1:224 G3(j,i) = G2((j-1)*224+i); end end %---B for i = 1:50176 B1(i) = bitand(A(i),255); end for j = 1:224 for i = 1:224 B2(j,i) = B1((j-1)*224+i); end end %---display image(:,:,1) = R3; //将RGB源信息存在3维数组中 image(:,:,2) = G3; image(:,:,3) = B2; imshow(uint8(image)); //将3维数组信息转化为uint8格式,并显示成图片

Matlab位运算函数:

 

例子:

a = 60; % 60 = 0011 1100   
b = 13; % 13 = 0000 1101 
c = bitand(a, b)      % 12 = 0000 1100  
c = bitor(a, b)       % 61 = 0011 1101 
c = bitxor(a, b)      % 49 = 0011 0001 
c = bitshift(a, 2)    % 240 = 1111 0000 */
c = bitshift(a,-2)    % 15 = 0000 1111 */

运行结果:

c =
    12
c =
    61
c =
    49
c =
   240
c =
    15