首页 > 解决方案 > java中轮廓的联合

问题描述

在 opencv/java 中是否有一种有效的方法来计算轮廓的并集并获得结果轮廓?

我曾尝试将opencv.contourto java.awt.geom.Area、 with Core.bitwise_and 转换为合并MatOfPoint到 curve and approxPolyDP,但没有给出好的结果。

是唯一的方法,drawContour用于黑色图像上的轮廓 1 和轮廓 2,然后使用findContour

标签: javaopencv

解决方案


一种解决方案是:

MatOfPoint contoursIntersection( Mat ref, MatOfPoint cnt1, MatOfPoint cnt2) {

        MatOfPoint intersec = new MatOfPoint();

        Mat blackImage = new Mat();
        ref.copyTo( blackImage);
        blackImage.setTo( new Scalar( 0, 0, 0));

        List<MatOfPoint> coll = new ArrayList<MatOfPoint>();
        coll.add(cnt1);
        coll.add(cnt2);
        Imgproc.drawContours(blackImage, coll, 0, new Scalar(255,0,0), Core.FILLED);
        Imgproc.drawContours(blackImage, coll, 1, new Scalar(255,0,0), Core.FILLED);
        //Imgproc.fillPoly( blackImage, coll, new Scalar(0,0,255));
        blackImage.copyTo( frameIntersection);

        Imgproc.cvtColor( blackImage, blackImage, Imgproc.COLOR_BGR2GRAY);

        List<MatOfPoint> contours = new ArrayList< MatOfPoint>();
        Mat hierarchy = new Mat();
        Imgproc.findContours( blackImage, contours, hierarchy,  Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE );

        System.out.println("===== contoursIntersection > number = " + contours.size());

        if (contours.size() == 1)   {
            intersec = contours.get(0);
        }
        return intersec;

推荐阅读