fillPoly没有按预期工作

时间:2018-03-24 10:13:44

标签: java android opencv

我正在尝试创建一个从点填充白色的面罩。我得到了脸部的虚线轮廓

这是代码

public Mat getFaceMask(Mat image){

        Mat faceMask =  Mat.zeros(image.size(), image.type());

        List<MatOfPoint> faceConvexHull = new ArrayList<>();
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(197, 154)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(190, 197)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(182, 217)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(170, 234)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(155, 251)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(138, 264)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(121, 268)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(104, 265)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(88, 253)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(73, 238)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(61, 221)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(51, 202)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(46, 181)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(42, 159)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(41, 137)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(49, 116)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(73,  95)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(164,  95)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(190, 115)));
        faceConvexHull.add(new MatOfPoint(new org.opencv.core.Point(197, 133)));

        fillPoly(faceMask, faceConvexHull, new Scalar(255, 255, 255));

        return faceMask;
    }

这是我想要的效果

这就是我得到的

1 个答案:

答案 0 :(得分:0)

就像Dan Masek上面指出的那样,fillPoly的输入是&#34;多边形数组,其中每个多边形表示为点数组。&#34;

按照建议我将我的地标转换为一系列点,然后将它们全部推送到一个MatOfPoint实例

public Mat getFaceMask(Mat image){

    List<Point> faceConvexHull = new ArrayList<>();
    faceConvexHull.add(new Point(197, 154));
    faceConvexHull.add(new Point(190, 197));
    faceConvexHull.add(new Point(182, 217));
    faceConvexHull.add(new Point(170, 234));
    faceConvexHull.add(new Point(155, 251));
    faceConvexHull.add(new Point(138, 264));
    faceConvexHull.add(new Point(121, 268));
    faceConvexHull.add(new Point(104, 265));
    faceConvexHull.add(new Point(88, 253));
    faceConvexHull.add(new Point(73, 238));
    faceConvexHull.add(new Point(61, 221));
    faceConvexHull.add(new Point(51, 202));
    faceConvexHull.add(new Point(46, 181));
    faceConvexHull.add(new Point(42, 159));
    faceConvexHull.add(new Point(41, 137));
    faceConvexHull.add(new Point(49, 116));
    faceConvexHull.add(new Point(73,  95));
    faceConvexHull.add(new Point(164,  95));
    faceConvexHull.add(new Point(190, 115));
    faceConvexHull.add(new Point(197, 133));

    org.opencv.core.Point [] pointArray = new org.opencv.core.Point[faceConvexHull.size()];

    Point pt;
    for(int i = 0; i < faceConvexHull.size(); i++){
        pt = faceConvexHull.get(i);
        pointArray[i] = new org.opencv.core.Point(pt.x, pt.y);
    }



    Mat faceMask =  Mat.zeros(image.size(), CV_8UC3);

    MatOfPoint points = new MatOfPoint(pointArray);

    fillConvexPoly(faceMask, points, new Scalar(255, 255, 255));

    return faceMask;
}