目标4连通且单连通。
E是外角数目,I是内角数目。
procedure count_objects(B)
{
E=0;
I=0;
for L=0 to MaxRow-1
for P=0 to MaxCol-1
{
if external_match(L,P) then E++;
if internal_match(L,P) then I++;
}
return (E-I)/4;
}
其中的external_match(L,P)和internal_match(L,P)由自己完成,外角的匹配就是4个2*2的模板,三个值为0一个值为1,分别在4个方向。
而内角匹配相同,只不过是三个1一个0在4个方向。两个函数的设计就是扫描图像矩阵,用模板覆盖,完全覆盖则返回真,否则返回假。
procedure external_match(L,P)
{
if f[L,P] is 0 and f[L,P+1] is 0 and f[L+1,P] is 0 and f[L+1,P+1] is 1
then return true;
if f[L,P] is 0 and f[L,P+1] is 0 and f[L+1,P] is 1 and f[L+1,P+1] is 0
then return true;
if f[L,P] is 1 and f[L,P+1] is 0 and f[L+1,P] is 0 and f[L+1,P+1] is 0
then return true;
if f[L,P] is 0 and f[L,P+1] is 1 and f[L+1,P] is 0 and f[L+1,P+1] is 0
then return true;
else return false;
}
在识别前景物体的时候,可以先对图像进行选定阈值的二值化,再进行外角和内角计算匹配,可粗略得出前景物体数目。