Reputation: 124622
I'm currently using MATLAB's bweuler
function to find the number of "holes" in an object. I'm having difficulty due to the relatively low resolution of these images, currently only achieving a 50% success rate. Here is an example of an image that will fail (cropped for testing convenience):
Normalized and scaled up so that you can see what's going on:
The number of holes should be 1 here, but I haven't found a reliable way to threshold that small area out while leaving its surroundings intact.
My current method uses an adaptive threshold, but this is problematic because the correct window size is not constant across all samples. For instance, this example will work with a window size of 6, but others will fail.
For reference, here are a couple more failure cases:
N=1 ->
N=2 ->
And a few cases my current method handles:
N=6 ->
N=1 ->
N=1 ->
I'll post a short version of my current code as well, though as I said previously, I do not believe that an adaptive thresholding method will work. I will be playing with some other morphological segmentation methods in the meantime. Just read one of the test images in and call 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 ([email protected])
% 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
Upvotes: 2
Views: 206