连通分量及最大连通分量

void FindMaxComponent(const vector> contours, vector& contour)
{
    int idx = 0;
    int contour_size = 0;
    for (int i = 1; i < contours.size(); i++)
    {
	contour_size = contours[0].size();
	if (contour_size < contours[i].size())
	{
	    idx = i;
	}			
    }
    contour = contours[idx];
}

void FindConetComponents(const Mat& src, Mat& lableimage, vector>& contours)
{
    Point top(0, -1);
    Point bottom(0, 1);
    Point left(-1, 0);
    Point right(1, 0);
	
    int lable = 0;
	
    for (int r = 0; r < src.rows; r++)	
    {		
        for (int c = 0; c < src.cols; c++)
	{			
            if (src.at(r, c) > 0				
                && lableimage.at(r, c) == 0)			
            {
		stack store;
		store.push(Point(c, r));
		++lable;

		vector temp;
		while (!store.empty())
		{
		    Point curr = store.top();
		    temp.push_back(curr);
		    lableimage.at(curr) = lable;
    
		    store.pop();

		    if (src.at(curr + top) > 0
		        && lableimage.at(curr + top) == 0)
		    {
		        store.push(curr + top);
		    }
		    if (src.at(curr + left) > 0
	                && lableimage.at(curr + left) == 0)
		    {
		        store.push(curr + left);
		    }
		    if (src.at(curr + bottom) > 0
		        && lableimage.at(curr + bottom) == 0)
		    {
		        store.push(curr + bottom);
		    }
		    if (src.at(curr + right) > 0
		        && lableimage.at(curr + right) == 0)
	            {
		        store.push(curr + right);
		    }
	        }
	        contours.push_back(temp);
            }
        }
    }
}

 

你可能感兴趣的:(C++)