matlab练习程序(Floyd–Steinberg dithering)

close all;
clear all;
clc;
img=imread('pic.bmp');
imshow(img);
[m n]=size(img);

re=zeros(m,n);
tmp=zeros(m+2,n+2);
tmp(2:m+1,2:n+1)=img;

for i=2:m+1
for j=2:n+1
if tmp(i,j)<128
re(i-1,j-1)=0;
err=tmp(i,j);
else
re(i-1,j-1)=255;
err=tmp(i,j)-255;
end
tmp(i,j+1)=tmp(i,j+1)+7/16*err;
tmp(i+1,j-1)=tmp(i+1,j-1)+3/16*err;
tmp(i+1,j)=tmp(i+1,j)+5/16*err;
tmp(i+1,j+1)=tmp(i+1,j+1)+1/16*err;
end
end

figure,imshow(re);

图片福利:

matlab练习程序(Floyd–Steinberg dithering)                                   

matlab练习程序(Floyd–Steinberg dithering)

你可能感兴趣的:(matlab)