我目前正在使用 MATLAB 的bweuler
函数来查找对象中“孔”的数量。由于这些图像的分辨率相对较低,我遇到了困难,目前只有 50% 的成功率。这是一个将失败的图像示例(为方便测试而裁剪):
标准化和放大,以便您可以看到发生了什么:
此处的孔数应为 1,但我还没有找到一种可靠的方法来限制该小区域,同时保持其周围环境完好无损。
我当前的方法使用自适应阈值,但这是有问题的,因为正确的窗口大小在所有样本中都不是恒定的。例如,此示例将使用大小为 6 的窗口,但其他示例将失败。
作为参考,这里有几个失败的案例:
N=1 ->
N=2 ->
还有一些我目前的方法处理的情况:
N=6 ->
N=1 ->
N=1 ->
我也会发布我当前代码的简短版本,尽管正如我之前所说,我不相信自适应阈值方法会起作用。与此同时,我将使用其他一些形态分割方法。只需阅读其中一张测试图像并调用findholes(image)
.
function N = findholes(I)
bw = imclearborder(adaptivethreshold(I, 9));
N = abs(1 - bweuler(bw2, 4));
end
function bw=adaptivethreshold(IM,ws,C,tm)
%ADAPTIVETHRESHOLD An adaptive thresholding algorithm that seperates the
%foreground from the background with nonuniform illumination.
% bw=adaptivethreshold(IM,ws,C) outputs a binary image bw with the local
% threshold mean-C or median-C to the image IM.
% ws is the local window size.
% C is a constant offset subtracted from the final input image to im2bw (range 0...1).
% tm is 0 or 1, a switch between mean and median. tm=0 mean(default); tm=1 median.
%
% Contributed by Guanglei Xiong (xgl99@mails.tsinghua.edu.cn)
% at Tsinghua University, Beijing, China.
%
% For more information, please see
% http://homepages.inf.ed.ac.uk/rbf/HIPR2/adpthrsh.htm
if (nargin < 2)
error('You must provide IM and ws');
end
if (nargin<3)
C = 0;
tm = 0;
end
if (nargin==3)
tm=0;
elseif (tm~=0 && tm~=1)
error('tm must be 0 or 1.');
end
IM=mat2gray(IM);
if tm==0
mIM=imfilter(IM,fspecial('average',ws),'replicate');
else
mIM=medfilt2(IM,[ws ws]);
end
sIM=mIM-IM-C;
bw=im2bw(sIM,0);
bw=imcomplement(bw);
end