clear;
load demo;
figure;
imshow(demo);
[a, b]=size(demo);
dout=zeros(a,b);
len=(a-2)*(b-2);
duut=zeros(9, len);
for i=2:a-1
    for j=2:b-1
        tmp=[demo(i-1,j-1),demo(i-1,j), demo(i-1,j+1),demo(i,j-1),demo(i,j),demo(i,j+1),demo(i+1,j-1),demo(i+1,j), demo(i+1,j+1)];
        tmp=sort(tmp);
        dout(i,j)=tmp(5);
        k=(i-2)*(b-2)+j-1;
        tflag(k)=k;
        duut(:,k)=tmp;
    end
end
figure;
dout1=uint8(dout);
imshow(dout1);

fid1 = fopen('din1tt.dat', 'wt');
dout1 = duut(1,:);
fprintf(fid1, '%x\n', dout1);
fclose(fid1);

fid2 = fopen('din2tt.dat', 'wt');
dout2 = duut(2,:);
fprintf(fid2, '%x\n', dout2);
fclose(fid2);

fid3 = fopen('din3tt.dat', 'wt');
dout3 = duut(3,:);
fprintf(fid3, '%x\n', dout3);
fclose(fid3);

fid4 = fopen('din4tt.dat', 'wt');
dout4 = duut(4,:);
fprintf(fid4, '%x\n', dout4);
fclose(fid4);

fid5 = fopen('din5tt.dat', 'wt');
dout5 = duut(5,:);
fprintf(fid5, '%x\n', dout5);
fclose(fid5);

fid6 = fopen('din6tt.dat', 'wt');
dout6 = duut(6,:);
fprintf(fid6, '%x\n', dout6);
fclose(fid6);

fid7 = fopen('din7tt.dat', 'wt');
dout7 = duut(7,:);
fprintf(fid7, '%x\n', dout7);
fclose(fid7);

fid8 = fopen('din8tt.dat', 'wt');
dout8 = duut(8,:);
fprintf(fid8, '%x\n', dout8);
fclose(fid8);

fid9 = fopen('din9tt.dat', 'wt');
dout9 = duut(9,:);
fprintf(fid9, '%x\n', dout9);
fclose(fid9);


