【问题标题】:Can't find contours in image using OpenCV FindCountour function(in java)?无法使用 OpenCV FindCountour 函数(在 java 中)在图像中找到轮廓?
【发布时间】:2018-02-12 19:00:04
【问题描述】:

Input Image Output Image

这是我使用 OpenCV 在 Java 中查找轮廓的代码。我首先使用图像来检测图像中的边缘,然后将该图像的位置传递给轮廓函数,但它没有显示任何轮廓。请看一下。我还将输入和输出的图像附加到代码中。

 package test;
 import org.opencv.imgcodecs.Imgcodecs;

 import org.opencv.core.*;
 import org.opencv.imgproc.Imgproc;
 import org.opencv.highgui.Highgui;

 import java.util.*;

 import javax.swing.*;


 public class DMImageFactory {

   public void contours(String location) {

       System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat image =Imgcodecs.imread(location);
        Mat dst=new Mat();

        Core.inRange(image, new Scalar(0,0,0), new Scalar(255,255,255),dst);

        if(dst.empty())
        System.out.print("empty matrix");

        Imshow im = new Imshow("Box1");
        im.showImage(image);

        List<MatOfPoint> contours = new ArrayList<>();
        Mat countour_result = Mat.zeros(image.size(), CvType.CV_8UC3);

       Imgproc.findContours(dst, contours, new Mat(),[enter image description here][1]Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

       Imgproc.drawContours(countour_result, contours, -1,new 
       Scalar(255,255,255));

       Imgcodecs.imwrite("/Users/pranay/Desktop/test/counterdetection_intermidiate.png",countour_result);

        System.out.print(contours+" ");

         for (MatOfPoint contour: contours) 
        Imgproc.fillPoly(countour_result, Arrays.asList(contour), new Scalar(255,255,255));

       Scalar green = new Scalar(81, 190, 0);

       for (MatOfPoint contour: contours) {
        RotatedRect rotatedRect = Imgproc.minAreaRect(new 
        MatOfPoint2f(contour.toArray()));
      drawRotatedRect(countour_result, rotatedRect, green, 4);
    }


     Imgcodecs.imwrite("/Users/pranay/Desktop/test/counterdetection.png",countour_result);


 }

 public static void drawRotatedRect(Mat image, RotatedRect rotatedRect, 
 Scalar color, int thickness) {
      Point[] vertices = new Point[4];
      rotatedRect.points(vertices);
      MatOfPoint points = new MatOfPoint(vertices);
      Imgproc.drawContours(image, Arrays.asList(points), -1, color, 
      thickness);
    }


 }

【问题讨论】:

  • 您的图像链接看起来像交换了:输入图像看起来像输出

标签: opencv javacv


【解决方案1】:

可能您将所有轮廓相互叠加,最后绘制最大的一个,它覆盖了所有其他轮廓。

尝试在新图像中绘制每个轮廓。

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 2012-01-06
    • 2011-06-13
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    相关资源
    最近更新 更多