C++实现查找连通域

目录

一、概述

1.1、四连通域算法

1.2、八连通域算法

1.3、种子填充法

二、代码


一、概述

图像处理中,查找连通域的算法是图像分割的重要方法之一。它能够将一幅图像分成若干个不重叠的区域,每个区域内部像素具有相似的性质,而不同区域之间的像素性质有明显的差异。在实际应用中,查找连通域的算法被广泛应用于数字图像处理、计算机视觉、医学影像分析等领域。

1.1、四连通域算法

四连通域算法是一种基于4邻域搜索的连通域查找算法。该算法认为每个像素与其上下左右四个相邻像素属于同一个连通域。具体步骤如下:

  1. 初始化:选择一个起始点作为种子点,将其标记为已访问;
  2. 搜索:从当前种子点出发,依次搜索其4邻域内的像素点,若该像素点未被访问过,则将其标记为已访问,并将其加入当前连通域;
  3. 更新:将当前连通域内的所有像素点的标签值更新为与种子点相同的标签值;
  4. 重复步骤2和3,直到所有像素点都被访问过。

四连通域算法简单直观,但容易产生过分割的问题。为了解决这个问题,可以采用八连通域算法。

1.2、八连通域算法

八连通域算法是一种基于8邻域搜索的连通域查找算法。该算法认为每个像素与其上下左右以及四个对角线方向上的相邻像素属于同一个连通域。具体步骤与四连通域算法类似,只是在搜索过程中需要考虑更多的相邻像素。

八连通域算法相对于四连通域算法来说,能够更好地避免过分割的问题,但计算量较大。因此,在实际应用中需要根据具体情况选择合适的算法。

1.3、种子填充法

种子填充法是一种基于种子点的连通域查找算法。它首先选择一个或多个种子点作为起始点,然后根据一定的规则(如颜色相似性)向周围扩展,直到所有满足条件的像素都被加入同一连通域。具体步骤如下:

  1. 初始化:选择一个或多个种子点作为起始点,将其标记为已访问;
  2. 搜索:从当前种子点出发,依次搜索其8邻域内的像素点,若该像素点未被访问过且满足条件(如颜色相似),则将其标记为已访问,并将其加入当前连通域;
  3. 更新:将当前连通域内的所有像素点的标签值更新为与种子点相同的标签值;
  4. 重复步骤2和3,直到所有像素点都被访问过。

种子填充法具有较强的灵活性和适应性,可以根据实际需求选择不同的起始点和扩展规则。但容易受到噪声的影响,因此在实际应用中需要进行预处理和后处理。

综上所述,查找连通域的算法是图像处理中的重要方法之一。不同的算法适用于不同的场景和需求,需要根据具体情况进行选择和应用。

二、代码

以下代码依赖opencv读取图片,仅此而已。代码中算法是基于种子填充算法实现,其实还有形成编码法,在不同上下文环境,算法性能不同。例如:图片连通域数量巨大,每个连通区域面积很小,可能形成编码法效率更高。这里把原图丢出来:

以下是完整代码:

#include <opencv2\opencv.hpp>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
using namespace cv;

typedef struct _Feather
{
    int label;              // 连通域的label值
    int area;               // 连通域的面积
    Rect boundingbox;       // 连通域的外接矩形框
} Feather;

/*
Input:src: 待检测连通域的二值化图像
Output:dst: 标记后的图像 featherList: 连通域特征的清单
return:
连通域数量。
*/
int bwLabel(Mat& src, Mat& dst, vector<Feather>& featherList)
{
    int rows = src.rows;
    int cols = src.cols;
    int labelValue = 0;
    Point seed, neighbor;
    stack<Point> pointStack;    // 堆栈
    int area = 0;               // 用于计算连通域的面积
    int leftBoundary = 0;       // 连通域的左边界,即外接最小矩形的左边框,横坐标值,依此类推
    int rightBoundary = 0;
    int topBoundary = 0;
    int bottomBoundary = 0;
    Rect box;                   // 外接矩形框
    Feather feather;
    featherList.clear();    // 清除数组
    dst.release();
    dst = src.clone();
    for (int i = 0; i < rows; i++)
    {
        uchar* pRow = dst.ptr<uchar>(i);
        for (int j = 0; j < cols; j++)
        {
            //【注:一旦连通域赋值为1;遍历当前行的下一行时候,就会自动跳过下面if判断】
            if (pRow[j] == 255)
            {
                area = 0;
                labelValue++;           // labelValue最大为254,最小为1.
                seed = Point(j, i); //(731,49)    // Point(横坐标,纵坐标)(x,y)
                                    //int p = dst.ptr<uchar>(i)[j];
                dst.at<uchar>(seed) = labelValue;//1
                pointStack.push(seed);//坐标压栈
                area++;
                leftBoundary = seed.x;//731
                rightBoundary = seed.x;//731
                topBoundary = seed.y;//49
                bottomBoundary = seed.y;//49
                while (!pointStack.empty())
                {
                    //<1>右邻像素点
                    neighbor = Point(seed.x + 1, seed.y);
                    if ((seed.x != (cols - 1)) && (dst.at<uchar>(neighbor) == 255))
                    {
                        dst.at<uchar>(neighbor) = labelValue;//【注:】赋值便签1,保证不走回头路和分类连通域
                        pointStack.push(neighbor);

                        area++;
                        if (rightBoundary < neighbor.x)//更新边界
                            rightBoundary = neighbor.x;
                    }
                    //<2>下邻像素点
                    neighbor = Point(seed.x, seed.y + 1);
                    if ((seed.y != (rows - 1)) && (dst.at<uchar>(neighbor) == 255))
                    {
                        dst.at<uchar>(neighbor) = labelValue;
                        pointStack.push(neighbor);

                        area++;
                        if (bottomBoundary < neighbor.y)
                            bottomBoundary = neighbor.y;
                    }
                    //<3>左邻像素点
                    neighbor = Point(seed.x - 1, seed.y);
                    //int testx = seed.x;
                    //int testp = dst.at<uchar>(neighbor);
                    if ((seed.x != 0) && (dst.at<uchar>(neighbor) == 255))
                    {
                        dst.at<uchar>(neighbor) = labelValue;
                        pointStack.push(neighbor);
                        area++;
                        if (leftBoundary > neighbor.x)
                            leftBoundary = neighbor.x;
                    }
                    //<4>上邻边界点
                    neighbor = Point(seed.x, seed.y - 1);
                    if ((seed.y != 0) && (dst.at<uchar>(neighbor) == 255))
                    {
                        dst.at<uchar>(neighbor) = labelValue;
                        pointStack.push(neighbor);
                        area++;
                        if (topBoundary > neighbor.y)
                            topBoundary = neighbor.y;
                    }
                    seed = pointStack.top();
                    pointStack.pop();
                }
                box = Rect(leftBoundary, topBoundary, rightBoundary - leftBoundary, bottomBoundary - topBoundary);
                rectangle(src, box, 255);
                feather.area = area;
                feather.boundingbox = box;
                feather.label = labelValue;
                featherList.push_back(feather);
            }
        }
    }
    return labelValue;
}

int main(int argc, char* argv[])
{
    Mat src(imread("d:/Data/123.png", 0));
    if (src.empty())
        exit(-1);
    threshold(src, src, 127, 255, THRESH_BINARY);   // 二值化图像
    vector<Feather> featherList;                    // 存放连通域特征
    Mat dst;
    cout << "连通域数量: " << bwLabel(src, dst, featherList) << endl;
    // 为了方便观察,可以将label“放大”
    for (int i = 0; i < dst.rows; i++)
    {
        uchar* p = dst.ptr<uchar>(i);
        for (int j = 0; j < dst.cols; j++)
        {
            p[j] = 30 * p[j];
        }
    }
    cout << "标号" << "\t" << "面积" << endl;
    for (vector<Feather>::iterator it = featherList.begin(); it < featherList.end(); it++)
    {
        cout << it->label << "\t" << it->area << endl;
        rectangle(dst, it->boundingbox, 255);
    }
    cv::namedWindow("dst");
    imshow("src", src);
    imshow("dst", dst);
    waitKey();
    destroyAllWindows();
    system("pause");
    return 0;
}

执行之后的效果图:

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄家驹beyond

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值