Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Image processing problem: halftoning by using matrix and computing the averages.

ID: 3887814 • Letter: I

Question

Image processing problem: halftoning by using matrix and computing the averages. Please show the Matlab code (prefer typed for clarity) and explanation is greatly appreciated. Thanks in advance.

Title: Binary Image Rendering using Halftoning. Halftoning is a technique that uses patterns of black and white dots to simulate a grayscale image. It is useful when an output device can only print or display black or white. The figure below shows 3x3 patterns of black and white dots that you will use to represent blocks of 3x3 pixels in grayscale images. That is, the input will be a grayscale image whose pixels have values from 0 to 255 and the output will be an image that only contains two values, one corresponding to black and the other to white. 4 6 (Ignore the fact that the dots above are circular. They should really be square.) Your assignment: a) In Matlab, write the function halftone that takes a grayscale image with values 0-255 as its input (this input should be a matrix, not a filename) and returns a binary (two-valued) image (again a matrix) as its output. The input and output should be of type uint8. Your function should use the 10 dot patterns above to transform the grayscale image to the binary image. You will have to decide on this transformation. Importantly, your input and output images should have the same size (number of pixels). Include the code for this function in your lab report. Write a test script that generates a test pattern image consisting of a gray scale "wedge" of size 256x256, whose first column is all 0, the next column is all 1, and so on, with the last column b)

Explanation / Answer

a)

%Program for Image Halftoning by Floyd Method

%Program Description
% The input gray image will be converted into halftone image
% of same size using Floyd's Error Diffusion Method.
%
%Parameters
% inImg - Input Gray Image
% outImg - Output Halftoned Image

%Author : Athi Narayanan S
%Student, M.E, EST,
%K.S.R College of Engineering
%Erode, Tamil Nadu, India.
%s_athi1983@yahoo.co.in
%http://sites.google.com/site/athisnarayanan/

function outImg = floydHalftone(inImg)
inImg = double(inImg);

[M,N] = size(inImg);
T = 127.5; %Threshold
y = inImg;
error = 0;

for rows = 1:M-1
  
%Left Boundary of Image
outImg(rows,1) =255*(y(rows,1)>=T);
error = -outImg(rows,1) + y(rows,1);
y(rows,1+1) = 7/16 * error + y(rows,1+1);
y(rows+1,1+1) = 1/16 * error + y(rows+1,1+1);
y(rows+1,1) = 5/16 * error + y(rows+1,1);
  
for cols = 2:N-1
%Center of Image
outImg(rows,cols) =255*(y(rows,cols)>=T);
error = -outImg(rows,cols) + y(rows,cols);
y(rows,cols+1) = 7/16 * error + y(rows,cols+1);
y(rows+1,cols+1) = 1/16 * error + y(rows+1,cols+1);
y(rows+1,cols) = 5/16 * error + y(rows+1,cols);
y(rows+1,cols-1) = 3/16 * error + y(rows+1,cols-1);
end
  
%Right Boundary of Image
outImg(rows,N) =255*(y(rows,N)>=T);
error = -outImg(rows,N) + y(rows,N);
y(rows+1,N) = 5/16 * error + y(rows+1,N);
y(rows+1,N-1) = 3/16 * error + y(rows+1,N-1);
  
end

%Bottom & Left Boundary of Image
rows = M;
outImg(rows,1) =255*(y(rows,1)>=T);
error = -outImg(rows,1) + y(rows,1);
y(rows,1+1) = 7/16 * error + y(rows,1+1);

%Bottom & Center of Image
for cols = 2:N-1
outImg(rows,cols) =255*(y(rows,cols)>=T);
error = -outImg(rows,cols) + y(rows,cols);
y(rows,cols+1) = 7/16 * error + y(rows,cols+1);
end

%Thresholding
outImg(rows,N) =255*(y(rows,N)>=T);

outImg = im2bw(uint8(outImg));