Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. void doGrabCut(){
  2. Vector2i imgDims = getImageDims();
  3.  
  4. //Wite image to OpenCV Mat.
  5. const Vector4u *rgb = getRGB();
  6. cv::Mat rgbMat(imgDims.height, imgDims.width, CV_8UC3);
  7. for (int i = 0; i < imgDims.height; i++) {
  8. for (int j = 0; j < imgDims.width; j++) {
  9. int idx = i * imgDims.width + j;
  10. rgbMat.ptr<cv::Vec3b>(i)[j][2] = rgb[idx].x;
  11. rgbMat.ptr<cv::Vec3b>(i)[j][1] = rgb[idx].y;
  12. rgbMat.ptr<cv::Vec3b>(i)[j][0] = rgb[idx].z;
  13. }
  14. }
  15.  
  16. //Do graph cut.
  17. cv::Mat res, fgModel, bgModel;
  18. cv::Rect bb(bb_begin.x, bb_begin.y, bb_end.x - bb_begin.x, bb_end.y - bb_begin.y);
  19. cv::grabCut(rgbMat, res, bb, bgModel, fgModel, 10, cv::GC_INIT_WITH_RECT);
  20. cv::compare(res, cv::GC_PR_FGD, res, cv::CMP_EQ);
  21.  
  22. //Write mask.
  23. Vector4u *maskPtr = getMask();//uchar
  24. for (int i = 0; i < imgDims.height; i++) {
  25. for (int j = 0; j < imgDims.width; j++) {
  26. cv::GrabCutClasses classification = res.at<cv::GrabCutClasses>(i, j);
  27. int idx = i * imgDims.width + j;
  28. std::cout << classification << std::endl;//Strange numbers here.
  29. maskPtr[idx].x = (classification == cv::GC_PR_FGD) ? 255 : 0;//This always evaluates to 0.
  30. }
  31. }
  32.  
  33. cv::Mat foreground(rgbMat.size(), CV_8UC3, cv::Scalar(255, 255, 255));
  34. rgbMat.copyTo(foreground, res);
  35. cv::imshow("GC Output", rgbMat);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement