OpenCV watershed 源码分析

¨ 考文献:

Meyer, F.Color Image Segmentation, ICIP92,1992

基本思想:

算法流程:

(1)首先得到mark标记图,标记区域的值为1,2...L,未标记区域为0,分水岭区域为-1(图像边界预先标记为分水岭)

(2)将mark区域的四邻域加入到优先级队列,优先级为0,1,...255 ,取决于两像素点rgb距离中最大的值(也可以称为棋盘距离吧)

while(!queue.empty())

   从优先级最高的队列开始,出队

       如果,其4邻域不存在其他label,那么4邻域就统统归它所有,同样对于俘获的像素也安装其与该出队的元素之间的距离作为优先级加入队列

       如果,其周围已经有其它的label了,那么画地为界,该像素充当分水岭,标记为-1



OpenCV watershed 源码分析_第1张图片

优先级队列及一维算法示例:

OpenCV watershed 源码分析_第2张图片

核心数据结构:

OpenCV watershed 源码分析_第3张图片

源码注释:

typedef struct CvWSNode	//像素节点
{
    struct CvWSNode* next;
    int mask_ofs;	//该节点对于mask中的偏移
    int img_ofs;	//该节点对于原图像中的偏移
}
CvWSNode;

typedef struct CvWSQueue	//同等级像素队列
{
    CvWSNode* first;
    CvWSNode* last;
}
CvWSQueue;

static CvWSNode*
icvAllocWSNodes( CvMemStorage* storage )
{
    CvWSNode* n = 0;
	//改内存块的总大小,减去内存块链接需要的空间,/一个节点的大小,得到的就是能存放节点的个数
	//减去1,可能是为了安全起见
    int i, count = (storage->block_size - sizeof(CvMemBlock))/sizeof(*n) - 1;
	//向该内存块要空间,搞成线性链表
    n = (CvWSNode*)cvMemStorageAlloc( storage, count*sizeof(*n) );
    for( i = 0; i < count-1; i++ )
        n[i].next = n + i + 1;
    n[count-1].next = 0;

    return n;
}


CV_IMPL void
cvWatershed( const CvArr* srcarr, CvArr* dstarr )
{
    const int IN_QUEUE = -2;		//进入队列标记
    const int WSHED = -1;	//分水岭标记
    const int NQ = 256;		//队列个数
    cv::Ptr<CvMemStorage> storage;	//内存块

    CvMat sstub, *src;
    CvMat dstub, *dst;
    CvSize size;
    CvWSNode* free_node = 0, *node;	//内存块的空闲内存指针,指向第一个未被占用的节点
    CvWSQueue q[NQ];	//可以看成数组实现的特殊队列
    int active_queue;	//当前要分类的队列,其实链表更准确
    int i, j;
    int db, dg, dr;	//d:距离  r,g,b
    int* mask;
    uchar* img;
    int mstep, istep;	//mask步长,image步长
    int subs_tab[513];

    // MAX(a,b) = b + MAX(a-b,0),要先看看subs_tab的赋值才能理解
    #define ws_max(a,b) ((b) + subs_tab[(a)-(b)+NQ])		//我去,比较大小搞这么复杂
    // MIN(a,b) = a - MAX(a-b,0)
    #define ws_min(a,b) ((a) - subs_tab[(a)-(b)+NQ])

    #define ws_push(idx,mofs,iofs)  \
    {                               \
        if( !free_node )            \
			//一次性分配很多空间,得到一个节点链表,要用的时候从里面取节点
            free_node = icvAllocWSNodes( storage );\	
        node = free_node;           \
        free_node = free_node->next;\	//取一个节点给node
        node->next = 0;             \
        node->mask_ofs = mofs;      \
        node->img_ofs = iofs;       \
        if( q[idx].last )           \
            q[idx].last->next=node; \	//跟在比人屁股后面去吧
        else                        \
            q[idx].first = node;    \	//嘿嘿,你是第一个哦
        q[idx].last = node;         \	
    }

    #define ws_pop(idx,mofs,iofs)   \
    {                               \
        node = q[idx].first;        \
        q[idx].first = node->next;  \
        if( !node->next )           \
            q[idx].last = 0;        \
        node->next = free_node;     \
        free_node = node;           \
        mofs = node->mask_ofs;      \
        iofs = node->img_ofs;       \
    }

    #define c_diff(ptr1,ptr2,diff)      \	//计算两像素之差,结果为r,g,b距离最大者
    {                                   \
        db = abs((ptr1)[0] - (ptr2)[0]);\
        dg = abs((ptr1)[1] - (ptr2)[1]);\
        dr = abs((ptr1)[2] - (ptr2)[2]);\
        diff = ws_max(db,dg);           \
        diff = ws_max(diff,dr);         \
        assert( 0 <= diff && diff <= 255 ); \
    }

    src = cvGetMat( srcarr, &sstub );	//取Mat 信息头,sstub在输入参数为Mat的情况下没有用到
    dst = cvGetMat( dstarr, &dstub );

    if( CV_MAT_TYPE(src->type) != CV_8UC3 )
        CV_Error( CV_StsUnsupportedFormat, "Only 8-bit, 3-channel input images are supported" );

    if( CV_MAT_TYPE(dst->type) != CV_32SC1 )
        CV_Error( CV_StsUnsupportedFormat,
            "Only 32-bit, 1-channel output images are supported" );

    if( !CV_ARE_SIZES_EQ( src, dst ))
        CV_Error( CV_StsUnmatchedSizes, "The input and output images must have the same size" );

    size = cvGetMatSize(src);
    storage = cvCreateMemStorage();	//默认要64K的空间

    istep = src->step;	// image step
    img = src->data.ptr;	//pointer to 8-bit unsigned elements !!!
    mstep = dst->step / sizeof(mask[0]);		//mask step
	//mstep的作用是方便移到某一行,比如 mask+mstep,是要移到下一行,但对于mask指针来说其对象为 32S
	//所以直接加 每一行的字节数就相当于跳到 mask+4*mstep
    mask = dst->data.i;	//pointer to 32-bit signed elements  !!!

    memset( q, 0, NQ*sizeof(q[0]) );

    for( i = 0; i < 256; i++ )
        subs_tab[i] = 0;
    for( i = 256; i <= 512; i++ )
        subs_tab[i] = i - 256;

    // draw a pixel-wide border of dummy "watershed" (i.e. boundary) pixels
	//上下两行标记为分水岭
    for( j = 0; j < size.width; j++ )
        mask[j] = mask[j + mstep*(size.height-1)] = WSHED;

	/*-----------------初始化:将标记区域4邻域的点加入优先级队列q-----------------------------*/
    // initial phase: put all the neighbor pixels of each marker to the ordered queue -
    // determine the initial boundaries of the basins
	//除边框外,把标记 4 邻域的像素加入队列,q有256个优先级,对应距离0-255,所以具体加入哪个
	//队列就看距离最小idx了
    for( i = 1; i < size.height-1; i++ )
    {
        img += istep; mask += mstep;
        mask[0] = mask[size.width-1] = WSHED;	//左右两端标为分水岭

        for( j = 1; j < size.width-1; j++ )
        {
            int* m = mask + j;
            if( m[0] < 0 ) m[0] = 0;	//此处归零是为了防止意外情况		
			//mask中值为0的为未分类的像素	
			//还没有归宿,但是其4邻域有标记区,那就入队,等待分配区域标号
            if( m[0] == 0 && (m[-1] > 0 || m[1] > 0 || m[-mstep] > 0 || m[mstep] > 0) )
            {
                uchar* ptr = img + j*3;	//三通道
                int idx = 256, t;
				//计算该点到其四邻域中最近的距离
                if( m[-1] > 0 )	//左
                    c_diff( ptr, ptr - 3, idx );
                if( m[1] > 0 )	//右
                {
                    c_diff( ptr, ptr + 3, t );
                    idx = ws_min( idx, t );
                }
                if( m[-mstep] > 0 )	//上
                {
                    c_diff( ptr, ptr - istep, t );
                    idx = ws_min( idx, t );
                }
                if( m[mstep] > 0 )	//下
                {
                    c_diff( ptr, ptr + istep, t );
                    idx = ws_min( idx, t );
                }
                assert( 0 <= idx && idx <= 255 );
                ws_push( idx, i*mstep + j, i*istep + j*3 );	//入队
                m[0] = IN_QUEUE;
            }
        }
    }

    // find the first non-empty queue
	//比如 q[0]表示所有标记区附近与标记区距离为0的点,自然很可能没有这样的点
    for( i = 0; i < NQ; i++ )
        if( q[i].first )
            break;

    // if there is no markers, exit immediately
    if( i == NQ )
        return;

    active_queue = i;
    img = src->data.ptr;
    mask = dst->data.i;

    // recursively fill the basins
    for(;;)
    {
        int mofs, iofs;
        int lab = 0, t;
        int* m;
        uchar* ptr;
		/*-----------------划分:如果其4邻域只有一个集水区那就归入其中,如果有多个那就标记为分水岭-----------------------------*/
        if( q[active_queue].first == 0 )	//当前层级处理完或无像素入队,换下一层
        {
            for( i = active_queue+1; i < NQ; i++ )
                if( q[i].first )	//换这层
                    break;
            if( i == NQ )	//退出大循环
                break;
            active_queue = i;	//下面给这一层的像素分配标号
        }
		//取一个点
        ws_pop( active_queue, mofs, iofs );	

        m = mask + mofs;	//mask的值
        ptr = img + iofs;	//img的值
		//4邻域只有一个区域,就滚到那里面去,要是处在多个集水区,那你就做大坝吧
        t = m[-1];
        if( t > 0 ) lab = t;
        t = m[1];
        if( t > 0 )
        {
            if( lab == 0 ) lab = t;
            else if( t != lab ) lab = WSHED;
        }
        t = m[-mstep];
        if( t > 0 )
        {
            if( lab == 0 ) lab = t;
            else if( t != lab ) lab = WSHED;
        }
        t = m[mstep];
        if( t > 0 )
        {
            if( lab == 0 ) lab = t;
            else if( t != lab ) lab = WSHED;
        }
        assert( lab != 0 );
        m[0] = lab;
        if( lab == WSHED )
            continue;	//为啥不接着找呢?
	    /*-----------------扩散:将标记区域4邻域的点中尚未分类的点加入优先级队列q-----------------------------*/
        if( m[-1] == 0 )		//左
        {
            c_diff( ptr, ptr - 3, t );
            ws_push( t, mofs - 1, iofs - 3 );
            active_queue = ws_min( active_queue, t );	//有更近的点,下一轮优先
            m[-1] = IN_QUEUE;	//标记为入队
        }
        if( m[1] == 0 )	//右
        {
            c_diff( ptr, ptr + 3, t );
            ws_push( t, mofs + 1, iofs + 3 );
            active_queue = ws_min( active_queue, t );
            m[1] = IN_QUEUE;
        }
        if( m[-mstep] == 0 )	//上
        {
            c_diff( ptr, ptr - istep, t );
            ws_push( t, mofs - mstep, iofs - istep );
            active_queue = ws_min( active_queue, t );
            m[-mstep] = IN_QUEUE;
        }
        if( m[mstep] == 0 )	//下
        {
            c_diff( ptr, ptr + istep, t );
            ws_push( t, mofs + mstep, iofs + istep );
            active_queue = ws_min( active_queue, t );
            m[mstep] = IN_QUEUE;
        }
    }
}


void cv::watershed( InputArray _src, InputOutputArray markers )
{
    Mat src = _src.getMat();
    CvMat c_src = _src.getMat(), c_markers = markers.getMat();
    cvWatershed( &c_src, &c_markers );
}


 

 

 

你可能感兴趣的:(源码分析,opencv,watershed)