private void FindStraightEdges() { // Use search direction selected. SearchDirection direction = (SearchDirection)Enum.Parse(typeof(SearchDirection), (string)searchDirection.SelectedItem); // Fill in the edge options structure from the controls on the form. EdgeOptions edgeOptions = new EdgeOptions(); edgeOptions.ColumnProcessingMode = (ColumnProcessingMode)Enum.Parse(typeof(ColumnProcessingMode), (string)smoothing.SelectedItem); edgeOptions.InterpolationType = (InterpolationMethod)Enum.Parse(typeof(InterpolationMethod), (string)interpolationMethod.SelectedItem); edgeOptions.KernelSize = (uint)kernelSize.Value; edgeOptions.MinimumThreshold = (uint)minimumThreshold.Value; edgeOptions.Polarity = (EdgePolaritySearchMode)Enum.Parse(typeof(EdgePolaritySearchMode), (string)polarity.SelectedItem); edgeOptions.Width = (uint)width.Value; // Fill in the straight edge options structure from the controls on the form. StraightEdgeOptions straightEdgeOptions = new StraightEdgeOptions(); straightEdgeOptions.AngleRange = (double)angleRange.Value; straightEdgeOptions.AngleTolerance = (double)angleTolerance.Value; straightEdgeOptions.HoughIterations = (uint)houghIterations.Value; straightEdgeOptions.ScoreRange.Initialize((double)minimumScore.Value, (double)maximumScore.Value); straightEdgeOptions.MinimumCoverage = (double)minimumCoverage.Value; straightEdgeOptions.MinimumSignalToNoiseRatio = (double)minimumSignalToNoiseRatio.Value; straightEdgeOptions.NumberOfLines = (uint)numberOfLines.Value; straightEdgeOptions.Orientation = (double)orientation.Value; straightEdgeOptions.SearchMode = (StraightEdgeSearchMode)Enum.Parse(typeof(StraightEdgeSearchMode), (string)searchMode.SelectedItem); straightEdgeOptions.StepSize = (uint)stepSize.Value; // Run the edge detection. StraightEdgeReport report = Algorithms.StraightEdge(imageViewer1.Image, imageViewer1.Roi, direction, edgeOptions, straightEdgeOptions); // Clear all overlays from previous run. imageViewer1.Image.Overlays.Default.Clear(); // Overlay the straight edges. foreach (StraightEdgeReportItem item in report.StraightEdges) { imageViewer1.Image.Overlays.Default.AddLine(item.StraightEdge, Rgb32Value.RedColor); } // Overlay the search lines. foreach (SearchLineInfo info in report.SearchLines) { // First the line itself. imageViewer1.Image.Overlays.Default.AddLine(info.Line, Rgb32Value.BlueColor); // Then the found edges on each line. foreach (EdgeInfo edgeInfo in info.EdgeReport.Edges) { imageViewer1.Image.Overlays.Default.AddOval(new OvalContour(edgeInfo.Position.X - 1, edgeInfo.Position.Y - 1, 3, 3), Rgb32Value.YellowColor, DrawingMode.PaintValue); } } }
private static FindEdgeReport IVA_FindEdge(VisionImage image, Roi roi, RakeDirection direction, EdgeOptions options, StraightEdgeOptions straightEdgeOptions, IVA_Data ivaData, int stepIndex) { // First, delete all the results of this step (from a previous iteration) Functions.IVA_DisposeStepResults(ivaData, stepIndex); // Find the Edge FindEdgeOptions edgeOptions = new FindEdgeOptions(direction); edgeOptions.EdgeOptions = options; edgeOptions.StraightEdgeOptions = straightEdgeOptions; FindEdgeReport lineReport = new FindEdgeReport(); lineReport = Algorithms.FindEdge(image, roi, edgeOptions); // If there was at least one line, get data if (lineReport.StraightEdges.Count >= 1) { // Store the results in the data structure. ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Point 1.X Position (Pix.)", lineReport.StraightEdges[0].StraightEdge.Start.X)); ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Point 1.Y Position (Pix.)", lineReport.StraightEdges[0].StraightEdge.Start.Y)); ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Point 2.X Position (Pix.)", lineReport.StraightEdges[0].StraightEdge.End.X)); ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Point 2.Y Position (Pix.)", lineReport.StraightEdges[0].StraightEdge.End.Y)); if ((image.InfoTypes & InfoTypes.Calibration) != 0) { ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Point 1.X Position (World)", lineReport.StraightEdges[0].CalibratedStraightEdge.Start.X)); ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Point 1.Y Position (World)", lineReport.StraightEdges[0].CalibratedStraightEdge.Start.Y)); ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Point 2.X Position (World)", lineReport.StraightEdges[0].CalibratedStraightEdge.End.X)); ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Point 2.Y Position (World)", lineReport.StraightEdges[0].CalibratedStraightEdge.End.Y)); } ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Angle", lineReport.StraightEdges[0].Angle)); if ((image.InfoTypes & InfoTypes.Calibration) != 0) { ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Angle (World)", lineReport.StraightEdges[0].CalibratedAngle)); } } return(lineReport); }
private void FindEdges() { if (imageViewer1.Roi.Count > 0) { // Use search direction selected. RakeDirection direction = (RakeDirection)Enum.Parse(typeof(RakeDirection), (string)searchDirection.SelectedItem); // Fill in the edge options structure from the controls on the form. EdgeOptions edgeOptions = new EdgeOptions(); edgeOptions.ColumnProcessingMode = (ColumnProcessingMode)Enum.Parse(typeof(ColumnProcessingMode), (string)smoothing.SelectedItem); edgeOptions.InterpolationType = (InterpolationMethod)Enum.Parse(typeof(InterpolationMethod), (string)interpolationMethod.SelectedItem); edgeOptions.KernelSize = (uint)kernelSize.Value; edgeOptions.MinimumThreshold = (uint)minimumThreshold.Value; edgeOptions.Polarity = (EdgePolaritySearchMode)Enum.Parse(typeof(EdgePolaritySearchMode), (string)polarity.SelectedItem); edgeOptions.Width = (uint)width.Value; // Fill in the straight edge options structure from the controls on the form. StraightEdgeOptions straightEdgeOptions = new StraightEdgeOptions(); straightEdgeOptions.AngleRange = (double)angleRange.Value; straightEdgeOptions.AngleTolerance = (double)angleTolerance.Value; straightEdgeOptions.HoughIterations = (uint)houghIterations.Value; straightEdgeOptions.ScoreRange.Initialize((double)minimumScore.Value, (double)maximumScore.Value); straightEdgeOptions.MinimumCoverage = (double)minimumCoverage.Value; straightEdgeOptions.MinimumSignalToNoiseRatio = (double)minimumSignalToNoiseRatio.Value; straightEdgeOptions.NumberOfLines = (uint)numberOfLines.Value; straightEdgeOptions.Orientation = (double)orientation.Value; straightEdgeOptions.SearchMode = (StraightEdgeSearchMode)Enum.Parse(typeof(StraightEdgeSearchMode), (string)searchMode.SelectedItem); straightEdgeOptions.StepSize = (uint)stepSize.Value; FindEdgeOptions options = new FindEdgeOptions(direction, true, true, true, true); options.EdgeOptions = edgeOptions; options.StraightEdgeOptions = straightEdgeOptions; // Clear all overlays from previous run. imageViewer1.Image.Overlays.Default.Clear(); // Run the edge detection. FindEdgeReport report = Algorithms.FindEdge(imageViewer1.Image, imageViewer1.Roi, options); } }
public static short FitLine(VisionImage image, RectangleContour roi, double EdgeStength, int searchDir, int edgeMode, ref PointContour PointStart, ref PointContour PointEnd)//视觉抓边算法 { SearchDirection SearchDirection = SearchDirection.BottomToTop; EdgePolaritySearchMode EdgeMode = EdgePolaritySearchMode.Falling; if (searchDir == 0) { SearchDirection = SearchDirection.LeftToRight; } else if (searchDir == 1) { SearchDirection = SearchDirection.RightToLeft; } else if (searchDir == 2) { SearchDirection = SearchDirection.TopToBottom; } else if (searchDir == 3) { SearchDirection = SearchDirection.BottomToTop; } if (edgeMode == 0) { EdgeMode = EdgePolaritySearchMode.Rising; } else if (edgeMode == 1) { EdgeMode = EdgePolaritySearchMode.Falling; } if (roi != null) { VisionImage imageTemp = new VisionImage(); Algorithms.Extract(image, imageTemp, roi); Algorithms.ImageToImage(imageTemp, image, new PointContour(roi.Left, roi.Top)); #region 抓边参数 StraightEdgeOptions vaStraightEdgeOptions = new StraightEdgeOptions(); vaStraightEdgeOptions.NumberOfLines = 1; vaStraightEdgeOptions.SearchMode = StraightEdgeSearchMode.FirstRakeEdges; vaStraightEdgeOptions.ScoreRange = new Range(0, 1024); vaStraightEdgeOptions.Orientation = 0; vaStraightEdgeOptions.AngleRange = 90; vaStraightEdgeOptions.AngleTolerance = 1; vaStraightEdgeOptions.StepSize = 3; vaStraightEdgeOptions.MinimumSignalToNoiseRatio = 0; vaStraightEdgeOptions.MinimumCoverage = 25; vaStraightEdgeOptions.HoughIterations = 5; // Set EdgeOptions EdgeOptions vaEdgeOptions = new EdgeOptions(); vaEdgeOptions.Polarity = EdgeMode; vaEdgeOptions.KernelSize = 7; vaEdgeOptions.Width = 3; vaEdgeOptions.MinimumThreshold = EdgeStength; vaEdgeOptions.InterpolationType = InterpolationMethod.ZeroOrder; vaEdgeOptions.ColumnProcessingMode = ColumnProcessingMode.Average; #endregion try { StraightEdgeReport report1 = Algorithms.StraightEdge2(image, roi.ConvertToRoi(), SearchDirection, vaEdgeOptions, vaStraightEdgeOptions, false); if (report1.StraightEdges.Count > 0) { PointStart = report1.StraightEdges[0].StraightEdge.Start; PointEnd = report1.StraightEdges[0].StraightEdge.End; //image.Overlays.Default.Clear(); image.Overlays.Default.AddLine(new LineContour(report1.StraightEdges[0].StraightEdge.Start, report1.StraightEdges[0].StraightEdge.End)); return(0); } } catch { } } PointStart.X = 0; PointStart.Y = 0; PointEnd.X = 0; PointEnd.Y = 0; return(1); }
public static PaletteType RectLeftPos(VisionImage image, Point pointoffset) { // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(12, 1); // Extract Color Plane using (VisionImage plane = new VisionImage(ImageType.U8, 7)) { // Extract the red color plane and copy it to the main image. Algorithms.ExtractColorPlanes(image, ColorMode.Rgb, plane, null, null); Algorithms.Copy(plane, image); } // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new RectangleContour using the given values. RectangleContour vaRect = new RectangleContour(630, 1313, 1073, 416); roi.Add(vaRect); // MatchPattern Grayscale string dicpath = System.Windows.Forms.Application.StartupPath; string vaTemplateFile = dicpath + $"{ @"/ImageConfig/LeftRectPos.png"}"; MatchingAlgorithm matchAlgorithm = MatchingAlgorithm.MatchGrayValuePyramid; float[] minAngleVals = { -10, 0 }; float[] maxAngleVals = { 10, 0 }; int[] advancedOptionsItems = { 100, 102, 106, 107, 108, 109, 114, 116, 117, 118, 111, 112, 113, 103, 104, 105 }; double[] advancedOptionsValues = { 5, 10, 300, 0, 6, 1, 25, 0, 0, 0, 20, 10, 20, 1, 20, 0 }; int numberAdvOptions = 16; int vaNumMatchesRequested = 1; float vaMinMatchScore = 700; pmResults = IVA_MatchPattern(image, ivaData, vaTemplateFile, matchAlgorithm, minAngleVals, maxAngleVals, advancedOptionsItems, advancedOptionsValues, numberAdvOptions, vaNumMatchesRequested, vaMinMatchScore, roi, 2); roi.Dispose(); if (pmResults.Count == 1) { // Set Coordinate System int vaCoordSystemIndex = 0; int stepIndexOrigin = 2; int resultIndexOrigin = 1; int stepIndexAngle = 2; int resultIndexAngle = 3; double refSysOriginX = 1160.5; double refSysOriginY = 1500.5; double refSysAngle = 0; AxisOrientation refSysAxisOrientation = AxisOrientation.Direct; int vaCoordSystemType = 3; IVA_CoordSys(vaCoordSystemIndex, stepIndexOrigin, resultIndexOrigin, stepIndexAngle, resultIndexAngle, refSysOriginX, refSysOriginY, refSysAngle, refSysAxisOrientation, vaCoordSystemType, ivaData); // Creates a new, empty region of interest. Roi roi2 = new Roi(); // Creates a new RotatedRectangleContour using the given values. PointContour vaCenter = new PointContour(789, 965.5); RotatedRectangleContour vaRotatedRect = new RotatedRectangleContour(vaCenter, 72, 1119, 0); roi2.Add(vaRotatedRect); // Reposition the region of interest based on the coordinate system. int coordSystemIndex = 0; Algorithms.TransformRoi(roi2, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex], ivaData.MeasurementSystems[coordSystemIndex])); // Find Straight Edge EdgeOptions vaOptions = new EdgeOptions(); vaOptions.ColumnProcessingMode = ColumnProcessingMode.Average; vaOptions.InterpolationType = InterpolationMethod.Bilinear; vaOptions.KernelSize = 9; vaOptions.MinimumThreshold = Position.Instance.EdgeThreshold_Left; vaOptions.Polarity = EdgePolaritySearchMode.Falling; vaOptions.Width = 5; StraightEdgeOptions vaStraightEdgeOptions = new StraightEdgeOptions(); vaStraightEdgeOptions.AngleRange = 45; vaStraightEdgeOptions.AngleTolerance = 1; vaStraightEdgeOptions.HoughIterations = 5; vaStraightEdgeOptions.MinimumCoverage = 25; vaStraightEdgeOptions.MinimumSignalToNoiseRatio = 0; vaStraightEdgeOptions.NumberOfLines = 1; vaStraightEdgeOptions.Orientation = 0; Range vaRange = new Range(0, 1000); vaStraightEdgeOptions.ScoreRange = vaRange; vaStraightEdgeOptions.StepSize = 20; vaStraightEdgeOptions.SearchMode = StraightEdgeSearchMode.FirstRakeEdges; vaEdgeReport = IVA_FindEdge(image, roi2, RakeDirection.LeftToRight, vaOptions, vaStraightEdgeOptions, ivaData, 4); roi2.Dispose(); // Creates a new, empty region of interest. Roi roi3 = new Roi(); // Creates a new RotatedRectangleContour using the given values. PointContour vaCenter2 = new PointContour(1162.5, 263); RotatedRectangleContour vaRotatedRect2 = new RotatedRectangleContour(vaCenter2, 595, 78, 0); roi3.Add(vaRotatedRect2); // Reposition the region of interest based on the coordinate system. int coordSystemIndex2 = 0; Algorithms.TransformRoi(roi3, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex2], ivaData.MeasurementSystems[coordSystemIndex2])); // Find Straight Edge EdgeOptions vaOptions2 = new EdgeOptions(); vaOptions2.ColumnProcessingMode = ColumnProcessingMode.Average; vaOptions2.InterpolationType = InterpolationMethod.Bilinear; vaOptions2.KernelSize = 9; vaOptions2.MinimumThreshold = Position.Instance.EdgeThreshold_Left; vaOptions2.Polarity = EdgePolaritySearchMode.Falling; vaOptions2.Width = 9; StraightEdgeOptions vaStraightEdgeOptions2 = new StraightEdgeOptions(); vaStraightEdgeOptions2.AngleRange = 45; vaStraightEdgeOptions2.AngleTolerance = 1; vaStraightEdgeOptions2.HoughIterations = 5; vaStraightEdgeOptions2.MinimumCoverage = 25; vaStraightEdgeOptions2.MinimumSignalToNoiseRatio = 0; vaStraightEdgeOptions2.NumberOfLines = 1; vaStraightEdgeOptions2.Orientation = 0; Range vaRange2 = new Range(0, 1000); vaStraightEdgeOptions2.ScoreRange = vaRange2; vaStraightEdgeOptions2.StepSize = 20; vaStraightEdgeOptions2.SearchMode = StraightEdgeSearchMode.FirstRakeEdges; vaEdgeReport2 = IVA_FindEdge(image, roi3, RakeDirection.TopToBottom, vaOptions2, vaStraightEdgeOptions2, ivaData, 5); roi3.Dispose(); // Creates a new, empty region of interest. Roi roi4 = new Roi(); // Creates a new RotatedRectangleContour using the given values. PointContour vaCenter3 = new PointContour(1530, 968.5); RotatedRectangleContour vaRotatedRect3 = new RotatedRectangleContour(vaCenter3, 78, 1137, 0); roi4.Add(vaRotatedRect3); // Reposition the region of interest based on the coordinate system. int coordSystemIndex3 = 0; Algorithms.TransformRoi(roi4, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex3], ivaData.MeasurementSystems[coordSystemIndex3])); // Find Straight Edge EdgeOptions vaOptions3 = new EdgeOptions(); vaOptions3.ColumnProcessingMode = ColumnProcessingMode.Average; vaOptions3.InterpolationType = InterpolationMethod.Bilinear; vaOptions3.KernelSize = 9; vaOptions3.MinimumThreshold = Position.Instance.EdgeThreshold_Left; vaOptions3.Polarity = EdgePolaritySearchMode.Falling; vaOptions3.Width = 9; StraightEdgeOptions vaStraightEdgeOptions3 = new StraightEdgeOptions(); vaStraightEdgeOptions3.AngleRange = 45; vaStraightEdgeOptions3.AngleTolerance = 1; vaStraightEdgeOptions3.HoughIterations = 5; vaStraightEdgeOptions3.MinimumCoverage = 25; vaStraightEdgeOptions3.MinimumSignalToNoiseRatio = 0; vaStraightEdgeOptions3.NumberOfLines = 1; vaStraightEdgeOptions3.Orientation = 0; Range vaRange3 = new Range(0, 1000); vaStraightEdgeOptions3.ScoreRange = vaRange3; vaStraightEdgeOptions3.StepSize = 20; vaStraightEdgeOptions3.SearchMode = StraightEdgeSearchMode.FirstRakeEdges; vaEdgeReport3 = IVA_FindEdge(image, roi4, RakeDirection.RightToLeft, vaOptions3, vaStraightEdgeOptions3, ivaData, 6); roi4.Dispose(); // Creates a new, empty region of interest. Roi roi5 = new Roi(); // Creates a new RotatedRectangleContour using the given values. PointContour vaCenter4 = new PointContour(1171.5, 1691.5); RotatedRectangleContour vaRotatedRect4 = new RotatedRectangleContour(vaCenter4, 543, 75, 0); roi5.Add(vaRotatedRect4); // Reposition the region of interest based on the coordinate system. int coordSystemIndex4 = 0; Algorithms.TransformRoi(roi5, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex4], ivaData.MeasurementSystems[coordSystemIndex4])); // Find Straight Edge EdgeOptions vaOptions4 = new EdgeOptions(); vaOptions4.ColumnProcessingMode = ColumnProcessingMode.Average; vaOptions4.InterpolationType = InterpolationMethod.Bilinear; vaOptions4.KernelSize = 11; vaOptions4.MinimumThreshold = Position.Instance.EdgeThreshold_Left; vaOptions4.Polarity = EdgePolaritySearchMode.Falling; vaOptions4.Width = 9; StraightEdgeOptions vaStraightEdgeOptions4 = new StraightEdgeOptions(); vaStraightEdgeOptions4.AngleRange = 45; vaStraightEdgeOptions4.AngleTolerance = 1; vaStraightEdgeOptions4.HoughIterations = 5; vaStraightEdgeOptions4.MinimumCoverage = 25; vaStraightEdgeOptions4.MinimumSignalToNoiseRatio = 0; vaStraightEdgeOptions4.NumberOfLines = 1; vaStraightEdgeOptions4.Orientation = 0; Range vaRange4 = new Range(0, 1000); vaStraightEdgeOptions4.ScoreRange = vaRange4; vaStraightEdgeOptions4.StepSize = 20; vaStraightEdgeOptions4.SearchMode = StraightEdgeSearchMode.FirstRakeEdges; vaEdgeReport4 = IVA_FindEdge(image, roi5, RakeDirection.BottomToTop, vaOptions4, vaStraightEdgeOptions4, ivaData, 7); roi5.Dispose(); // Caliper // Delete all the results of this step (from a previous iteration) Functions.IVA_DisposeStepResults(ivaData, 8); // Computes the vaIntersection point between two lines. Collection <PointContour> vaIntersection = IVA_GetIntersection(image, ivaData, 8, 5, 0, 5, 2, 6, 0, 6, 2); caliperIntersection = vaIntersection[0]; // Caliper // Delete all the results of this step (from a previous iteration) Functions.IVA_DisposeStepResults(ivaData, 9); // Computes the vaIntersection point between two lines. Collection <PointContour> vaIntersection2 = IVA_GetIntersection(image, ivaData, 9, 6, 0, 6, 2, 7, 0, 7, 2); caliperIntersection2 = vaIntersection2[0]; // Caliper // Delete all the results of this step (from a previous iteration) Functions.IVA_DisposeStepResults(ivaData, 10); // Computes the vaIntersection point between two lines. Collection <PointContour> vaIntersection3 = IVA_GetIntersection(image, ivaData, 10, 4, 0, 4, 2, 7, 0, 7, 2); caliperIntersection3 = vaIntersection3[0]; // Caliper // Delete all the results of this step (from a previous iteration) Functions.IVA_DisposeStepResults(ivaData, 11); // Computes the vaIntersection point between two lines. Collection <PointContour> vaIntersection4 = IVA_GetIntersection(image, ivaData, 11, 4, 0, 4, 2, 5, 0, 5, 2); caliperIntersection4 = vaIntersection4[0]; //计算每个角的偏差 string str1 = Math.Round((-caliperIntersection.X - pointoffset.X + Position.Instance.SpecLeftPos_X[0]) / 96, 3).ToString() + ";" + Math.Round((-caliperIntersection.Y - pointoffset.Y + Position.Instance.SpecLeftPos_Y[0]) / 96, 3).ToString() + ";"; string str2 = Math.Round((-caliperIntersection2.X - pointoffset.X + Position.Instance.SpecLeftPos_X[1]) / 96, 3).ToString() + ";" + Math.Round((-caliperIntersection2.Y - pointoffset.Y + Position.Instance.SpecLeftPos_Y[1]) / 96, 3).ToString() + ";"; string str3 = Math.Round((-caliperIntersection3.X - pointoffset.X + Position.Instance.SpecLeftPos_X[2]) / 96, 3).ToString() + ";" + Math.Round((-caliperIntersection3.Y - pointoffset.Y + Position.Instance.SpecLeftPos_Y[2]) / 96, 3).ToString() + ";"; string str4 = Math.Round((-caliperIntersection4.X - pointoffset.X + Position.Instance.SpecLeftPos_X[3]) / 96, 3).ToString() + ";" + Math.Round((-caliperIntersection4.Y - pointoffset.Y + Position.Instance.SpecLeftPos_Y[3]) / 96, 3).ToString(); LeftCali = str1 + str2 + str3 + str4; LeftCaliArrary = new string[] { str1, str2, str3, str4 }; } else { LeftCali = "0;0;0;0;0;0;0;0"; LeftCaliArrary = new string[] { "0;0", "0;0", "0;0", "0;0" }; } // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(PaletteType.Gray); }