2014-03-02 145 views
0

我試圖使用OpenCV4Android SDK和我的Android手機相機檢測形狀(三角形和正方形)。 到目前爲止,我需要修改這部分的代碼,但我不知道如何使用openCv Approxpoly函數來檢測這些形狀 任何幫助將不勝感激。在OpenCV中檢測三角形Approxpoly

public void process(Mat rgbaImage) 
     { 
      Imgproc.pyrDown(rgbaImage, mPyrDownMat); 
      Imgproc.pyrDown(mPyrDownMat, mPyrDownMat); 

     Imgproc.cvtColor(mPyrDownMat, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL); 

     Core.inRange(mHsvMat, mLowerBound, mUpperBound, mMask); 
     Imgproc.dilate(mMask, mDilatedMask, new Mat()); 

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

     Imgproc.findContours(mDilatedMask, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 

     // Find max contour area 
     double maxArea = 0; 
     Iterator<MatOfPoint> each = contours.iterator(); 
     while (each.hasNext()) 
     { 
      MatOfPoint wrapper = each.next(); 
      double area = Imgproc.contourArea(wrapper); 
      if (area > maxArea) 
       maxArea = area; 
     } 

     //Imgproc.approxPolyDP(mSpectrum, approxCurve, epsilon, closed); 

     // Filter contours by area and resize to fit the original image size 
     mContours.clear(); 
     each = contours.iterator(); 

     while (each.hasNext()) 
     { 
      MatOfPoint contour = each.next(); 
      if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) 
      { 
       Core.multiply(contour, new Scalar(4,4), contour); 
       mContours.add(contour); 
      } 
     } 
    } 

回答

3

由於輪廓檢測返回的類型略有不同,以及PolyPolyDP期望的類型稍有不同。看看這個功能,我開發了幾乎做你在找什麼:

public static boolean isContourSquare(MatOfPoint thisContour) { 

    Rect ret = null; 

    MatOfPoint2f thisContour2f = new MatOfPoint2f(); 
    MatOfPoint approxContour = new MatOfPoint(); 
    MatOfPoint2f approxContour2f = new MatOfPoint2f(); 

    thisContour.convertTo(thisContour2f, CvType.CV_32FC2); 

    Imgproc.approxPolyDP(thisContour2f, approxContour2f, 2, true); 

    approxContour2f.convertTo(approxContour, CvType.CV_32S); 

    if (approxContour.size().height == 4) { 
     ret = Imgproc.boundingRect(approxContour); 
    } 

    return (ret != null); 
} 

好了,使用這個功能在你的代碼,我會用這樣的:

public static List<MatOfPoint> getSquareContours(List<MatOfPoint> contours) { 

    List<MatOfPoint> squares = null; 

    for (MatOfPoint c : contours) { 

     if ((ContourUtils.isContourSquare(c)) { 

      if (squares == null) 
       squares = new ArrayList<MatOfPoint>(); 
      squares.add(c); 
     } 
    } 

    return squares; 
} 

所以在你的代碼,後:

Imgproc.findContours(mDilatedMask, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 

,你可以作出這樣一個電話:

List<MatOfPoint> squareContours = getSquareContours(contours); 
然後個

squareContours只會有方形輪廓(或三角形,如果像你說的,你使用值3檢查approxContour.size()高度時。)

,那麼你可以用剩下的代碼如下繼續:

// Filter contours by area and resize to fit the original image size 
    mContours.clear(); 
    each = squareContours.iterator(); 

    while (each.hasNext()) 
    { 
     MatOfPoint contour = each.next(); 
     if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) 
     { 
      Core.multiply(contour, new Scalar(4,4), contour); 
      mContours.add(contour); 
     } 
    } 
+0

我想我明白代碼在做什麼,但是如何在上面的代碼中實現?我想爲了檢測三角形,輪廓尺寸應該是3。 – Iker