最近我被給了一個項目,在那裡我必須從給定的圖像中提取臉(頭發).
我以下列方式解決這個問題.
>我正在從給定的圖像中提取臉部位置. [我正在找一個矩形]
>我正在提取該矩形并将其放置在與輸入圖像相同尺寸的另一個圖像中.[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算法的性能,使其隻能從給定的圖像中檢測到面部和頭發?