gpt4 book ai didi

matlab - 尼黑阈值处理

转载 作者:行者123 更新时间:2023-12-02 05:46:39 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

enter image description here

关于matlab - 尼黑阈值处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9871084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com