Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void doGrabCut(){
- Vector2i imgDims = getImageDims();
- //Wite image to OpenCV Mat.
- const Vector4u *rgb = getRGB();
- cv::Mat rgbMat(imgDims.height, imgDims.width, CV_8UC3);
- for (int i = 0; i < imgDims.height; i++) {
- for (int j = 0; j < imgDims.width; j++) {
- int idx = i * imgDims.width + j;
- rgbMat.ptr<cv::Vec3b>(i)[j][2] = rgb[idx].x;
- rgbMat.ptr<cv::Vec3b>(i)[j][1] = rgb[idx].y;
- rgbMat.ptr<cv::Vec3b>(i)[j][0] = rgb[idx].z;
- }
- }
- //Do graph cut.
- cv::Mat res, fgModel, bgModel;
- cv::Rect bb(bb_begin.x, bb_begin.y, bb_end.x - bb_begin.x, bb_end.y - bb_begin.y);
- cv::grabCut(rgbMat, res, bb, bgModel, fgModel, 10, cv::GC_INIT_WITH_RECT);
- cv::compare(res, cv::GC_PR_FGD, res, cv::CMP_EQ);
- //Write mask.
- Vector4u *maskPtr = getMask();//uchar
- for (int i = 0; i < imgDims.height; i++) {
- for (int j = 0; j < imgDims.width; j++) {
- cv::GrabCutClasses classification = res.at<cv::GrabCutClasses>(i, j);
- int idx = i * imgDims.width + j;
- std::cout << classification << std::endl;//Strange numbers here.
- maskPtr[idx].x = (classification == cv::GC_PR_FGD) ? 255 : 0;//This always evaluates to 0.
- }
- }
- cv::Mat foreground(rgbMat.size(), CV_8UC3, cv::Scalar(255, 255, 255));
- rgbMat.copyTo(foreground, res);
- cv::imshow("GC Output", rgbMat);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement