C++ 二值图像连通区域标记

这篇文章最初发表在http://blog.csdn.net/j56754gefge/article/details/38777267,均是我原创,他人转载请注明出处!


Labeling connected components in binary images (C++ source code)


因为需要做连通区域标记,Matlab里有现成的算法,但在C++编程的时候发现没有合适的工具可用,从网上找代码,C代码不多,在咱们的csdn博客上找到了两个:

[1]http://blog.csdn.net/icvpr/article/details/10259577

[2]http://blog.csdn.net/augusdi/article/details/8863843

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,我来回答你的问题。首先,连通区域标记是图像处理中常用的一种算法,可以用来对图像中的连通区域进行标记,常用于图像分割、目标检测等领域。 二次扫描算法是一种比较常用的连通区域标记算法,下面我将用C++OpenCV来实现这个算法。 首先,我们需要加载一张图像并对其进行二值化处理,这里我使用OpenCV中的cv::threshold函数来实现: ```cpp cv::Mat src = cv::imread("test.jpg", cv::IMREAD_GRAYSCALE); cv::Mat dst; cv::threshold(src, dst, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU); ``` 接下来,我们需要定义一个结构体来表示标记信息: ```cpp struct LabelInfo { int label; int area; int xmin; int ymin; int xmax; int ymax; }; ``` 其中,label表示标记的编号,area表示标记的面积,xmin、ymin、xmax、ymax表示标记的最小外接矩形。 然后,我们需要定义一个函数来实现二次扫描算法: ```cpp void scan(cv::Mat& img, std::vector<LabelInfo>& labels) { int rows = img.rows; int cols = img.cols; int label = 0; std::vector<int> parent(rows * cols); // 第一次扫描 for (int i = 0; i < rows; i++) { int* data = img.ptr<int>(i); for (int j = 0; j < cols; j++) { if (data[j] != 0) { int left = (j == 0) ? 0 : parent[i * cols + j - 1]; int up = (i == 0) ? 0 : parent[(i - 1) * cols + j]; if (left == 0 && up == 0) { label++; parent[i * cols + j] = label; labels.push_back({ label, 1, j, i, j, i }); } else if (left != 0 && up == 0) { parent[i * cols + j] = left; labels[left - 1].area++; labels[left - 1].xmax = std::max(labels[left - 1].xmax, j); labels[left - 1].ymax = i; } else if (left == 0 && up != 0) { parent[i * cols + j] = up; labels[up - 1].area++; labels[up - 1].xmax = j; labels[up - 1].ymax = std::max(labels[up - 1].ymax, i); } else { if (left != up) { int minlabel = std::min(left, up); int maxlabel = std::max(left, up); parent[i * cols + j] = minlabel; labels[minlabel - 1].area += labels[maxlabel - 1].area + 1; labels[minlabel - 1].xmax = std::max(labels[maxlabel - 1].xmax, j); labels[minlabel - 1].ymax = std::max(labels[maxlabel - 1].ymax, i); for (int k = 0; k < rows * cols; k++) { if (parent[k] == maxlabel) { parent[k] = minlabel; } } labels[maxlabel - 1].area = 0; labels[maxlabel - 1].xmin = 0; labels[maxlabel - 1].ymin = 0; labels[maxlabel - 1].xmax = 0; labels[maxlabel - 1].ymax = 0; } else { parent[i * cols + j] = left; labels[left - 1].area++; labels[left - 1].xmax = std::max(labels[left - 1].xmax, j); labels[left - 1].ymax = std::max(labels[left - 1].ymax, i); } } } } } // 第二次扫描 for (int i = 0; i < rows; i++) { int* data = parent.data() + i * cols; for (int j = 0; j < cols; j++) { if (data[j] != 0) { data[j] = labels[data[j] - 1].label; } } } } ``` 最后,我们可以调用这个函数来进行连通区域标记: ```cpp std::vector<LabelInfo> labels; scan(dst, labels); ``` 这样就完成了用C++OpenCV实现连通区域标记的二次扫描算法的过程。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值