Java中的轮廓并集

时间:2019-07-05 09:31:55

标签: java opencv

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

我尝试将opencv.contour转换为java.awt.geom.Area并使用Core.bitwise_,并合并MatOfPoint到曲线和approxPolyDP,但是没有什么能给您带来好处结果。

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

1 个答案:

答案 0 :(得分:0)

一种解决方案是:

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;