繁体   English   中英

java中轮廓的并集

[英]union of contour in java

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

我已经尝试将opencv.contour转换为java.awt.geom.Area ,使用Core.bitwise_和,将MatOfPoint合并到 curve 和approxPolyDP ,但没有给出好的结果。

是唯一的方法,在黑色图像drawContour用于 contour1 和 contour2,然后使用findContour吗?

一种解决方案是:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM