grabCut isn't cutting out the background..... (C++)

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_IMAGE, Size(100, 100));
for (int i = 0; i < faces.size(); i++)
{
std::cout << faces[0];
Mat mask = cv::Mat::ones(image.size(), CV_8U) * cv::GC_BGD;
Mat bgdModel, fgdModel;
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);
cv::Mat dstImage = cv::Mat::zeros(image.size(), image.type());
image.copyTo(dstImage, mask);
imshow(“Detected Face”, dstImage);
}
waitKey(0);
return 0;
}
This code first finds a face, and then I want to remove the rest of the background, the problem is that it doesn’t remove it, I do get a mask on my black image, but it looks just like if someone cut the area within the rectangle rect , it didn’t remove the rest of the background, can someone help?

may I suggest that you give your code some formatting? there’s a button for that. select the code and click the button.

image

related:

I tried, but it didn’t work, sorry, I probably did something wrong…