Output image after performing grabcut using openCV C++ sample code [closed]

asked 2016-07-13 22:34:11 -0600

shekar95 gravatar image

updated 2016-07-13 23:06:58 -0600

I am new to openCV, I am trying to implement Grabcut. I am using the Grabcut example code provided by openCV in samples (same as the one in OpenCV Github repo). Link Here.

However, if you run this code you will get the output in the same window as the input and the output image cannot be used in the program for post processing.

Consider the code snippet below.

void GCApplication::showImage() const
{
    if( image->empty() || winName->empty() )
        return;

    Mat res;
    Mat binMask;
    if( !isInitialized )
        image->copyTo( res );
    else
    {
        getBinMask( mask, binMask );
        image->copyTo( res, binMask );
    }

    vector<Point>::const_iterator it;
    for( it = bgdPxls.begin(); it != bgdPxls.end(); ++it )
        circle( res, *it, radius, BLUE, thickness );
    for( it = fgdPxls.begin(); it != fgdPxls.end(); ++it )
        circle( res, *it, radius, RED, thickness );
    for( it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it )
        circle( res, *it, radius, LIGHTBLUE, thickness );
    for( it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it )
        circle( res, *it, radius, PINK, thickness );

    if( rectState == IN_PROCESS || rectState == SET )
        rectangle( res, Point( rect.x, rect.y ), Point(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);

    imshow( *winName, res );
}

How can this showImage() function can be modified so that it returns the final segmented image(after all iterations). Please suggest a method to do this (without using the imwrite() function).

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-11-07 02:12:42.566352

Comments

why not return 'res' from your function ?

berak gravatar imageberak ( 2016-07-14 00:03:07 -0600 )edit

Please suggest a way to do that. I changed void showImage() to cv::Mat& showImage() to return 'res', but as the function calls are intricate in the program it is throwing errors. Where in the function should I return it ?

shekar95 gravatar imageshekar95 ( 2016-07-14 00:15:38 -0600 )edit

res is a local var, so return a plain Mat, not a reference.

berak gravatar imageberak ( 2016-07-14 01:22:23 -0600 )edit