niblack分割

function imagBW = niblack(imag)
% Reference:
%   Wayne Niblack. An Introduction to Digital Image Processing. pp: 115.
%   1986. Prentice/Hall International. ISBN: 013 480674 3
tic;

k = -0.2;  
b = 80;   % R*R的大小
choice = 1; % 1 for pixel-to-pixel computation, 2 for pixel averaging within the square neighborhood for fast computation.

imag = imag( :, :, 1);
[Hei, Wid] = size(imag);

imag = padarray(imag, [b b], 'symmetric', 'both');  % Pad image array 
Hei_pad = Hei + 2 * b;
Wid_pad = Wid + 2 * b;
imagBW = false(Hei_pad, Wid_pad);
switch choice
    case 1
        for i = 1+b : Hei+b
            for j = 1+b : Wid+b
                upR = i-floor(b/2-1/2);
                dnR = i+floor(b/2);
                lfC = j-floor(b/2-1/2);
                rtC = j+floor(b/2);
                m_ij = mean(mean(imag(upR : dnR, lfC : rtC)));
                sigma_squared = double(imag(upR : dnR, lfC : rtC)) - m_ij;
                sigma_squared = mean(mean(sigma_squared .^2));
                sigma = sqrt(sigma_squared);
                th_ij = m_ij + k * sigma;
                if double(imag(i,j)) > th_ij
                   imagBW(i,j) = 1;
                end
            end
        end
    case 2
        for i = 1+b : b : Hei+b
            for j = 1+b : b : Wid+b
                upR =  i-floor(b/2-1/2);
                dnR =  i+floor(b/2);
                lfC =  j-floor(b/2-1/2);
                rtC =  j+floor(b/2);
                m_ij = mean(mean(imag(upR : dnR, lfC : rtC)));
                sigma_squared = double(imag(upR : dnR, lfC : rtC)) - repmat(m_ij, (dnR-upR+1), (rtC-lfC+1));
                sigma_squared = sigma_squared .^ 2;
                sigma_squared = mean(mean(sigma_squared));
                sigma = sqrt(sigma_squared);
                th_ij = m_ij + k * sigma;
                imagBW(upR : dnR, lfC : rtC) = double(imag(upR : dnR, lfC : rtC)) > th_ij;
            end
        end   
    otherwise
        display('Wrong Choice!');
end
imagBW = imagBW(1+b : Hei+b, 1+b : Wid+b);

toc;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值