利用种子填充法对二值图像进行连通域标记

利用种子填充法对二值图像进行连通域标记

最近一直在用OpenCV做设计,其中有一个设计环节是设计检测二值图像的连通域及其特征,在网上搜了一下算法,看到了有two-pass法和种子填充法两种。两种经典的方法都编程实现过,个人觉得,种子填充法比较直观,不需要像two-pass法那么绕,只需要遍历一遍图像,而且还能顺带计算面积和外接矩形框,

种子填充法原理

关于种子填充法的详细原理可以参考OpenCV_连通区域分析(Connected Component Analysis/Labeling)

大致算法如下:
设二值化图像A中,像素值为255的点是前景,为0的点是背景。A(x, y)为坐标(x, y)处的像素值,定义连通区域的label是一个1~254的整数,互不联通的区域有不同的label。初始化label=0。遍历图像的每个像素:
1、 如果像素值不等于255,则继续访问下一个元素。
2、 如果像素值为A(x, y) = 255,则label++,当前值A(x, y) = label,并且
a. 检查其4个邻域,如果有属于前景的像素也给它赋予label值,并将它的坐标压栈。
b. 弹出栈顶坐标,重复a的过程,知道堆栈为空。
此时,便找到了一个连通区域,该区域内的像素值被标记为label。

3、 重复1、2的过程,检测出所有的区域。

动态演示

废话少说,上代码!

实现程序

该程序基于OpenCV2.4.9 和VS2010平台:


#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++)
        {
            if(pRow[j] == 255)
            {
                area = 0;
                labelValue++;           // labelValue最大为254,最小为1.
                seed = Point(j, i);     // Point(横坐标,纵坐标)
                dst.at<uchar>(seed) = labelValue;
                pointStack.push(seed);

                area++;
                leftBoundary = seed.x;
                rightBoundary = seed.x;
                topBoundary = seed.y;
                bottomBoundary = seed.y;

                while(!pointStack.empty())
                {
                    neighbor = Point(seed.x+1, seed.y);
                    if((seed.x != (cols-1)) && (dst.at<uchar>(neighbor) == 255))
                    {
                        dst.at<uchar>(neighbor) = labelValue;
                        pointStack.push(neighbor);

                        area++;
                        if(rightBoundary < neighbor.x)
                            rightBoundary = neighbor.x;
                    }

                    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;

                    }

                    neighbor = Point(seed.x-1, seed.y);
                    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;
                    }

                    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("shape.jpg", 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);
    }

    imshow("src", src);
    imshow("dst", dst);

    waitKey();
    destroyAllWindows();

    system("pause");
    return 0;
}

运行结果:

原图:
运行结果:src

检测结果:
这里写图片描述

特征清单:
这里写图片描述

请多多指教!

猜你喜欢

转载自blog.csdn.net/XJT2015/article/details/51283387