%**************************************************************************
%date :2004_11_18
%Function name:dither_test.m
%Function :simplification the grey image form 8bit to 2bit
%programer :lj
%**************************************************************************
function dither_test() %define the function name
A=double(imread('d:\lena图像\lena_256x256_gray.jpg')); %input the image lenna (grey,8bit,256X256 pixel)
size(A);
imshow(A/255),pause,
[m,n]=size(A);
for i=1:m
for j=1:n
a=A(i,j);
if a<=128
a=a;
else
a=a-255;
end % determind the basic value,when A(i,j)>128,the a is negative;while
% A(i,j)<=128,the a is positive
if (i<m)&&(j==1)
A(i,j+1)=A(i,j+1)+(7*a)/16;
A(i+1,j+1)=A(i+1,j+1)+(1*a)/16;
A(i+1,j)=A(i+1,j)+(5*a)/16;
elseif (i<m)&&(j<n)
A(i,j+1)=A(i,j+1)+(7*a)/16;
A(i+1,j+1)=A(i+1,j+1)+(1*a)/16;
A(i+1,j)=A(i+1,j)+(5*a)/16;
A(i+1,j-1)=A(i+1,j-1)+(3*a)/16;
elseif (i<m)&&(j==n)
A(i+1,j-1)=A(i+1,j-1)+(3*a)/16;
A(i+1,j)=A(i+1,j)+(5*a)/16;
elseif (i==m)&&(j<n)
A(i,j+1)=A(i,j+1)+(7*a)/16;
elseif (i==m)&&(j==n)
A(i,j)=A(i,j);
end
end
end
A=fix(A); % similate the float
for i=1:m
for j=1:n
if A(i,j)<=128
A(i,j)=0;
else
A(i,j)=1;
end
end
end
b=ones(2,2);
A=kron(A,b);
% B=(imread('lenna_gray.bmp'));
% C=dither(B);
% D=im2bw(B);
%**************************************************************************
%Now redfine the matrix of B by four pixel
%**************************************************************************
E=double(imread('d:\lena图像\lena_256x256_gray.jpg'));
for i=1:m
for j=1:n
if (i<m)&&(j==1)
a=(E(i,j)+E(i,j+1)+E(i+1,j+1)+ E(i+1,j))/4;
E(i,j)=a;
E(i,j+1)=a;
E(i+1,j+1)=a;
E(i+1,j)=a;
elseif (i<m)&&(j<n)
a=(E(i,j)+E(i,j+1)+E(i+1,j+1)+ E(i+1,j)+E(i+1,j-1))/5;
E(i,j)=a;
E(i,j+1)=a;
E(i+1,j+1)=a;
E(i+1,j)=a;
E(i+1,j-1)=a;
elseif (i<m)&&(j==n)
a=(E(i,j)+E(i+1,j-1)+E(i+1,j))/3;
E(i,j)=a;
E(i+1,j-1)=a;
E(i+1,j)=a;
elseif (i==m)&&(j<n)
a=(E(i,j)+ E(i,j+1))/2;
E(i,j)=a;
E(i,j+1)=a;
elseif (i==m)&&(j==n)
E(i,j)=E(i,j);
end
end
end
E=fix(E); % similate the float
for i=1:m
for j=1:n
if E(i,j)<=128
E(i,j)=0;
else
E(i,j)=1;
end
end
end
%**************************************************************************
% Now for every pixel,we use a 5X5 Matrix to reconstrcture the image A
% The 5X5 Matrix is magic(5)
%**************************************************************************
%subplot(3,2,1),subimage(B),title('former');
%subplot(3,2,2),subimage(C),title('matlab-dither()');
%subplot(3,2,3),subimage(D),title('matlab-im2bw()');
%subplot(3,2,4),subimage(E),title('Two-values');
%subplot(3,2,5),subimage(A),title('error-diffusion');
imshow(A),title('error-diffusion'),
size(A),save lena_dither.mat A