static Point?GetFirstDifferenceWith(Arr first, Arr second) { var difference = new Mat(first.Size, Depth.U8, 1); CV.Cmp(first, second, difference, ComparisonOperation.NotEqual); for (int row = 1, rowEnd = difference.Rows - 1; row < rowEnd; row++) { for (int col = 1, colEnd = difference.Cols - 1; col < colEnd; col++) { if (difference.GetReal(row, col) != 0.0) { return(new Point(col, row)); } } } return(null); }
public void ComputeGradient(Arr source, GradientOperator gradientOperator, double gradientThreshold) { // gaussian filtering CV.Smooth(source, _smoothed, SmoothMethod.Gaussian, 5, 5, 1.0); // calculate gradients switch (gradientOperator) { case GradientOperator.Prewitt: CV.Filter2D(_smoothed, _gradientX, PrewittX); CV.Filter2D(_smoothed, _gradientY, PrewittY); if (gradientThreshold < 0.0) { gradientThreshold = 6.0; } break; case GradientOperator.Sobel: CV.Sobel(_smoothed, _gradientX, 1, 0); CV.Sobel(_smoothed, _gradientY, 0, 1); break; case GradientOperator.Scharr: CV.Sobel(_smoothed, _gradientX, 1, 0, -1); CV.Sobel(_smoothed, _gradientY, 0, 1, -1); break; default: throw new Exception($"Unknown gradient operator: {gradientOperator}"); } // calculate absolute values for gradients CV.ConvertScaleAbs(_gradientX, _absGradientX); CV.ConvertScaleAbs(_gradientY, _absGradientY); // merge gradients // d = 0.5 * abs(dx) + 0.5 * abs(dy) CV.AddWeighted(_absGradientX, 0.5, _absGradientY, 0.5, 0.0, GradientMap); // eliminate gradient weak pixels CV.Threshold(GradientMap, GradientMap, gradientThreshold, 255, ThresholdTypes.ToZero); // edge direction // abs(dx) >= abs(dy) => VERTICAL CV.Cmp(_absGradientX, _absGradientY, DirectionMap, ComparisonOperation.GreaterOrEqual); }
public void DrawEdge( int row, int column, string gradientFileName, string directionFileName, string expectedFileName, string differencesFileName) { // Arrange var gradientMap = LoadImage(gradientFileName); var directionMap = LoadImage(directionFileName); var expectedMap = LoadImage(expectedFileName); var rows = gradientMap.Size.Height; var columns = gradientMap.Size.Width; var edgesMap = new Mat(rows, columns, Depth.U8, 1); edgesMap.Set(Scalar.All(0)); var differencesMap = new Mat(rows, columns, Depth.U8, 1); // Act EdgeDrawing.DrawEdge(row, column, edgesMap, gradientMap, directionMap, rows, columns); // Assert CV.Cmp(edgesMap, expectedMap, differencesMap, ComparisonOperation.NotEqual); SaveImage(differencesMap, differencesFileName); }