1、引言
根据个人理解,骨架提取(顾名思义)就是根据各个连通区域,将其抽离出与其轮廓近似的单像素表示形态。以便于直观观察、图像的后继处理。因此可以将其视为图像处理中的预处理,其操作是基于二值图。为了更好的提取图像骨架,必要时需要对图像进行相应的预处理(比如去噪、滤波、形态学变换等)。
我的应用主要集中在对一些包含线条型的零件检测,除此之外,骨架提取的应用特别广泛,比如文字的检测/识别、道路观测等。
2、原理
Zhang和Suen提出了一种带有模板匹配的并行细化算法,生成一个像素宽的骨架,不仅保持图像的连通性,并且产生更薄的结果,保持快速的处理速度。
Zhang-Suen细化算法通常是一个迭代算法,整个迭代过程分为两步:
第一步:循环所有前景像素点,对符合如下条件的像素点标记为删除:
1)2<=N(P1)<=6
2)S(P1)=1
3)P2P4P6=0
4)P4P6P8=0
其中N(P1)表示跟P1相邻的8个像素点中,为前景像素点的个数,S(P1)表示从P2-P9-P2像素中出现0-1的累积次数,其中0表示背景,1表示前景,完整的P1-P9的像素位置分布如表1:
第二步:
1)2<=N(P1)<=6
2)S(P1)=1
3)P2P4P8=0
4)P2P6P8=0
循环以上两个步骤,直到两步中没有像素被标记为删除为止,输出的结果即为二值图像细化后的骨架。
3、案例核心代码
//Zhang-Sun细化算法
void SkeletonExtraction()
{
//原图像名称
string Img_name = "TEST.png";
//载入源图像
Mat Src = imread(Img_name);
Mat src = Src.clone();
//灰度化
cvtColor(src, src, COLOR_RGB2GRAY);
//Otsu求阈值
int thre = Otsu(src);
Mat Img;
//二值化
threshold(src, Img, thre, 255, THRESH_BINARY_INV);
namedWindow("原始二值化图像", 0);
imshow("原始二值化图像", Img);
Mat srcImg = Img.clone();
/****************骨架提取算法:Zhang-Suen法*****检测焊条数量************************/
vector<Point> deleteList;
int neighbourhood[9];
int row = srcImg.rows;
int col = srcImg.cols;
bool inOddIterations = true;
while (true) {
for (int j = 1; j < (row - 1); j++) {
uchar* data_last = srcImg.ptr<uchar>(j - 1);
uchar* data = srcImg.ptr<uchar>(j);
uchar* data_next = srcImg.ptr<uchar>(j + 1);
for (int i = 1; i < (col - 1); i++) {
if (data[i] == 255) {
int whitePointCount = 0;
neighbourhood[0] = 1;
//判断中心点8邻域的像素特征
if (data_last[i] == 255) neighbourhood[1] = 1;
else neighbourhood[1] = 0;
if (data_last[i + 1] == 255) neighbourhood[2] = 1;
else neighbourhood[2] = 0;
if (data[i + 1] == 255) neighbourhood[3] = 1;
else neighbourhood[3] = 0;
if (data_next[i + 1] == 255) neighbourhood[4] = 1;
else neighbourhood[4] = 0;
if (data_next[i] == 255) neighbourhood[5] = 1;
else neighbourhood[5] = 0;
if (data_next[i - 1] == 255) neighbourhood[6] = 1;
else neighbourhood[6] = 0;
if (data[i - 1] == 255) neighbourhood[7] = 1;
else neighbourhood[7] = 0;
if (data_last[i - 1] == 255) neighbourhood[8] = 1;
else neighbourhood[8] = 0;
for (int k = 1; k < 9; k++) {
//二进制值为1的个数
whitePointCount += neighbourhood[k];
}
//条件①2<=B(p1)<=6
if ((whitePointCount >= 2) && (whitePointCount <= 6)) {
int ap = 0;
//条件②A(p1)值
if ((neighbourhood[1] == 0) && (neighbourhood[2] == 1)) ap++;
if ((neighbourhood[2] == 0) && (neighbourhood[3] == 1)) ap++;
if ((neighbourhood[3] == 0) && (neighbourhood[4] == 1)) ap++;
if ((neighbourhood[4] == 0) && (neighbourhood[5] == 1)) ap++;
if ((neighbourhood[5] == 0) && (neighbourhood[6] == 1)) ap++;
if ((neighbourhood[6] == 0) && (neighbourhood[7] == 1)) ap++;
if ((neighbourhood[7] == 0) && (neighbourhood[8] == 1)) ap++;
if ((neighbourhood[8] == 0) && (neighbourhood[1] == 1)) ap++;
if (ap == 1) {
if (inOddIterations && (neighbourhood[3] * neighbourhood[5] * neighbourhood[7] == 0)
&& (neighbourhood[1] * neighbourhood[3] * neighbourhood[5] == 0)) {
deleteList.push_back(Point(i, j));
}
else if (!inOddIterations && (neighbourhood[1] * neighbourhood[5] * neighbourhood[7] == 0)
&& (neighbourhood[1] * neighbourhood[3] * neighbourhood[7] == 0)) {
deleteList.push_back(Point(i, j));
}
}
}
}
}
}
if (deleteList.size() == 0)
break;
for (size_t i = 0; i < deleteList.size(); i++) {
Point tem;
tem = deleteList[i];
uchar* data = srcImg.ptr<uchar>(tem.y);
data[tem.x] = 0;
}
deleteList.clear();
inOddIterations = !inOddIterations;
}
namedWindow("骨架提取", 0);
imshow("骨架提取", srcImg);
}