本文主要是介绍【造轮子】纯C++实现的联通组件标记算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
学习《OpenCV应用开发:入门、进阶与工程化实践》一书
做真正的OpenCV开发者,从入门到入职,一步到位!
连接组件标记算法
连接组件标记算法(connected component labeling algorithm-CCL)是图像分析中最常用的算法之一,算法的实质是扫描一幅图像的每个像素,对于像素值相同的分为相同的组(group),最终得到图像中所有的像素连通组件。扫描的方式可以是从上到下,从左到右,对于一幅有N个像素的图像来说,最大连通组件个数为N/2。扫描是基于每个像素单位,对于二值图像而言,连通组件集合可以是V={1|白色}或者V={0|黑色}, 取决于前景色与背景色的不同。对于灰度图像来说,连图组件像素集合可能是一系列在0 ~ 255之间k的灰度值。最常见的连接组件标记算法(CCL) 是两步法,其基本原理与步骤如下:
寻找当前像素点P(x, y)相邻的两个像素P1(x-1, y) 与 P2(x, y-1) 都为0,则标记当前P(x,y) ,否则就就P1跟P2较小的标签赋值给P,然后继续扫描,知道结束,然后再合并所有等价标签,所以两步法可以归纳为:
算法第一步:扫描
算法第二步:合并等价类
C++ 代码实现
第一步先是扫描所有像素,然后标记每个像素,同时记录保存所有的等价类;第二步则是基于LUT查找表直接合并替换所有等价类为最小标签值,完成整个图像的标记。
void CustomCCLAlgo::connectCompoent(cv::Mat &binary, int max_k, int max_v) {int64 start = cv::getTickCount();int h = binary.rows;int w = binary.cols;cv::Mat label = cv::Mat::zeros(binary.size(), CV_32SC1);int index = 0;// apply for memory to useint** equi_labels = new int*[max_k];for (int i = 0; i < max_k; i++)equi_labels[i] = new int[max_v] {};// scan and give label id for each pixelint eq_index = 0;for (int row = 0; row < h; row++) {for (int col = 0; col < w; col++) {int pv = binary.at<uchar>(row, col);if (pv > 0) {int p1 = 0;int p2 = 0;if ((col - 1) >= 0) {p1 = label.at<int>(row, col - 1);}if ((row - 1) >= 0) {p2 = label.at<int>(row-1, col);}int nlabel = std::min(p1, p2);if (p1 == 0 || p2 == 0) {nlabel = p1 + p2;}if (nlabel == 0) {index = index + 1;nlabel = index;}if (p1 > 0 && p2 > 0 && p1 != p2) {int m_row = -1;int m_col = -1;int cnt = 0;int max_p = std::max(p1, p2);for (int i=0; i< eq_index; i++){int* v = equi_labels[i];for (int j = 0; j < max_v; j++) {if (v[j] == 0) {break;}if (v[j] == nlabel) {m_col = j;m_row = i;}if (v[j] == max_p) {cnt = 1;}}}if (m_col >=0 && m_row >=0) {if (cnt == 0) {equi_labels[m_row][m_col + 1] = max_p;} }else {std::vector<int> temp_eq;if (p1 < p2) {equi_labels[eq_index][0] = p1;equi_labels[eq_index][1] = p2;}else {equi_labels[eq_index][0] = p2;equi_labels[eq_index][1] = p1;}eq_index++;}}label.at<int>(row, col) = nlabel;}}}this->numOfLabels = eq_index;// setup lookup tableint total = 0;for (int s = 0; s < eq_index; s++) {int* v = equi_labels[s];for (int c = 0; c < max_v; c++) {if (v[c] == 0) {break;}total++;}}int *a = new int[total+1]();a[0] = 0;for (int s = 0; s < eq_index; s++) {int* v = equi_labels[s];for (int t = 0; t < max_v; t++) {a[v[t]] = s+1;}}// meger labelfor (int row = 0; row < h; row++) {for (int col = 0; col < w; col++) {int pv = label.at<int>(row, col);label.at<int>(row, col) = a[pv];}}//释放空间for (int i = 0; i < max_k; i++)delete[] equi_labels[i];delete[] equi_labels;delete[] a;double ct = (cv::getTickCount() - start) / cv::getTickFrequency();std::cout << "total labels: " << this->numOfLabels << std::endl;printf("connected component execute time : %.5f ms\n", ct * 1000);
}
测试代码
基于4x7的像素块,测试代码如下:
CustomCCLAlgo ccalgo;
cv::Mat binary = cv::Mat::zeros(4, 7, CV_8UC1);
std::cout << "binary: " << binary << std::endl;
binary.at<uchar>(0, 2) = 255;
binary.at<uchar>(0, 5) = 255;binary.at<uchar>(1, 0) = 255;
binary.at<uchar>(1, 1) = 255;
binary.at<uchar>(1, 2) = 255;binary.at<uchar>(1, 4) = 255;
binary.at<uchar>(1, 5) = 255;
binary.at<uchar>(1, 6) = 255;binary.at<uchar>(2, 2) = 255;
binary.at<uchar>(2, 5) = 255;binary.at<uchar>(3, 1) = 255;
binary.at<uchar>(3, 2) = 255;binary.at<uchar>(3, 4) = 255;
binary.at<uchar>(3, 5) = 255;
std::cout << "binary:" << binary << std::endl;ccalgo.connectCompoent(binary, 2, 5);
运行结果如下:
说明代码真的可以了。
后记
这个代码有个很不好的地方,就是没有实现数组的自增长,暂时我都是自己开辟固定长度的,随着扫描的组件数目增多,耗时会不断增加,因为这个有搜索指定标签的环节,如果可以用key来直接替换,或许速度变成跟扫描组件数目增长线性无关的操作,这样速度会更快。
学习《OpenCV应用开发:入门、进阶与工程化实践》一书
做真正的OpenCV开发者,从入门到入职,一步到位!
这篇关于【造轮子】纯C++实现的联通组件标记算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!