示例#1
0
 private OpenCV.Core.Point[] FindBiggestContourWithFourBorders(IList <MatOfPoint> contours)
 {
     OpenCV.Core.Point[] biggestContour = null;
     Parallel.ForEach(contours, (c) =>
     {
         MatOfPoint2f mat2f = new MatOfPoint2f(c.ToArray());
         double perimeter   = Imgproc.ArcLength(mat2f, true);
         if (perimeter > 1500)
         {
             MatOfPoint2f approx = new MatOfPoint2f();
             Imgproc.ApproxPolyDP(mat2f, approx, 0.02 * perimeter, true);
             if (approx.Total() == 4)
             {
                 MatOfPoint approxMat = new MatOfPoint(approx.ToArray());
                 if (Imgproc.IsContourConvex(approxMat))
                 {
                     if (biggestContour == null)
                     {
                         biggestContour = approx.ToArray();
                         detectedBorder = approx;
                         oldPerimeter   = perimeter;
                     }
                     else
                     {
                         if (oldPerimeter < perimeter)
                         {
                             biggestContour = approx.ToArray();
                             detectedBorder = approx;
                             oldPerimeter   = perimeter;
                         }
                     }
                 }
             }
         }
         mat2f.Dispose();
     });
     return(biggestContour);
 }