/* 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(); }
/** * Convert a Graph * into an image * * @param graph The Source graph * @param rows Image Height * @param cols Image Width * * * @return Converted Image */ Mat GRAPHCUT::graph2img(GCGraph<float>& graph, int rows, int cols) { Mat img(rows, cols, CV_32F); for (int r = 0; r < rows; r++) for (int c = 0; c < cols; c++) img.at<float>(r, c) = graph.inSourceSegment(c + img.cols * r); return img; }
/* 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; } } } }