[Image Processing] Merging of Equivalent Pairs in Binary Image Connected Region Markers

Recently, in the process of digital image processing practice, I encountered the problem of labeling connected regions. There are many explanations of related algorithms on the Internet. I refer to this article . According to the author's explanation, the labeling of connected regions can be easily realized.

I wrote a simple program to implement the third step in the stroke-based tagging process - "merging of equivalent pairs". Hope it can be a reference for you guys.

Among them, equivalences stores all equivalent pairs, and overlap stores all merged results.

void MergeRuns(vector<pair<int,int>>equivalences,  vector<vector<int>>&overlap)
{
    for(int i=0; i<equivalences.size();i++)
    {
        vector<int>cur;     //存放当前正在合并的团

        //初始化当前合并的团(即把第一个未合并的等价对放入其中)
        int first = equivalences[i].first;
        int second = equivalences[i].second;    
        cur.push_back(first);
        cur.push_back(second);

        //从第一个未合并的等价对之后开始对比判断
        for(int j=i+1;j<equivalences.size();j++)
        {
            int pre_size = cur.size();  //当前合并团在该次合并前的大小
            for(int k=0;k<cur.size();k++)
            {
                //判断当前合并团是否与当前等价对有重叠,
                //如果重叠,则将另一团标号放入当前合并团,并跳出循环
                if(cur[k] == equivalences[j].first)
                {
                    cur.push_back(equivalences[j].second);
                    vector<pair<int,int>>::iterator iter = equivalences.begin()+j;
                    equivalences.erase(iter);
                    iter = equivalences.begin();
                    break;
                }
                else if(cur[k]==equivalences[j].second)
                {
                    cur.push_back(equivalences[j].first);
                    vector<pair<int,int>>::iterator iter = equivalences.begin()+j;
                    equivalences.erase(iter);
                    iter = equivalences.begin();
                    break;
                }
            }

            //判断此次合并是否有新团标号加入,
            //如果是,则需从第一个未合并的等价对之后开始对比判断
            if(pre_size!=cur.size())
            {
                j=i;
            }
        }
        overlap.push_back(cur);     //将当前合并完成的团放入结果vector中
    }

}

The result is shown in Figure 1;


Figure 1 Merge results

However, this program has some shortcomings:
1. The generated results are not sorted (it will not affect the final mark, so no optimization is added)
2. Furthermore, the separate group label is not added when merging, that is, it does not exist The clique number of the equivalent pair, such as number 4 in this example.


2017-8-29 Modified
Modified the code, perfected less than 2, to complete the merger of separate group labels

//添加参数int label_num,表示团标号最大值
void MergeRuns(vector<pair<int,int>>equivalences,  vector<vector<int>>&overlap, int label_num)
{
    ...
    //以上为之前代码,未做修改
    //下面为添加无等价对的团标号代码
    //处理方式其实很无脑,就是对所有团标号进行查找,
    //找到不在合并后的团中的就将其放入结果vector中
    for(int i = 0;i<=label_num;i++)
    {
        for(int j=0;j<overlap.size();j++)
        {
            int k=0;
            for(;k<overlap[j].size();k++)
                if(overlap[j][k]==i)
                    break;
            if(k<overlap[j].size())
                break;
            else if(j==overlap.size()-1)
            {
                vector<int>cur;
                cur.push_back(i);
                overlap.push_back(cur);
            }
        }
    }
}

The processing method is actually very brainless. It is to search for all the group labels, and put it into the result vector if it finds that it is not in the merged group.

The result is shown in Figure 2;


Figure 2 Modified merge result

Here my group label is counted from 0, so the result contains 0, which is the individual group label. If starting from 1, modify the code at the beginning of the loop to 1.

Throwing bricks, hope for advice!

Reference article:
http://www.cnblogs.com/ronny/p/img_aly_01.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325722889&siteId=291194637