public (Mat, OpenCvSharp.Point) ProcessContours(ref Mat imgPreContours, ref Mat imgGrayscale, ref System.Drawing.Point actuatorPositionPixels) { // Find all contours from image allContours = IPCore.FindAllContours(ref imgPreContours); if (allContours.Count() > 0) { // Get the contours that has the largest area largestContourArea = IPCore.FindLargestContourArea(ref imgGrayscale, ref allContours); // Get largest contour's area center point imgPreContours OpenCvSharp.Point contourCenter = IPCore.FindContourCenter(ref imgPreContours, largestContourArea); // Put center coordinates on image and return it return(IPCore.ComposeImageDTC(ref imgGrayscale, ref largestContourArea, ref contourCenter, ref actuatorPositionPixels), contourCenter); } else { // Return the grayscale image if no contours were found return(imgGrayscale, new OpenCvSharp.Point(int.MinValue, int.MinValue)); } }
// C# 7 tuple syntax // See dummy reference for documentation public (Mat, int) ProcessImage(ref Mat frame) { // Crop image to square croppedImg = IPCore.CropToROI(ref frame); // Convert image to grayscale imgGrayscale = IPCore.ConvertToGrayscale(ref croppedImg); // If checkBoxVideoFeedShowProcessedImage is not checked, return cropped and grayscale image if (IPCore.VideoFeedSettings.IsProcessedImageFed == false) { return(imgGrayscale, 0); } // Set image brightness if necessary if (IPCore.TASettings.Brightness != 0) { imgGrayscale = IPCore.SetImageBrightness(ref imgGrayscale); } // Blur grayscale image imgBlurred = IPCore.SimpleImageBlur(ref imgGrayscale); // Calculate image histogram histogram = IPCore.CalculateHistogram(imgBlurred); // Get maximum histogram value double maxHistValue = IPCore.FindMaxHistogramValue(histogram); // Get index of maximum value of histogram int maxHistIndex = IPCore.FindMaxHistogramIndex(histogram, maxHistValue); // Get value right of histogram, which is smaller or equal that the maximum value divided by a predefined value int dividedValIndex = IPCore.FindHistogramDividedIndex(histogram, Enums.HistogramCalculation.Reversed, maxHistValue, maxHistIndex); // Get threshold value & apply right offset if necessary int thresholdValue = IPCore.GetThresholdValue(dividedValIndex); // Apply threshold filter imgThreshold = IPCore.ThresholdFilter(ref imgBlurred, thresholdValue, ThresholdTypes.Binary); // Invert Image invertedThreshold = IPCore.InvertImage(ref imgThreshold); // Apply median filter imgMedian = IPCore.MedianFilter(ref invertedThreshold); // Find all contours from image allContours = IPCore.FindAllContours(ref imgMedian); // Find largest 2 contors largestTwoContours = IPCore.FindLargestTwoContours(ref imgMedian, ref allContours); if (largestTwoContours.Count() == 2) { this.pointsList = new List <OpenCvSharp.Point>(); foreach (Mat contour in largestTwoContours) { pointsList.Add(IPCore.FindContourCenter(ref imgMedian, contour)); } double distance = IPCore.CalculateEuclidianDistance(pointsList); return(IPCore.ComposeImageIDC(imgGrayscale, largestTwoContours, pointsList, (int)distance), (int)distance); } else { return(imgGrayscale, 0); } }