# Can't find docs on grabCut..... (Hair segmentation) (C++)

**URL:** <https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373>\
**Category:** C++\
**Created:** [May 17, 2021, 7:01pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373 "2021-05-17T19:01:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![idk2706](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/idk2706/32/2123_2.png) [@idk2706](https://forum.opencv.org/u/idk2706)\
**Post date:** [May 17, 2021, 7:01pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373/1 "2021-05-17T19:01:31Z")

</div>

So I want to make a software that will segment hair and color it to a different color, I want to do it in C++, I also want to use grabCut, but I can’t find any docs on grabCut for C++, only plain code or docs for python, could anyone provide me with a tutorial or with docs for grabCut??? Also, if you think that there is a better way to do this project, I am open to suggestions… I only ever did facial recognition in C++ openCV… Thank you in advance…

---

<div class="post-metadata">

**Author:** ![idk2706](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/idk2706/32/2123_2.png) [@idk2706](https://forum.opencv.org/u/idk2706)\
**Post date:** [May 17, 2021, 7:59pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373/2 "2021-05-17T19:59:12Z")

</div>

nvm, I figured it out, but know I get this error with my code:

```auto
#include <opencv2/core/core.hpp>
#include "opencv2/objdetect/objdetect.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;

int main() {
    CascadeClassifier face_cascade;
    face_cascade.load("C:\\Users\\path\Downloads\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_alt.xml");
    Mat image = imread("people.png", IMREAD_COLOR);
    
    std::vector<Rect> faces;
    face_cascade.detectMultiScale(image, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
    for (int i = 0; i < faces.size(); i++)
    {
        std::cout << faces[0];
        Mat mask = cv::Mat::ones(image.size(), CV_8U) * cv::GC_BGD;

        //mask.setTo(Scalar::all(GC_BGD));
        //mask.create(image->size(), CV_8UC1);
        Mat bgdModel, fgdModel;
        //mask.setTo(GC_BGD);
        rectangle(image, Point(faces[i].x, faces[i].y ), Point(faces[i].x + faces[i].width , faces[i].y + faces[i].height ), cv::Scalar(0, 255, 0));
        Rect rect = Rect(10, 20, 40, 60);
        grabCut(image, mask, rect , bgdModel, fgdModel, 1, cv::GC_INIT_WITH_MASK);
        
    }

    imshow("Detected Face", image);

    waitKey(0);
    return 0;
}

```

This is my code, and I get this error:

```auto
[44 x 44 from (209, 100)]OpenCV(4.1.2) Error: Assertion failed (!bgdSamples.empty() && !fgdSamples.empty()) in initGMMs, file C:\build\master_winpack-build-win64-vc15\opencv\modules\imgproc\src\grabcut.cpp, line 386

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 17, 2021, 9:07pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373/3 "2021-05-17T21:07:20Z")

</div>

error from

> <https://github.com/opencv/opencv/blob/a604d44d06841565f03da03d8df3124fca639ca3/modules/imgproc/src/grabcut.cpp#L386>

following the code backwards… your mask is completely filled with just one label.

both labels need pixels. a completely uniform mask is useless and it cries about that situation.

---

<div class="post-metadata">

**Author:** ![idk2706](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/idk2706/32/2123_2.png) [@idk2706](https://forum.opencv.org/u/idk2706)\
**Post date:** [May 18, 2021, 4:12pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373/4 "2021-05-18T16:12:45Z")

</div>

I am sorry, but I don’t understand how I should add that part of their code to mine,  
I tried just copy-pasting it, but I got some errors as well, I guess that I should add something else…

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 18, 2021, 4:24pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373/5 "2021-05-18T16:24:17Z")

</div>

> [@idk2706](#):
>
> how I should add that part of their code to mine,

you’re not meant to use that code above in your program, it’s the grabcut src code, and you should try to understand, how it works internally.

> [@crackwitz](#):
>
> your mask is completely filled with just one label.

again, that’s the problem

you probably need to set the face region in the mask to “possible foreground”, if you want to use `cv::GC_INIT_WITH_MASK` , like:

```
mask(faces[i]) = CV::GC_PR_FGD;

```

---

<div class="post-metadata">

**Author:** ![idk2706](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/idk2706/32/2123_2.png) [@idk2706](https://forum.opencv.org/u/idk2706)\
**Post date:** [May 18, 2021, 6:10pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373/6 "2021-05-18T18:10:30Z")

</div>

Thank you a lot, this fixed the error, this is my code now :  
#include \<opencv2/core/core.hpp\>  
#include “opencv2/objdetect/objdetect.hpp”  
#include \<opencv2/highgui/highgui.hpp\>  
#include \<opencv2/imgproc.hpp\>  
#include

using namespace cv;

int main() {  
CascadeClassifier face\_cascade;  
face\_cascade.load(“C:\Users\Mrsovic\Downloads\opencv\build\etc\haarcascades\haarcascade\_frontalface\_alt.xml”);  
Mat image = imread(“images.jpg”, IMREAD\_COLOR);

```
std::vector<Rect> faces;
face_cascade.detectMultiScale(image, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < faces.size(); i++)
{
	std::cout << faces[0];
	Mat mask = cv::Mat::ones(image.size(), CV_8U) * cv::GC_BGD;

	//mask.setTo(Scalar::all(GC_BGD));
	//mask.create(image->size(), CV_8UC1);
	Mat bgdModel, fgdModel;
	//Mat _bgdSamples((int)bgdSamples.size(), 3, CV_32FC1, &bgdSamples[0][0]);
	//mask.setTo(GC_BGD);
	mask(faces[i]) = GC_PR_FGD;
	rectangle(image, Point(faces[i].x, faces[i].y ), Point(faces[i].x + faces[i].width , faces[i].y + faces[i].height ), cv::Scalar(0, 255, 0));
	Rect rect = Rect(faces[i].x, faces[i].y, faces[i].height, faces[i].width);
	grabCut(image, mask, rect , bgdModel, fgdModel, 1, cv::GC_INIT_WITH_MASK);
	
}

imshow("Detected Face", image);

waitKey(0);
return 0;

```

}

Now I need to figure out why it isn’t actually “grabCutting” it,  
I see that grabCut is working, I get some information in console, but at the end my image is still the same, Imma look at the code again, maybe I missed something…

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 18, 2021, 6:20pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373/7 "2021-05-18T18:20:17Z")

</div>

> [@idk2706](#):
>
> but at the end my image is still the same

yea, it won’t change the input image, all information is in mask/bgModel/fgModel  
[kindly, take a look at the sample code](https://github.com/opencv/opencv/blob/master/samples/cpp/grabcut.cpp)

---

<div class="post-metadata">

**Author:** ![idk2706](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/idk2706/32/2123_2.png) [@idk2706](https://forum.opencv.org/u/idk2706)\
**Post date:** [May 18, 2021, 6:36pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373/8 "2021-05-18T18:36:29Z")

</div>

Thank you a lot, for all help, sorry that I bothered you…

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 19, 2021, 7:18pm UTC](https://forum.opencv.org/t/cant-find-docs-on-grabcut-hair-segmentation-c/3373/9 "2021-05-19T19:18:31Z")

</div>

related:

> [@grabCut isn't cutting out the background..... (C++)](https://forum.opencv.org/t/grancut-isnt-cutting-out-the-background-c/3425):
>
> So, this is my code: #include \<opencv2/core/core.hpp\> #include “opencv2/objdetect/objdetect.hpp” #include \<opencv2/highgui/highgui.hpp\> #include \<opencv2/imgproc.hpp\> #include using namespace cv; int main() { CascadeClassifier face\_cascade; face\_cascade.load(“C:\Users\path\Downloads\opencv\build\etc\haarcascades\haarcascade\_frontalface\_alt.xml”); Mat image = imread(“facerec.png”, IMREAD\_COLOR); std::vector faces; face\_cascade.detectMultiScale(image, faces, 1.1, 2, 0 | CASCADE\_SCALE\_…
