尼黑阈值处理

Nee*_*elp 3 matlab image-processing threshold

我正在尝试实现 niblack 阈值算法,该算法使用以下公式:

pixel = ( pixel >  mean + k * standard_deviation ) ? object : background
Run Code Online (Sandbox Code Playgroud)

其中 k 的标准值为 0。有人可以告诉我如何在 matlab 中实现这个吗?我不知道该怎么做

Ser*_*erg 5

Matlab 的强大之处在于矩阵运算,因此您无需使用单个 for 循环即可完成很多工作。下面的代码可以满足您的需要。

% 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');
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述