/// <summary> /// 显示视觉图像 /// </summary> /// <param name="arg1"></param> /// <param name="arg2"></param> /// <param name="arg3"></param> private void Instance_ImageShowEvent(Module arg1, Camera arg2, VisionImage arg3) { this.BeginInvoke(new Action(() => { if (arg1 == this.Module) { if (arg2 == Camera.Top) { CommonAlgorithms.Copy(arg3, imageControl1.imageSet.Image); } else if (arg2 == Camera.Bottom1) { CommonAlgorithms.Copy(arg3, imageControl2.imageSet.Image); } else if (arg2 == Camera.Bottom2) { CommonAlgorithms.Copy(arg3, imageControl3.imageSet.Image); } } arg3?.Dispose(); })); }
private void bStartChkCailb_Click(object sender, EventArgs e) { if (Thd_Cailb == null || !Thd_Cailb.IsAlive) { Thd_Cailb = new Thread(() => { #region 开始测量 try { Form_Main.Instance.StartGrabImage(); int row = int.Parse(this.tJigRow.Text); // 行 int col = int.Parse(this.tJigCol.Text); // 列 CailbResult = new Jig_ResultItem[row, col]; PointF basePoint = new PointF(); basePoint.X = float.Parse(this.tCailbBaseX.Text); basePoint.Y = float.Parse(this.tCailbBaseY.Text); double distX = float.Parse(this.tJigDx.Text); double distY = float.Parse(this.tJigDy.Text); RectangleContour rect = (RectangleContour)Form_Main.Instance.imageSet.Roi[0].Shape; short rMin = short.Parse(this.upCircleRmin.Text); short rMax = short.Parse(this.upCircleRmax.Text); PointContour center = new PointContour(); double radius = 0; double gain = 1; double offset = 0; double cycle = 1; gain = double.Parse(this.tGainValue.Text); offset = double.Parse(this.tOffsetValue.Text); cycle = double.Parse(this.tHanldeCycle.Text); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { Jig_ResultItem item = new Jig_ResultItem(); item.MachinePos = this.chkPoint[i * col + j]; //item.MachinePos.X = (float)(basePoint.X + distX * j); //item.MachinePos.Y = (float)(basePoint.Y + distY * i); // 移动到目标位 Form_Main.Instance.XYGoPosTillStop(20 * 1000, item.MachinePos, variable); //Thread.Sleep(100); // 获得上相机图像 VisionImage image = Form_Main.Instance.GrabImage2View(Camera.CAM.Top); // 预处理 for (int time = 0; time < cycle; ++time) { image = Form_Main.Instance.GainOffset(image, gain, offset); } Algorithms.Copy(image, Form_Main.Instance.imageSet.Image); // 找圆 if (CPKTools.FindCircle(image, rect, rMin, rMax, ref center, ref radius, 0, 0, 0)) { item.Image_CircleCenter.X = (float)center.X; item.Image_CircleCenter.Y = (float)center.Y; item.Real_CircleCenter = Camera.CameraDefine.Instance[Camera.CAM.Top].ImagePt2WorldPt(item.MachinePos, center, 0); } image.Dispose(); CailbResult[i, j] = item; } } // 计算 间距 for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (CailbResult[i, j].Real_CircleCenter.X == 0 && CailbResult[i, j].Real_CircleCenter.Y == 0) { CailbResult[i, j].RightDist = 0; CailbResult[i, j].DownDist = 0; continue; } if (j != 0 && j < col)// 右间距 { CailbResult[i, j].RightDist = CPKTools.GetDist(ref CailbResult[i, j].Real_CircleCenter, ref CailbResult[i, 0].Real_CircleCenter) - j * distX; } if (i != 0 && i < row)// 下间距 { CailbResult[i, j].DownDist = CPKTools.GetDist(ref CailbResult[i, j].Real_CircleCenter, ref CailbResult[0, j].Real_CircleCenter) - i * distY; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } #endregion }); Thd_Cailb.Start(); } }
public static PaletteType ProcessImage(VisionImage image, string path, out double[] distance) { // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(17, 1); distance = new double[4] { 0, 0, 0, 0 }; // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new AnnulusContour using the given values. PointContour vaCenter = new PointContour(1283, 965); AnnulusContour vaOval = new AnnulusContour(vaCenter, 418, 702, 0, 0); roi.Add(vaOval); // Find Circular Edge EdgeOptions vaOptions = new EdgeOptions(); vaOptions.ColumnProcessingMode = ColumnProcessingMode.Average; vaOptions.InterpolationType = InterpolationMethod.Bilinear; vaOptions.KernelSize = 3; vaOptions.MinimumThreshold = 18; vaOptions.Polarity = EdgePolaritySearchMode.Rising; vaOptions.Width = 3; CircularEdgeFitOptions vaFitOptions = new CircularEdgeFitOptions(); vaFitOptions.ProcessType = RakeProcessType.GetFirstEdges; vaFitOptions.StepSize = 7; vaFitOptions.MaxPixelRadius = 3; vaCircularEdgeReport = IVA_FindCircularEdge(image, roi, SpokeDirection.InsideToOutside, vaOptions, vaFitOptions, ivaData, 1); roi.Dispose(); // Set Coordinate System int vaCoordSystemIndex = 0; int stepIndexOrigin = 1; int resultIndexOrigin = 0; int stepIndexAngle = -1; int resultIndexAngle = 0; double refSysOriginX = vaCircularEdgeReport.Center.X; double refSysOriginY = vaCircularEdgeReport.Center.Y; double refSysAngle = 0; AxisOrientation refSysAxisOrientation = AxisOrientation.Direct; int vaCoordSystemType = 0; IVA_CoordSys(vaCoordSystemIndex, stepIndexOrigin, resultIndexOrigin, stepIndexAngle, resultIndexAngle, refSysOriginX, refSysOriginY, refSysAngle, refSysAxisOrientation, vaCoordSystemType, ivaData); // Image Buffer: Push Functions.IVA_PushBuffer(ivaData, image, 0); // Get Image string vaFilePath = path; FileInformation vaFileInfo = Algorithms.GetFileInformation(vaFilePath); // Set the image size to 0 to speed up the cast. //image.SetSize(0, 0); //image.Type = vaFileInfo.ImageType; //image.BitDepth = 0; image.ReadFile(vaFilePath); switch (image.Type) { case ImageType.I16: case ImageType.U16: if (image.BitDepth == 0 & false) { image.BitDepth = 10; } break; default: break; } // Operators: Absolute Difference Image Algorithms.AbsoluteDifference(image, Functions.IVA_GetBuffer(ivaData, 0), image); // Creates a new, empty region of interest. Roi roi2 = new Roi(); // Creates a new AnnulusContour using the given values. PointContour vaCenter2 = new PointContour(vaCircularEdgeReport.Center.X, vaCircularEdgeReport.Center.Y); AnnulusContour vaOval2 = new AnnulusContour(vaCenter2, 527, 846, 0, 0); roi2.Add(vaOval2); // Reposition the region of interest based on the coordinate system. int coordSystemIndex = 0; Algorithms.TransformRoi(roi2, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex], ivaData.MeasurementSystems[coordSystemIndex])); // Mask from ROI IVA_Mask_From_ROI(image, roi2, false, false); roi2.Dispose(); // Color Threshold Range plane1Range = new Range(0, 60); Range plane2Range = new Range(0, 50); Range plane3Range = new Range(0, 255); using (VisionImage thresholdImage = new VisionImage(ImageType.U8, 7)) { Algorithms.ColorThreshold(image, thresholdImage, ColorMode.Rgb, 1, plane1Range, plane2Range, plane3Range); Algorithms.Copy(thresholdImage, image); } // Truncates the frequencies of an image. IVA_FFT_Truncate(image, TruncateMode.High, 7); // Advanced Morphology: Remove Objects int[] vaCoefficients = { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; StructuringElement vaStructElem = new StructuringElement(3, 3, vaCoefficients); vaStructElem.Shape = StructuringElementShape.Square; // Filters particles based on their size. Algorithms.RemoveParticle(image, image, 30, SizeToKeep.KeepLarge, Connectivity.Connectivity8, vaStructElem); // Invert Binary Image. IVA_BinaryInverse(image); // Advanced Morphology: Remove Objects int[] vaCoefficients2 = { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; StructuringElement vaStructElem2 = new StructuringElement(3, 3, vaCoefficients2); vaStructElem.Shape = StructuringElementShape.Square; // Filters particles based on their size. Algorithms.RemoveParticle(image, image, 5, SizeToKeep.KeepLarge, Connectivity.Connectivity8, vaStructElem2); // Basic Morphology - Applies morphological transformations to binary images. int[] vaCoefficients3 = { 0, 1, 0, 1, 1, 1, 0, 1, 0 }; StructuringElement vaStructElem3 = new StructuringElement(3, 3, vaCoefficients3); vaStructElem.Shape = StructuringElementShape.Square; // Applies morphological transformations for (int i = 0; i < 3; ++i) { Algorithms.Morphology(image, image, MorphologyMethod.Erode, vaStructElem3); } // Advanced Morphology: Fill Holes VisionImage image1 = new VisionImage(); Algorithms.FillHoles(image, image1, Connectivity.Connectivity8); // Particle Analysis - Computes the number of particles detected in a binary image and // returns the requested measurements about the particles. Collection <MeasurementType> vaPixelMeasurements = new Collection <MeasurementType>(new MeasurementType[] { MeasurementType.Area }); Collection <MeasurementType> vaCalibratedMeasurements = new Collection <MeasurementType>(new MeasurementType[] { }); IVA_Particle(image1, Connectivity.Connectivity8, vaPixelMeasurements, vaCalibratedMeasurements, ivaData, 16, out vaParticleReport, out vaParticleReportCalibrated); double[,] area = vaParticleReport.PixelMeasurements; double Maxarea = 0; for (int i = 0; i < area.GetLength(0); i++) { for (int j = 0; j < area.GetLength(1); j++) { if (area[i, j] > Maxarea) { Maxarea = area[i, j]; } } } image1.Dispose(); if (Maxarea > 1000000) { // Creates a new, empty region of interest. Roi roi3 = new Roi(); // Creates a new AnnulusContour using the given values. PointContour vaCenter3 = new PointContour(1295, 963); AnnulusContour vaOval3 = new AnnulusContour(vaCenter3, 496, 892, 0, 0); roi3.Add(vaOval3); // Reposition the region of interest based on the coordinate system. int coordSystemIndex2 = 0; Algorithms.TransformRoi(roi3, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex2], ivaData.MeasurementSystems[coordSystemIndex2])); // Extract the contour edges from the image CurveParameters vaCurveParams = new CurveParameters(ExtractionMode.NormalImage, 1, EdgeFilterSize.ContourTracing, 30, 20, 10, true); double[] vaConstraintMinArray = { }; double[] vaConstraintMaxArray = { }; ConnectionConstraintType[] vaConstraintTypeArray = { }; ExtractContourReport vaExtractReport = IVA_ExtractContour(image, roi3, ExtractContourDirection.AnnulusOuterInner, vaCurveParams, vaConstraintTypeArray, vaConstraintMinArray, vaConstraintMaxArray, ExtractContourSelection.Closest); // Fit a circle to the contour ContourOverlaySettings vaEquationOverlay = new ContourOverlaySettings(true, Rgb32Value.GreenColor, 1, true); ContourOverlaySettings vaPointsOverlay = new ContourOverlaySettings(true, Rgb32Value.RedColor, 1, true); PartialCircle vaCircleReport = Algorithms.ContourFitCircle(image, 100, true); Algorithms.ContourOverlay(image, image, vaPointsOverlay, vaEquationOverlay); ComputeDistanceReport vaDistanceReport = Algorithms.ContourComputeDistances(image, image, 0); MaxDistance = 0; MaxDistanceIndex = 0; for (int i = 0; i < vaDistanceReport.Distances.Count; i++) { if (vaDistanceReport.Distances[i].Distance > MaxDistance) { MaxDistance = vaDistanceReport.Distances[i].Distance; MaxDistanceIndex = i; } } var pos = vaDistanceReport.Distances[MaxDistanceIndex]; distance[0] = MaxDistance; roi3.Dispose(); // Creates a new, empty region of interest. Roi roi4 = new Roi(); // Creates a new AnnulusContour using the given values. PointContour vaCenter4 = new PointContour(1294, 962); AnnulusContour vaOval4 = new AnnulusContour(vaCenter4, 499, 885, 0, 0); roi4.Add(vaOval4); // Reposition the region of interest based on the coordinate system. int coordSystemIndex3 = 0; Algorithms.TransformRoi(roi4, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex3], ivaData.MeasurementSystems[coordSystemIndex3])); // Extract the contour edges from the image CurveParameters vaCurveParams2 = new CurveParameters(ExtractionMode.NormalImage, 1, EdgeFilterSize.ContourTracing, 30, 25, 10, true); double[] vaConstraintMinArray2 = { }; double[] vaConstraintMaxArray2 = { }; ConnectionConstraintType[] vaConstraintTypeArray2 = { }; ExtractContourReport vaExtractReport2 = IVA_ExtractContour(image, roi4, ExtractContourDirection.AnnulusInnerOuter, vaCurveParams2, vaConstraintTypeArray2, vaConstraintMinArray2, vaConstraintMaxArray2, ExtractContourSelection.Closest); // Fit a circle to the contour ContourOverlaySettings vaEquationOverlay2 = new ContourOverlaySettings(true, Rgb32Value.GreenColor, 1, true); ContourOverlaySettings vaPointsOverlay2 = new ContourOverlaySettings(true, Rgb32Value.RedColor, 1, true); PartialCircle vaCircleReport2 = Algorithms.ContourFitCircle(image, 100, true); Algorithms.ContourOverlay(image, image, vaPointsOverlay2, vaEquationOverlay2); ComputeDistanceReport vaDistanceReport2 = Algorithms.ContourComputeDistances(image, image, 0); MaxDistance1 = 0; MaxDistanceIndex1 = 0; for (int i = 0; i < vaDistanceReport2.Distances.Count; i++) { if (vaDistanceReport2.Distances[i].Distance > MaxDistance1) { MaxDistance1 = vaDistanceReport2.Distances[i].Distance; MaxDistanceIndex1 = i; } } var pos1 = vaDistanceReport2.Distances[MaxDistanceIndex1]; distance[1] = MaxDistance1; distance[2] = (vaCircleReport2.Center.X - vaCircularEdgeReport.Center.X) / 96; distance[3] = (vaCircleReport2.Center.Y - vaCircularEdgeReport.Center.Y) / 96; roi4.Dispose(); } else { distance[0] = 9999; distance[1] = 9999; distance[2] = 9999; distance[3] = 9999; } // Dispose the IVA_Data structure. ivaData.Dispose(); if (path == $"{ @"./ImageTemp/temp.jpg"}") { image.Dispose(); } // Return the palette type of the final image. return(PaletteType.Binary); }