Java中的findContours

Java中的findContours

  if (DETECT_RED_OBJECTS_ONLY)
  {
    Imgproc.cvtColor(inputFrame, gray,
    Imgproc.COLOR_BGR2RGB);
  }
  else
  {
     Imgproc.cvtColor(inputFrame, gray, Imgproc.COLOR_BGR2GRAY);
  }
     Imgproc.cvtColor(inputFrame, dst, Imgproc.COLOR_BGR2RGB);


     // down-scale and upscale the image to filter out the noise
     Imgproc.pyrDown(gray, downscaled, new Size(gray.cols() / 2, gray.rows()  2));
     Imgproc.pyrUp(downscaled, upscaled, gray.size());

     if (DETECT_RED_OBJECTS_ONLY) {
         // convert the image from RGBA to HSV
         Imgproc.cvtColor(upscaled, hsv, Imgproc.COLOR_RGB2HSV);
         // threshold the image for the lower and upper HSV red range
         Core.inRange(hsv, HSV_LOW_RED1, HSV_LOW_RED2, lowerRedRange);
         Core.inRange(hsv, HSV_HIGH_RED1, HSV_HIGH_RED2, upperRedRange);
         // put the two thresholded images together


         Core.addWeighted(lowerRedRange, 1.0, upperRedRange, 1.0, 0.0, bw);
         // apply canny to get edges only
         System.out.println(bw);
         Imgproc.Canny(bw, bw, 0, 255);
     } else {
         // Use Canny instead of threshold to catch squares with gradient shading
         Imgproc.Canny(upscaled, bw, 0, 255);
     }


    // dilate canny output to remove potential
     // holes between edge segments
     Imgproc.dilate(bw, bw, new Mat(), new Point(-1, 1), 1);

     // find contours and store them all as a list
     List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
     contourImage = bw.clone();
     Imgproc.findContours(
             contourImage,
             contours,
             hierarchyOutputVector,
             Imgproc.RETR_EXTERNAL,
             Imgproc.CHAIN_APPROX_SIMPLE
     );

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

     // loop over all found contours
     for (MatOfPoint cnt : contours) {
         MatOfPoint2f curve = new MatOfPoint2f(cnt.toArray());

         // approximates a polygonal curve with the specified precision
         Imgproc.approxPolyDP(
                 curve,
                 approxCurve,
                 0.02 * Imgproc.arcLength(curve, true),
                 true
         );

         int numberVertices = (int) approxCurve.total();

当我将三角形用作输入图像时,此图形将起作用并检测三角形[计数器变为1,numberVertices变为3]。但是当我输入矩形图像时,轮廓变为2,numberVertices变为2。它必须为4。是不是谁能帮我找到错误。

最佳答案

如果您在最新的Visual Studio中使用此功能,建议您使用:

JavaList List<MatOfPoint> contours = new JavaList<MatOfPoint>();

这可能会改变轮廓的行为。请让我知道这是否解决了您的错误。否则,请尝试其他方法

关于java - opencv Java中的findContours,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47259808/

10-13 00:06