【问题标题】:Removed contours aren't gone移除的轮廓没有消失
【发布时间】:2017-08-11 00:09:25
【问题描述】:

我正在尝试删除任何不是方形的轮廓。我检查前后的图像,看看是否有任何轮廓被删除。我使用圆形度公式,0.7 到 0.8 之间的值是方形的。我希望看到一些轮廓线被删除,但没有一个是

这是我到目前为止所做的。

public static void main(String[] args) {


        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat capturedFrame = Imgcodecs.imread("first.png");

        //Gray
        Mat gray = new Mat();
        Imgproc.cvtColor(capturedFrame, gray, Imgproc.COLOR_BGR2GRAY);

        //Blur
        Mat blur = new Mat();
        Imgproc.blur(gray, blur, new Size(3,3));
        //Canny image
        Mat canny = new Mat();
        Imgproc.Canny(blur, canny, 20, 40, 3, true);


        Imgcodecs.imwrite("test.png", canny);

        //Dilate image to increase size of lines
        Mat kernel = Imgproc.getStructuringElement(1, new Size(3,3));
        Mat dilated = new Mat();
        Imgproc.dilate(canny,dilated, kernel);


        List<MatOfPoint> contours = new ArrayList<>();
        //find contours
        Imgproc.findContours(dilated, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE);

        //convert image 
        Imgproc.cvtColor(capturedFrame, capturedFrame, Imgproc.COLOR_BGR2RGB);


        //Draw contours on original image
        for(int n = 0; n < contours.size(); n++){
            Imgproc.drawContours(capturedFrame, contours, n, new Scalar(255, 0 , 0), 1);
        }

        Imgcodecs.imwrite("before.png", capturedFrame);

        //display image with all contours
        Imshow showImg = new Imshow("displayImage");
        showImg.show(capturedFrame);


        //Remove contours that aren't close to a square shape.
        for(int i = 0; i < contours.size(); i++){

            double area = Imgproc.contourArea( contours.get(i)); 
            MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
            double perimeter = Imgproc.arcLength(contour2f, true);

            //Found squareness equation on wiki... 
            // https://en.wikipedia.org/wiki/Shape_factor_(image_analysis_and_microscopy)
            double squareness = 4 * Math.PI * area / Math.pow(perimeter, 2);

            System.out.println("Squareness: " + squareness);


            if(squareness <= 0.7 && squareness >=  0.8){
                contours.remove(i);
            }
        }


        for(int i = 0; i < contours.size(); i++){
            Imgproc.drawContours(capturedFrame, contours, i, new Scalar(0, 255, 0), 1);
        }

        showImg.show(capturedFrame);
        Imgcodecs.imwrite("remove.png", capturedFrame);

    }

这是原图:

这是删除任何轮廓之前的图像:

这是图像的最终图像,其中轮廓应删除一些轮廓:

【问题讨论】:

  • 删除轮廓时我看到的第一件事是,您需要减少 counter i 。
  • 我还有什么需要做的吗? @AndreySmorodov

标签: opencv contour


【解决方案1】:

squareness &lt;= 0.7 &amp;&amp; squareness &gt;= 0.8 看起来不可能是你mean squareness &lt;= 0.7 || squareness &gt;= 0.8 的条件吗?

【讨论】:

  • 我的意思是方形度大于0.7小于0.8
  • 它怎么可能同时小于 lo 界和大于 up 界?尝试在纸上绘制间隔并将点放在那里。可能是方形 >= 0.7 && 方形
  • 我只想删除不在 0.7 和 0.8 之间的值
  • 好的,那么不是方形度 = 0.8 而是方形度 = 0.8 .
  • 好吧,如果我要添加介于 0.7 和 0.8 之间的轮廓,会是我发布的内容吗?
猜你喜欢
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 2022-01-07
  • 2012-01-04
  • 2020-09-26
相关资源
最近更新 更多