Search code examples
matlabimage-processingthreshold

niblack thresholding


I am trying to implement the niblack thresholding algorithm which uses the formula:

pixel = ( pixel >  mean + k * standard_deviation ) ? object : background

where k has standard value 0. Could someone please tell me how to implement this in matlab? I cant figure out how to do it


Solution

  • The power of Matlab is matrix operations, so you can do a lot without a single for-loop. The code below does what you need.

    % define parameters
    imgname = 'rice.png'; % matlab's image
    filt_radius = 25; % filter radius [pixels]
    k_threshold = 0.2; % std threshold parameter
    %% load the image
    X = double(imread(imgname)); 
    X = X / max(X(:)); % normalyze to [0, 1] range
    %% build filter
    fgrid = -filt_radius : filt_radius;
    [x, y] = meshgrid(fgrid);
    filt = sqrt(x .^ 2 + y .^ 2) <= filt_radius;
    filt = filt / sum(filt(:));
    %% calculate mean, and std
    local_mean = imfilter(X, filt, 'symmetric');
    local_std = sqrt(imfilter(X .^ 2, filt, 'symmetric'));
    %% calculate binary image
    X_bin = X >= (local_mean + k_threshold * local_std);
    %% plot
    figure; ax = zeros(4,1);
    ax(1) = subplot(2,2,1); imshow(X); title('original image');
    ax(2) = subplot(2,2,2); imshow(X_bin); title('binary image');
    ax(3) = subplot(2,2,3); imshow(local_mean); title('local mean');
    ax(4) = subplot(2,2,4); imshow(local_std); title('local std');
    linkaxes(ax, 'xy');
    

    enter image description here