如何在OpenCV中绘制填充形状

时间:2018-01-24 11:35:13

标签: opencv

我有一个点列表,我想根据它们绘制填充形状,我已尝试填充形状但是这段代码,但它只画了5点

List<Point> points = new ArrayList<>();
points.add(new Point(0, 0));
points.add(new Point(100, 0));
points.add(new Point(100, 100));
points.add(new Point(0, 100));
points.add(new Point(50, 50));
Imgproc.polylines(imgMAT, matOfPoints, true, new Scalar(255, 0, 0), 8);

我也用过fillpoly

Imgproc.fillPoly(imgMAT, matOfPoints, new Scalar(255, 0, 0));

但没有任何变化,我的问题在哪里

enter image description here

感谢

1 个答案:

答案 0 :(得分:1)

看看this tutorial

import org.opencv.core.*;
import org.opencv.core.Point;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import java.util.*;
import java.util.List;

class GeometricDrawingRun{

    private static final int W = 100;
    public void run(){
        String window = "Drawing Polygon";
        Mat image = Mat.zeros( W, W, CvType.CV_8UC3 );
        MyPolygon( image );

        HighGui.imshow( window, image );
        HighGui.waitKey( 0 );
        System.exit(0);
    }

    private void MyPolygon( Mat img ) {
        int lineType = 8;
        int shift = 0;

        List<Point> points = new ArrayList<>();
        points.add(new Point(0, 0));
        points.add(new Point(100, 0));
        points.add(new Point(100, 100));
        points.add(new Point(0, 100));
        points.add(new Point(50, 50));
        MatOfPoint matPt = new MatOfPoint();
        matPt.fromList(points);
        List<MatOfPoint> ppt = new ArrayList<MatOfPoint>();
        ppt.add(matPt);

        Imgproc.fillPoly(img,
                ppt,
                new Scalar( 255, 255, 255 ),
                lineType,
                shift,
                new Point(0,0) );
    }
}
public class BasicGeometricDrawing {
    public static void main(String[] args) {
        // Load the native library.
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        new GeometricDrawingRun().run();
    }
}

结果:

enter image description here