骨架提取算法应用

1、引言

2、原理
Zhang和Suen提出了一种带有模板匹配的并行细化算法，生成一个像素宽的骨架，不仅保持图像的连通性，并且产生更薄的结果，保持快速的处理速度。
Zhang-Suen细化算法通常是一个迭代算法，整个迭代过程分为两步

1)2<=N(P1)<=6
2)S(P1)=1
3)P2P4P6=0
4)P4P6P8=0

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 = 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);
}


©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客