有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中轮廓的opencv并集

在opencv/java中有没有一种有效的方法来计算轮廓的并集并得到最终的轮廓

我试过把opencv.contour转换成java.awt.geom.Area,用Core.bitwise_和,把MatOfPoint合并成曲线和approxPolyDP,但都没有得到好的结果

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


共 (1) 个答案

  1. # 1 楼答案

    一个解决方案是:

    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;