Niblack Two is a relatively simple local threshold method, the formula for the threshold is T = m + k*v, where m is the average gray value of the region centered on the pixel, V is the standard deviation of the region, K is a factor, and the MATLAB program is as follows:
I = Imread (' card3.bmp '); i = Rgb2gray (i); w = 2;% max = 0; min = 0; [M,n] = size (I); T = zeros (m, n); % For i = (w + 1):(m-w) for j = (W + 1):(n-w) sum = 0; For k =-w:w for L =-w:w sum = sum + uint32 (i (i + k,j + L)); End End average = double (sum)/((2*w+1) * (2*w+1)); s = 0; For k =-w:w for L =-w:w s = s + (UInt32 (i (i + k,j + L))-average) * (UInt32 (I (i + k,j + L))-average); End End s= sqrt (double (s)/((2*w+1) * (2*w+1)); T (i,j) = average + 0.2*s; End end for i = 1:m for j = 1:n If I (i,j) > T (i,j) I (i,j) = uint8 (255); else I (i,j) = uint8 (0); End End End Imshow (I);
The window size selected in the above program is 5*5, the processing result is not good, and a lot of noise is produced ...