gpt4 book ai didi

java - fillPoly 没有按预期工作

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:52 25 4
gpt4 key购买 nike

我正在尝试创建一个用点填充白色的 mask 。我得到的是脸部的虚线轮廓

这是代码

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;
}

这就是我想要的效果

这就是我想要的

最佳答案

正如 Dan Masek 在上面指出的那样,fillPoly 的输入是“多边形数组,其中每个多边形都表示为点数组”。

按照建议,我将地标转换为点数组,并将它们全部推送到 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;
}

关于java - fillPoly 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49463513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com