Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV GrabCut Algorithm example not working

Tags:

opencv

I am trying to implement a grabcut algorithm in OpenCV using C++ I stumble upon this site and found a very simple way how to do it. Unfortunately, it seems like the code is not working for me

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( )
{
 // Open another image
    Mat image;
    image= cv::imread("images/mango11a.jpg");

    // define bounding rectangle 
    cv::Rect rectangle(50,70,image.cols-150,image.rows-180);

    cv::Mat result; // segmentation result (4 possible values)
    cv::Mat bgModel,fgModel; // the models (internally used)

    // GrabCut segmentation
    cv::grabCut(image,    // input image
                    result,   // segmentation result
                            rectangle,// rectangle containing foreground 
                            bgModel,fgModel, // models
                            1,        // number of iterations
                            cv::GC_INIT_WITH_RECT); // use rectangle
    cout << "oks pa dito" <<endl;
    // Get the pixels marked as likely foreground
    cv::compare(result,cv::GC_PR_FGD,result,cv::CMP_EQ);
    // Generate output image
    cv::Mat foreground(image.size(),CV_8UC3,cv::Scalar(255,255,255));
    image.copyTo(foreground,result); // bg pixels not copied

    // draw rectangle on original image
    cv::rectangle(image, rectangle, cv::Scalar(255,255,255),1);
    cv::namedWindow("Image");
    cv::imshow("Image",image);

    // display result
    cv::namedWindow("Segmented Image");
    cv::imshow("Segmented Image",foreground);


    waitKey();
    return 0;
}

Can anyone help me with this please? What is supposed to be the problem PS: NO errors were printed while compiling.

like image 989
Mark Avatar asked Nov 12 '22 09:11

Mark


1 Answers

check your settings again. I just executed the same tutorial and it worked fine for me. enter image description here

like image 103
adhg Avatar answered Nov 15 '22 12:11

adhg