本文介绍了niblack阈值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下公式实现niblack阈值算法:
I am trying to implement the niblack thresholding algorithm which uses the formula:
pixel = ( pixel > mean + k * standard_deviation ) ? object : background
其中k的标准值为0.
有人可以告诉我怎么样在matlab中实现这个?我无法弄清楚如何做到这一点
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
推荐答案
Matlab的强大功能是矩阵操作,所以你可以做很多事情而不用一个for循环。
下面的代码可以满足您的需求。
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');
这篇关于niblack阈值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!