I0=imread('t.png');
B=rgb2gray(I0)
figure;imshow(B);title('test image');
I=double(B);
count=0;
fid=fopen('test.txt','w');
for i=1:16;
for j=1:16;
count=count+1;
if(count==16)
fprintf(fid,'\n');
end
fprintf(fid,'%x\t',I(i,j));
end
end
fclose(fid);
%本函数可以读取任何以空格,回车,制表符进行分割的纯数据的txt文件
i=0;
j=0;
count=0;
%待写入的矩阵大小的参数
nSize=262144;%因为上述矩阵共有512*512个数据
nRow=512;
nColumn=512;
%读取txt文件,因为是16进制,按要求将其换算为10进制的
a=textread('test.txt','%s')';%以字符形式打开文件
alpha=hex2dec(a); %16进制转化为10进制数,存入alpha矩阵
for i=1:16
for j=1:16
count=count+1;
A(i,j)=alpha(count);
end
end
C=uint8(double(A))
%本函数可以读取任何以空格,回车,制表符进行分割的纯数据的txt文件
figure;imshow(C);title('changed image');