最近我被给了一个项目,在那里我必须从给定的图像中提取脸(头发).
我以下列方式解决这个问题.
>我正在从给定的图像中提取脸部位置. [我正在找一个矩形]
>我正在提取该矩形并将其放置在与输入图像相同尺寸的另一个图像中.[face_image]
>我在第2步的face_image上应用了grabCut算法.
当face_image包含平滑背景时,算法grabCut它工作得很好,但是当face_image的背景很复杂时,算法grabCut在处理后的图像中也提取了一部分背景.
这是我获得的结果的快照.
这是我的grabCut代码:
public void extractFace(Mat image, String fileNameWithCompletePath,
int xOne, int xTwo, int yOne, int yTwo) throws CvException {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Rect rectangle = new Rect(xOne, yOne, xTwo, yTwo);
Mat result = new Mat();
Mat bgdModel = new Mat();
Mat fgdModel = new Mat();
Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3));
Imgproc.grabCut(image, result, rectangle, bgdModel, fgdModel, 8, Imgproc.GC_INIT_WITH_RECT);
Core.compare(result, source, result, Core.CMP_EQ);
Mat foreground = new Mat(image.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
image.copyTo(foreground, result);
Imgcodecs.imwrite(fileNameWithCompletePath, foreground);
}
如何提高grabCut算法的性能,使其只能从给定的图像中检测到面部和头发?