【二值化】Niblack算法

Niblack二值化算法是比较简单的局部阈值方法,阈值的计算公式是T = m + k*v,其中m为以该像素点为中心的区域的平均灰度值,v是该区域的标准差,k是一个系数,matlab程序如下:

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);

上述程序中选择的窗口大小是5*5,处理结果不好,产生了大量的噪声...

你可能感兴趣的:(算法,matlab)