public Rect DetectSoccerCourt(Mat img, ColorRange fieldMarkingsColor, int minBboxArea) { // Mask white lines (includes bars) var lower = ClassicImageProcessor.HsvToScalar(fieldMarkingsColor.Lower); var upper = ClassicImageProcessor.HsvToScalar(fieldMarkingsColor.Upper); var threshImg = img.CvtColor(ColorConversionCodes.BGR2HSV) .InRange(lower, upper); // Find contours threshImg.FindContours(out Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxTC89L1); // Get bboxes and filter out the small ones that are irrelevant var boundingBoxes = ClassicImageProcessor.GetBoundingRects(contours) .Where(rect => Area(rect) > minBboxArea); // Get soccer court elements var corners = GetCorners(boundingBoxes); var touchlines = GetTouchlines(boundingBoxes); var goals = GetGoals(boundingBoxes); var halfWayLines = GetHalfWayLine(threshImg.Size(), boundingBoxes); threshImg = threshImg.CvtColor(ColorConversionCodes.GRAY2BGR); return(new Rect()); }
public Rect DetectBall(Mat img, ColorRange ballColor) { var lower = ClassicImageProcessor.HsvToScalar(ballColor.Lower); var upper = ClassicImageProcessor.HsvToScalar(ballColor.Upper); var threshImg = img .CvtColor(ColorConversionCodes.BGR2HSV) .InRange(lower, upper); threshImg.FindContours(out Point[][] contours, out HierarchyIndex[] hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxTC89L1); var ballRect = ClassicImageProcessor.GetBoundingRects(contours) .OrderBy(rect => rect.Width * rect.Height) .Last(); img.Rectangle(ballRect, new Scalar(255, 0, 0), 2); return(ballRect); }