使用OpenCV FindCountour函数(在java中)无法在图像中找到轮廓?

问题描述 投票:0回答:1

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个回答
0
投票

可能你正在绘制彼此之上的所有轮廓,最大的轮廓是最后绘制的,它涵盖了所有其他轮廓。

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

© www.soinside.com 2019 - 2024. All rights reserved.