private MovesDetectionResult DetectMoves(CamService cam) { var maxDartContourArc = configService.MaxContourArcValue * 1.5; var maxDartContourArea = configService.MaxContourAreaValue * 1.5; var maxDartContourWidth = configService.MaxContourWidthValue * 1.5; var minDartContourArc = configService.MinContourArcValue; var minDartContourArea = configService.MinContourAreaValue; var minDartContourWidth = configService.MinContourWidthValue; var allContours = new VectorOfVectorOfPoint(); CvInvoke.FindContours(cam.ThrowExtractedRoiFrame, allContours, new Mat(), RetrType.External, ChainApproxMethod.ChainApproxNone); if (!detectionEnabled || allContours.Size == 0) { return(MovesDetectionResult.Nothing); } var contourWithMaxArc = new VectorOfPoint(); var contourWithMaxArea = new VectorOfPoint(); var contourWithMaxWidth = new VectorOfPoint(); var maxArс = 0.0; var maxArea = 0.0; var maxWidth = 0.0; for (var i = 0; i < allContours.Size; i++) { var tempContour = allContours[i]; var tempContourArс = CvInvoke.ArcLength(tempContour, true); if (tempContourArс > maxArс) { maxArс = tempContourArс; contourWithMaxArc = tempContour; } var tempContourArea = CvInvoke.ContourArea(tempContour); if (tempContourArea > maxArea) { maxArea = tempContourArea; contourWithMaxArea = tempContour; } var rect = CvInvoke.MinAreaRect(tempContour); var box = CvInvoke.BoxPoints(rect); var contourBoxPoint1 = new PointF(box[0].X, (float)cam.roiPosYSlider + box[0].Y); var contourBoxPoint2 = new PointF(box[1].X, (float)cam.roiPosYSlider + box[1].Y); var contourBoxPoint4 = new PointF(box[3].X, (float)cam.roiPosYSlider + box[3].Y); var side1 = MeasureService.FindDistance(contourBoxPoint1, contourBoxPoint2); var side2 = MeasureService.FindDistance(contourBoxPoint4, contourBoxPoint1); var tempContourWidth = side1 < side2 ? side1 : side2; if (tempContourWidth > maxWidth) { maxWidth = tempContourWidth; contourWithMaxWidth = tempContour; } } if (workingMode == DetectionServiceWorkingMode.Crossing || maxArс >= minDartContourArc && maxArс <= maxDartContourArc && maxArea >= minDartContourArea && maxArea <= maxDartContourArea && maxWidth >= minDartContourWidth && maxWidth <= maxDartContourWidth && contourWithMaxArc.Equals(contourWithMaxArea) && contourWithMaxArea.Equals(contourWithMaxWidth) && contourWithMaxWidth.Equals(contourWithMaxArc) ) { return(MovesDetectionResult.Throw); } if (maxArс > maxDartContourArc || maxArea > maxDartContourArea || maxWidth > maxDartContourWidth) { return(MovesDetectionResult.Extraction); } return(MovesDetectionResult.Nothing); }