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

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…

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

#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:

[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

error from

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.

1 Like

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…

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.

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;

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…

yea, it won’t change the input image, all information is in mask/bgModel/fgModel
kindly, take a look at the sample code

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

related: