Skip to main content

Digital Half-toning Error Diffusion Method (Floyd-Steinberg) - MATLAB Code

Introduction to Digital Half toning is provided in the below link

http://imageprocessing-sankarsrin.blogspot.tw/2016/12/digital-half-toning-ordered-dithering.html


The halftones are generally obtained by thresholding each image pixel with a certain value (ex. 128). In error diffusion, the error is computed between the actual and obtained output and it is distributed to the neighborhood pixels. Floyd-Stein berg, Jarvis, Stucki kernels are widely adopted  to distribute the error.

The Error Diffusion half toning is as follows

1) image(x,y) > 128  Apply thresholding (assume  the image pixel value is 157,  in that case, 255 will be assigned to output). ( as the half toning corresponds to two values 0 and 255)

2) Compute the error between actual and obtained output    E=   255-157= 98

3) In case of floyd steinberg the value 98 is distributed along the neighborhood pixels

4) This steps are performed in sequence and serpentine scan yields superior halftone output. The main problem with error diffusion technique is its sequential processing approach, and in further, dot diffusion technique is introduced to overcome this drawback. 

https://imageprocessing-sankarsrin.blogspot.com/2017/11/dot-diffusion-digital-halftoning-knuth.html



MATLAB CODE: Floyd-Steinberg (Change fc value to execute others) - Reference code from The University of Texas at Austin

function [out, qn, k] = errdiff(in, l, dir, v)

%ERRDIFF Core routine for error diffusion.
% [OUT, QN, K] = ERRDIFF(IN, L, DIR, V) performs error
% diffusion on image IN using error filter FC, modified parameter L,
% and direction DIR, where 1 is raster scan and -1 is serpentine.
% When V (verbose) is non-zero, progress is printed to the output.
% Input range is 0 to 1, as is output range.  Input image is modified
% (no error pipe) for maximum speed.  This routine is the core for all
% error diffusion routines.
%
% Ref: R. Eschbach and K. Knox, "Error diffusion algorithm with
% edge enhancement", J. Opt. Soc. Am. A, Vol. 8, No. 12, December
% 1991, pp. 1844-1850.




if nargin<5 % default to verbose
  v=1; end
if nargin<4 % default to raster
  dir=1; end
if nargin<3 % default to unmodified
  l=0; end


% default to Floyd-Steinberg
  fc=[0 -99*16 7; 3 5 1]/16; 

[ri,ci]=size(in);
[rm,cm]=size(fc);
[r0,c0]=find(fc==-99); % find origin of error filter
fc(r0,c0)=0;

rm=rm-1; cm=cm-1;
inpad=zeros(ri+rm,ci+cm); % modified input image
inpad(r0:r0+ri-1,c0:c0+ci-1)=in;
out=zeros(ri,ci); qn=out;
sp=1; ep=ci; step=1; % for direction changing
r0=r0-1; c0=c0-1;

for y=1:ri
  for x=sp:step:ep
    inpix=inpad(y+r0,x+c0);
    outpix=(inpix+l*in(y,x))>=0.5;
    out(y,x)=outpix;
    qerr=outpix-inpix;
    qn(y,x)=qerr;
    inpad(y:y+rm,x:x+cm)=inpad(y:y+rm,x:x+cm)-qerr*fc;
  end
  if dir==-1
    step=-step; temp=ep; ep=sp; sp=temp;
    fc=fc(:,cm+1:-1:1); end
  if v
   % fprintf('\rDithering... %3d%% done',round(y/ri*100)), 
   end
end

if v
  fprintf('\n')
end

if nargout==3
  xp=out(:)-0.5-qn(:);
  k=sum(abs(xp))/(2*sum(xp.^2));
end







Cheers !!

PYTHON Code: https://github.com/SankarSrin/Digital-Halftoning

Comments

Popular posts from this blog

Image RGB to CMYK : Image Conversion - MATLAB Code (RGB to CMYK, CMYK to RGB)

MATAB use makecform and applycform functions to perform this color model conversion  Ref: makecform:  https://www.mathworks.com/help/images/ref/makecform.html Ref: applycform:  https://www.mathworks.com/help/images/ref/applycform.html RGB to CMYK: I_rgb = imread('lena_color.jpg'); %Read the color image  C = makecform('srgb2cmyk');     %srgb (standard rgb) to cmyk  I_CMYK= applycform(I_rgb,C); TO SAVE IN CMYK Format imwrite(I_CMYK, 'test.tiff'); USE IMFINFO to confirm the conversion imfinfo('test.tiff') the command gives the output ... Width: 512 Height: 512 BitDepth: 32 ColorType: 'CMYK' ... CMYK to RGB: I_CMYK = imread('lena_color.jpg'); %Read the color image  C = makecform('cmyk2srgb');     %srgb (standard rgb) to cmyk  I_rgb= applycform(I_CMYK, C);

Digital Half Toning - Ordered Dithering - MATLAB Code Bayer/ Ulichney

Digital Half-toning is a technique to convert the gray scale / color image (in the range 0-255) to binary images (in the range 0-1)  that is useful for printing (especially black and white printers). Though at first glance dots are clearly visible, as Human Vision System is a low pass filter, when dots are printed closely and viewed from the specific distance it can be perceived as a continuous tone image (i.e. gray scale). In general, half toning techniques is widely used in newspapers, journals and magazines printing.  Halftoning algorithms aims for better #Image Quality #Low Complexity #Optimal ink usage Coming to the topic, Ordered dithering is one of the standard technique to produce halftone image from continuous (gray scale) images. Let us assume, the first block containing 'B' is an image matrix and the  'H' is the threshold matrix. And the process is simply a element wise (pixel wise) comparison; if element of B > H (g