/* Estimate segmentation using MaxFlow algorithm */ static void estimateSegmentation( GCGraph<double>& graph, InputArray _img) { Mat img = _img.getMat(); Mat result; result.create(img.size(), CV_8UC1); graph.maxFlow(); Point p; for( p.y = 0; p.y < img.rows; p.y++ ) { for( p.x = 0; p.x < img.cols; p.x++ ) { if( graph.inSourceSegment( p.y*img.cols+p.x /*vertex index*/ ) ) { //printf("1\n"); result.at<uchar>(p) = 0; } else { //printf("2\n"); result.at<uchar>(p) = 255; } } } namedWindow("Image",1); imshow("Image", result); waitKey(); }
/* Estimate segmentation using MaxFlow algorithm */ void estimateSegmentation( GCGraph<double>& graph, Mat& mask ) { graph.maxFlow(); Point p; for( p.y = 0; p.y < mask.rows; p.y++ ) { for( p.x = 0; p.x < mask.cols; p.x++ ) { if( mask.at<uchar>(p) == GC_PR_BGD || mask.at<uchar>(p) == GC_PR_FGD ) { if( graph.inSourceSegment( p.y*mask.cols+p.x /*vertex index*/ ) ) mask.at<uchar>(p) = GC_PR_FGD; else mask.at<uchar>(p) = GC_PR_BGD; } } } }