// 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); } }
// C# 7 tuple syntax public (Mat, OpenCvSharp.Point) ProcessImage(ref Mat frame, ref System.Drawing.Point actuatorPositionPixels) { // Crop image to square croppedImg = IPCore.CropToROI(ref frame); // Convert image to grayscale imgGrayscale = IPCore.ConvertToGrayscale(ref croppedImg); // Invert image, if necessary if (IPCore.VideoFeedSettings.IsInverted == true) { imgGrayscale = IPCore.InvertImage(ref imgGrayscale); } // If checkBoxVideoFeedShowProcessedImage is not checked, return grayscale image if (IPCore.VideoFeedSettings.IsProcessedImageFed == false) { return(imgGrayscale, new OpenCvSharp.Point(int.MinValue, int.MinValue)); } // Set image brightness if necessary if (IPCore.TASettings.Brightness != 0) { imgGrayscale = IPCore.SetImageBrightness(ref imgGrayscale); } // Blur grayscale image imgBlurred = IPCore.SimpleImageBlur(ref imgGrayscale); if (IPCore.TASettings.ImgProcAlgorithm == Enums.ImgProcAlgorithm.StaticThresh) { imgPreContours = IPCore.ThresholdFilter(ref imgBlurred, IPCore.TASettings.StaticThresholdValue, ThresholdTypes.Binary); } else if (IPCore.TASettings.ImgProcAlgorithm == Enums.ImgProcAlgorithm.DynamicThresh) { // 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.Normal, 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); // Apply median filter imgPreContours = IPCore.MedianFilter(ref imgThreshold); } else if (IPCore.TASettings.ImgProcAlgorithm == Enums.ImgProcAlgorithm.OstuThresh) { imgPreContours = IPCore.ThresholdFilter(ref imgBlurred, IPCore.TASettings.StaticThresholdValue, ThresholdTypes.Otsu); } return(ProcessContours(ref imgPreContours, ref imgGrayscale, ref actuatorPositionPixels)); }