2017-11-13 3 views
1
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에서 사용하는 경우opencv Java의 findContours

답변

0

, 당신이 사용하는 것이 좋습니다 :

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

이 당신의 윤곽의 동작을 변경할 수 있습니다. 이게 당신의 버그를 수정했는지 알려주세요. 그렇지 않으면 다른 것을 시도 할 수 있습니다.