Hello Friends, How can i create a mask image in opencv and create trimap segments. Please guide its urgent.

asked 2013-12-09 07:38:24 -0500

Joystna gravatar image

updated 2013-12-09 07:46:32 -0500

Hello Friends,

I want to create a mask image and i want to segment it with trimap. and put that mask image on the original image and segment it F & B image. actually i am trying Gracut where i need to create a mask image and segment it and apply it on the original image. Below is my source code and have got result but it is not according to the exact result. Please guide me with the source code to create mask image and put that on original image. YOur kind help will be highly appreciated.

Thank you in advance. Joystna

include <opencv2 core="" core.hpp="">

include <opencv2 highgui="" highgui.hpp="">

include <opencv2 imgproc="" imgproc.hpp="">

include <iostream>

using namespace cv; using namespace std;

int main( ) { // Open another image Mat image; image = cv::imread("2.png");

if(! image.data ) // Check for invalid input
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;

// define bounding rectangle

  cv::Rect rectangle(20,35,image.cols-40,image.rows-70);

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
// Get the pixels marked as likely foreground
// 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,0,0),5);

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

return 0;

} C:\fakepath\Untitled.png

edit retag flag offensive close merge delete