public static void LearnPattern(ImageViewer SourceImage, float fUpper = 0, float fLower = 0) { using (VisionImage plane = new VisionImage(ImageType.U8, 7)) { // Extract the green color plane and copy it to the main image. if (SourceImage.Image.Type == ImageType.Rgb32) { Algorithms.ExtractColorPlanes(SourceImage.Image, NationalInstruments.Vision.ColorMode.Rgb, null, plane, null); Algorithms.Copy(plane, SourceImage.Image); } } // Algorithms.LearnPattern2(SourceImage.Image); OvalContour vaRect2 = new OvalContour(0, 0, 0, 0); Roi roi = new Roi(); roi.Add(vaRect2); // Histogram Grayscale using (VisionImage imageMask = new VisionImage(ImageType.U8, 7)) { RotationAngleRange ra = new RotationAngleRange(fLower, fUpper); PixelValue fillValue = new PixelValue(255); Algorithms.RoiToMask(imageMask, roi, fillValue, SourceImage.Image); Algorithms.LearnPattern2(SourceImage.Image, imageMask, MatchingAlgorithm.MatchGrayValuePyramid, ra); } roi.Dispose(); }
//////////////////////////////////////////////////////////////////////////////// // // Function Name: IVA_Classification_Extract_Particles // // Description : Extracts the region of interests of the bounding rectangles of // all particles. // // Parameters : image - Input image // imageMask - Image mask // rois - Array of ROIs // // Return Value : success // //////////////////////////////////////////////////////////////////////////////// public static Collection <Roi> IVA_Classification_Extract_Particles(VisionImage image, VisionImage imageMask) { Collection <MeasurementType> measurements = new Collection <MeasurementType>(); measurements.Add(MeasurementType.BoundingRectLeft); measurements.Add(MeasurementType.BoundingRectTop); measurements.Add(MeasurementType.BoundingRectRight); measurements.Add(MeasurementType.BoundingRectBottom); // Compute the region of interests around each particle. ParticleMeasurementsReport particleReport = Algorithms.ParticleMeasurements(imageMask, measurements, Connectivity.Connectivity8, ParticleMeasurementsCalibrationMode.Pixel); Collection <Roi> rois = new Collection <Roi>(); for (int i = 0; i < particleReport.PixelMeasurements.GetLength(0); i++) { double top = particleReport.PixelMeasurements[i, 1] + imageMask.MaskOffset.Y - 5; top = (top < 0 ? 0 : top); double left = particleReport.PixelMeasurements[i, 0] + imageMask.MaskOffset.X - 5; left = (left < 0 ? 0 : left); double bottom = particleReport.PixelMeasurements[i, 3] + imageMask.MaskOffset.Y + 5; bottom = (bottom > (image.Height - 1) ? (image.Height - 1) : bottom); double right = particleReport.PixelMeasurements[i, 2] + imageMask.MaskOffset.X + 5; right = (right > (image.Width - 1) ? (image.Width - 1) : right); Roi particleROI = new Roi(); particleROI.Add(new RectangleContour(left, top, right - left + 1, bottom - top + 1)); rois.Add(particleROI); } return(rois); }
public static PaletteType ProcessImage(VisionImage image) { // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(3, 0); // Extract Color Plane using (VisionImage plane = new VisionImage(ImageType.U8, 7)) { // Extract the green color plane and copy it to the main image. Algorithms.ExtractColorPlanes(image, ColorMode.Rgb, null, plane, 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(20, 20, 2552, 1904); roi.Add(vaRect); // Geometric Matching string vaTemplateFile = $"{ @"./VisionModel/Polygon_20.5M/Mode.png"}"; CurveOptions vaCurveOptions = new CurveOptions(); vaCurveOptions.ColumnStepSize = 15; vaCurveOptions.ExtractionMode = ExtractionMode.NormalImage; vaCurveOptions.FilterSize = EdgeFilterSize.Normal; vaCurveOptions.MaximumEndPointGap = 10; vaCurveOptions.MinimumLength = 20; vaCurveOptions.RowStepSize = 15; vaCurveOptions.Threshold = 145; MatchGeometricPatternEdgeBasedOptions matchGPMOptions = new MatchGeometricPatternEdgeBasedOptions(); matchGPMOptions.Advanced.ContrastMode = ContrastMode.Original; matchGPMOptions.Advanced.MatchStrategy = GeometricMatchingSearchStrategy.Balanced; matchGPMOptions.MinimumMatchScore = 800; matchGPMOptions.Mode = GeometricMatchModes.RotationInvariant; matchGPMOptions.NumberOfMatchesRequested = 1; double[] vaRangesMin = { -20, 0, 50, 0 }; double[] vaRangesMax = { 20, 0, 200, 50 }; matchGPMOptions.OcclusionRange = new Range(vaRangesMin[3], vaRangesMax[3]); matchGPMOptions.RotationAngleRanges.Add(new Range(vaRangesMin[0], vaRangesMax[0])); matchGPMOptions.RotationAngleRanges.Add(new Range(vaRangesMin[1], vaRangesMax[1])); matchGPMOptions.ScaleRange = new Range(vaRangesMin[2], vaRangesMax[2]); matchGPMOptions.SubpixelAccuracy = true; gpm2Results = IVA_MatchGeometricPattern2(image, vaTemplateFile, vaCurveOptions, matchGPMOptions, ivaData, 2, roi); roi.Dispose(); // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(PaletteType.Gray); }
public static PaletteType ProcessImage(VisionImage image) { // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(2, 0); // Creates a new, empty region of interest. Roi roi = new Roi(); // 建一條巡邊線Star(229,40) End(229,300) PointContour vaStartPoint = new PointContour(229, 40); PointContour vaEndPoint = new PointContour(229, 300); LineContour vaLine = new LineContour(vaStartPoint, vaEndPoint); roi.Add(vaLine); // 邊緣偵測;採用簡單邊緣偵測方式 SimpleEdgeOptions vaSimpleEdgeOptions = new SimpleEdgeOptions(); vaSimpleEdgeOptions.Process = EdgeProcess.All; vaSimpleEdgeOptions.Type = LevelType.Absolute; vaSimpleEdgeOptions.Threshold = 128; vaSimpleEdgeOptions.Hysteresis = 2; vaSimpleEdgeOptions.SubPixel = true; simpleEdges = IVA_SimpleEdge(image, roi, vaSimpleEdgeOptions, ivaData, 0); roi.Dispose(); // Caliper // Delete all the results of this step (from a previous iteration) Functions.IVA_DisposeStepResults(ivaData, 1); // Computes the vaDistance between two points. Collection <double> vaDistance = IVA_GetDistance(image, ivaData, 1, 0, 3, 0, 5); caliperDistance = vaDistance[0]; // Dispose the IVA_Data structure. ivaData.Dispose(); //列出相關AOI資料 MessageBox.Show("座標點1" + simpleEdges[2].ToString() + "\r\n" + "座標點2" + simpleEdges[0].ToString() + "\r\n" + "座標點3" + simpleEdges[1].ToString() + "\r\n" + "\r\n" + "間距量測" + caliperDistance.ToString()); //繪出檢測直線(巡邊線) //Graphics g = Graphics.FromImage(FileName) //g.DrawLine(0, 0, 100, 100); // Return the palette type of the final image. return(PaletteType.Gray); }
public string ProcessQR(VisionImage image, List <QRConfig> qRConfigs) { string qRInfo = string.Empty; // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(1, 0); for (int i = 0; i < qRConfigs.Count; i++) { // Creates a new, empty region of interest. Roi roi = new Roi(); //// Creates a new RectangleContour using the given values. //RectangleContour vaRect = new RectangleContour(720, 96, 1792, 1240); RectangleContour vaRect = new RectangleContour(qRConfigs[i].Rectangle.Left, qRConfigs[i].Rectangle.Top, qRConfigs[i].Rectangle.Height, qRConfigs[i].Rectangle.Width); roi.Add(vaRect); image.Overlays.Default.AddRoi(roi); // Read QR Code QRDescriptionOptions vaQROptions = new QRDescriptionOptions(); vaQROptions.Dimensions = qRConfigs[i].QRDimension; vaQROptions.MirrorMode = QRMirrorMode.AutoDetect; vaQROptions.ModelType = QRModelType.AutoDetect; vaQROptions.Polarity = qRConfigs[i].Polarity; QRSizeOptions vaQRSizeOptions = new QRSizeOptions(3, 15); QRSearchOptions vaQRSearchOptions = new QRSearchOptions(); vaQRSearchOptions.CellFilterMode = QRCellFilterMode.AutoDetect; vaQRSearchOptions.CellSampleSize = qRConfigs[i].CellSize; vaQRSearchOptions.DemodulationMode = QRDemodulationMode.AutoDetect; vaQRSearchOptions.EdgeThreshold = 30; vaQRSearchOptions.RotationMode = QRRotationMode.Unlimited; vaQRSearchOptions.SkewDegreesAllowed = 10; vaQRSearchOptions.SkipLocation = false; vaQRCode = Algorithms.ReadQRCode(image, roi, vaQROptions, vaQRSizeOptions, vaQRSearchOptions); if (vaQRCode.Found) { image.Overlays.Default.AddPolygon(new PolygonContour(vaQRCode.Corners), Rgb32Value.RedColor, DrawingMode.DrawValue); } System.Text.ASCIIEncoding vaASCIIEncoding = new System.Text.ASCIIEncoding(); vaQRCodeData = vaASCIIEncoding.GetString(vaQRCode.GetData()); qRInfo += string.Format("{0},", vaQRCodeData); roi.Dispose(); } qRInfo = qRInfo.Substring(0, qRInfo.Length - 1); // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(qRInfo); }
public static PaletteType ProcessImage(VisionImage image) { // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(4, 0); // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new AnnulusContour using the given values. PointContour vaCenter = new PointContour(1295, 970); AnnulusContour vaOval = new AnnulusContour(vaCenter, 412, 954, 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 = 50; vaOptions.Polarity = EdgePolaritySearchMode.Rising; vaOptions.Width = 3; CircularEdgeFitOptions vaFitOptions = new CircularEdgeFitOptions(); vaFitOptions.ProcessType = RakeProcessType.GetBestEdges; vaFitOptions.StepSize = 10; vaFitOptions.MaxPixelRadius = 3; vaCircularEdgeReport = IVA_FindCircularEdge(image, roi, SpokeDirection.InsideToOutside, vaOptions, vaFitOptions, ivaData, 1); roi.Dispose(); // Overlays a line onto the image. int[] xCoords = { 1296, 1296 }; int[] yCoords = { 0, 1944 }; byte[] vaColor = { 6, 0, 239 }; IVA_OverlayLine(image, xCoords, yCoords, vaColor); // Overlays a line onto the image. int[] xCoords2 = { 0, 2592 }; int[] yCoords2 = { 972, 972 }; byte[] vaColor2 = { 6, 0, 239 }; IVA_OverlayLine(image, xCoords2, yCoords2, vaColor2); // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(PaletteType.Gray); }
private void testPartsButton_Click(object sender, EventArgs e) { // Save the regions on the viewer. testLines.Clear(); foreach (Contour c in imageViewer1.Roi) { testLines.Add(c.Shape); } // Clear the viewer regions and disable the Test Parts button. imageViewer1.Roi.Clear(); DisableRoiSelection(); testPartsButton.Enabled = false; resetButton.Enabled = true; // Enable the timer. timer1.Enabled = true; timer1_Tick(timer1, EventArgs.Empty); }
public static PaletteType ProcessImage(VisionImage image) { // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(2, 0); // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new LineContour using the given values. PointContour vaStartPoint = new PointContour(229, 42); PointContour vaEndPoint = new PointContour(229, 298); LineContour vaLine = new LineContour(vaStartPoint, vaEndPoint); roi.Add(vaLine); // Edge Detector - Simple Edge SimpleEdgeOptions vaSimpleEdgeOptions = new SimpleEdgeOptions(); vaSimpleEdgeOptions.Process = EdgeProcess.All; vaSimpleEdgeOptions.Type = LevelType.Absolute; vaSimpleEdgeOptions.Threshold = 128; vaSimpleEdgeOptions.Hysteresis = 2; vaSimpleEdgeOptions.SubPixel = true; simpleEdges = IVA_SimpleEdge(image, roi, vaSimpleEdgeOptions, ivaData, 0); roi.Dispose(); // Caliper // Delete all the results of this step (from a previous iteration) Functions.IVA_DisposeStepResults(ivaData, 1); // Computes the vaDistance between two points. Collection <double> vaDistance = IVA_GetDistance(image, ivaData, 1, 0, 3, 0, 5); caliperDistance = vaDistance[0]; // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(PaletteType.Gray); }
public static double FindCircleHistogramMean(VisionImage image, OvalContour vaRect2) { Roi roi = new Roi(); // Creates a new RectangleContour using the given values. roi.Add(vaRect2); // Histogram Grayscale double HistogramMean = -100; using (VisionImage imageMask = new VisionImage(ImageType.U8, 7)) { PixelValue fillValue = new PixelValue(255); Range intervalRange = new Range(0, 0); Algorithms.RoiToMask(imageMask, roi, fillValue, image); // Calculates and returns statistical parameters on the image. HistogramMean = Algorithms.Histogram(image, 256, intervalRange, imageMask).Mean; } roi.Dispose(); return(HistogramMean); }
public static PointContour MatchPattern(VisionImage SourceImage, RectangleContour vaRect, string TemplateFile, int vaNumMatchesRequested, float vaMinMatchScore, float fUpper = 0, float fLower = 0) { PointContour point = new PointContour(); point.X = -10000; point.Y = -10000; // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new RotatedRectangleContour using the given values. PointContour vaCenter = new PointContour(vaRect.Left + vaRect.Width / 2, vaRect.Top + vaRect.Height / 2); RotatedRectangleContour vaRotatedRect = new RotatedRectangleContour(vaCenter, vaRect.Width - 50, vaRect.Height - 50, 0); roi.Add(vaRotatedRect); // MatchPattern Grayscale // string TemplateFile = "D:\\t1.png"; MatchingAlgorithm matchAlgorithm = MatchingAlgorithm.MatchGrayValuePyramid; float[] minAngleVals = { fLower, 0 }; float[] maxAngleVals = { fUpper, 0 }; int[] advancedOptionsItems = { 102, 106, 107, 108, 109, 111, 112, 113, 103, 104, 105, 100 }; double[] advancedOptionsValues = { 10, 300, 0, 6, 1, 20, 10, 20, 1, 20, 0, 5 }; int numberAdvOptions = 12; //int vaNumMatchesRequested = 1; //float vaMinMatchScore = 800; using (VisionImage imageTemplate = new VisionImage(ImageType.U8, 7)) { Collection <PatternMatchReport> patternMatchingResults = new Collection <PatternMatchReport>(); // Read the image template. imageTemplate.ReadVisionFile(TemplateFile); // Set the angle range. Collection <RotationAngleRange> angleRange = new Collection <RotationAngleRange>(); for (int i = 0; i < 2; ++i) { angleRange.Add(new RotationAngleRange(minAngleVals[i], maxAngleVals[i])); } // Set the advanced options. Collection <PMMatchAdvancedSetupDataOption> advancedMatchOptions = new Collection <PMMatchAdvancedSetupDataOption>(); for (int i = 0; i < numberAdvOptions; ++i) { advancedMatchOptions.Add(new PMMatchAdvancedSetupDataOption((MatchSetupOption)advancedOptionsItems[i], advancedOptionsValues[i])); } // Searches for areas in the image that match a given pattern. patternMatchingResults = Algorithms.MatchPattern3(SourceImage, imageTemplate, matchAlgorithm, vaNumMatchesRequested, vaMinMatchScore, angleRange, roi, advancedMatchOptions); string sPatterScore = ""; if (patternMatchingResults.Count > 0) { for (int i = 0; i < 1 /* patternMatchingResults.Count*/; ++i) { point.X = patternMatchingResults[i].Position.X; point.Y = patternMatchingResults[i].Position.Y; sPatterScore += " " + patternMatchingResults[i].Score.ToString(); //SourceImage.Overlays.Default.AddRectangle(new RectangleContour(point.X - imageTemplate.Width / 2 - 1, point.Y - imageTemplate.Height / 2 - 1, imageTemplate.Width, imageTemplate.Height), Rgb32Value.GreenColor); LineContour l1 = new LineContour(); l1.Start.X = point.X - (imageTemplate.Width / 2 - 3); l1.Start.Y = point.Y; l1.End.X = point.X + (imageTemplate.Width / 2 - 3); l1.End.Y = point.Y; SourceImage.Overlays.Default.AddLine(l1, Rgb32Value.RedColor); LineContour l2 = new LineContour(); l2.Start.X = point.X; l2.Start.Y = point.Y - (imageTemplate.Height / 2 - 3); l2.End.X = point.X; l2.End.Y = point.Y + (imageTemplate.Height / 2 - 3); SourceImage.Overlays.Default.AddLine(l2, Rgb32Value.RedColor); } } } roi.Dispose(); return(point); }
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); }
public PaletteType ProcessImage(VisionImage image) { // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(7, 0); // 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); } // Filters: Convolution - Applies a linear filter to an image by convolving the image with a filtering kernel. double[] vaCoefficients = { 1, 2, 4, 2, 1, 2, 4, 8, 4, 2, 4, 8, 16, 8, 4, 2, 4, 8, 4, 2, 1, 2, 4, 2, 1 }; Algorithms.Convolute(image, image, new Kernel(5, 5, vaCoefficients)); // Filters: Convolution - Applies a linear filter to an image by convolving the image with a filtering kernel. double[] vaCoefficients2 = { -1, -1, -1, -1, 10, -1, -1, -1, -1 }; Algorithms.Convolute(image, image, new Kernel(3, 3, vaCoefficients2)); // Automatic Threshold Algorithms.AutoThreshold(image, image, 2, ThresholdMethod.Clustering); // Basic Morphology - Applies morphological transformations to binary images. int[] vaCoefficients3 = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; StructuringElement vaStructElem = new StructuringElement(7, 7, vaCoefficients3); vaStructElem.Shape = StructuringElementShape.Hexagon; // Applies morphological transformations Algorithms.Morphology(image, image, MorphologyMethod.Open, vaStructElem); // Lookup Table: Equalize // Calculates the histogram of the image and redistributes pixel values // accross the desired range to maintain the same pixel value distribution. Range equalizeRange = new Range(0, 255); if (image.Type != ImageType.U8) { equalizeRange.Maximum = 0; } Algorithms.Equalize(image, image, null, equalizeRange, null); // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new RectangleContour using the given values. RectangleContour vaRect = new RectangleContour(0, 0, image.Width, image.Height); roi.Add(vaRect); // Classifies all the objects located in the given ROI. string vaClassifierFilePath = "C:\\DATA\\#hasilscan2\\Ukuran Classifier.clf"; vaClassifierReports = IVA_Classify(image, roi, vaClassifierFilePath); roi.Dispose(); // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(PaletteType.Binary); }
public string ProcessDatamatrix(VisionImage image, TemplateConfig templateConfig, List <DataMatrixConfig> dataMatrixConfigs) { string dataMatrixInfo = string.Empty; // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(3, 1); // Creates a new, empty region of interest. Roi roi = new Roi(); //// Creates a new RotatedRectangleContour using the given values. //RotatedRectangleContour vaRotatedRect = new RotatedRectangleContour(vaCenter, 1268, 1220, 0); // Creates a new RectangleContour using the given values. RectangleContour vaRotatedRect = new RectangleContour(templateConfig.Rectangle.Left, templateConfig.Rectangle.Top, templateConfig.Rectangle.Width, templateConfig.Rectangle.Height); roi.Add(vaRotatedRect); image.Overlays.Default.AddRoi(roi); // MatchPattern Grayscale MatchMode vaMode = MatchMode.RotationInvariant; bool vaSubpixelVal = false; int[] minAngleVals = { -30, 0 }; int[] maxAngleVals = { 30, 0 }; int vaNumMatchesRequested = 1; double vaMinMatchScore = 800; double vaOffsetX = 0; double vaOffsetY = 0; pmResults = IVA_MatchPattern(image, ivaData, templateConfig.TemplatePath, vaMode, vaSubpixelVal, minAngleVals, maxAngleVals, vaNumMatchesRequested, vaMinMatchScore, roi, vaOffsetX, vaOffsetY, 0); foreach (PatternMatch match in pmResults) { image.Overlays.Default.AddPolygon(new PolygonContour(match.Corners), Rgb32Value.RedColor); } roi.Dispose(); // Set Coordinate System int vaCoordSystemIndex = 0; int stepIndexOrigin = 0; int resultIndexOrigin = 1; int stepIndexAngle = 0; int resultIndexAngle = 3; double refSysOriginX = templateConfig.Position.X; double refSysOriginY = templateConfig.Position.Y; double refSysAngle = 0; AxisOrientation refSysAxisOrientation = AxisOrientation.Direct; int vaCoordSystemType = 3; IVA_CoordSys(vaCoordSystemIndex, stepIndexOrigin, resultIndexOrigin, stepIndexAngle, resultIndexAngle, refSysOriginX, refSysOriginY, refSysAngle, refSysAxisOrientation, vaCoordSystemType, ivaData); for (int i = 0; i < dataMatrixConfigs.Count; i++) { // Creates a new, empty region of interest. Roi roiDM = new Roi(); // Creates a new RectangleContour using the given values. RectangleContour vaRect = new RectangleContour(dataMatrixConfigs[i].Rectangle.Left, dataMatrixConfigs[i].Rectangle.Top, dataMatrixConfigs[i].Rectangle.Width, dataMatrixConfigs[i].Rectangle.Height); roiDM.Add(vaRect); // Reposition the region of interest based on the coordinate system. int coordSystemIndex = 0; Algorithms.TransformRoi(roiDM, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex], ivaData.MeasurementSystems[coordSystemIndex])); image.Overlays.Default.AddRoi(roiDM); // Read DataMatrix Barcode DataMatrixDescriptionOptions vaDescriptionOptions = new DataMatrixDescriptionOptions(); vaDescriptionOptions.AspectRatio = 0; vaDescriptionOptions.CellFill = DataMatrixCellFillMode.AutoDetect; uint matrixSizeColumns = 25; uint.TryParse(dataMatrixConfigs[i].MatrixSize.Split('X')[1], out matrixSizeColumns); vaDescriptionOptions.Columns = matrixSizeColumns; vaDescriptionOptions.MinimumBorderIntegrity = 90; vaDescriptionOptions.MirrorMode = DataMatrixMirrorMode.AutoDetect; vaDescriptionOptions.Polarity = dataMatrixConfigs[0].Polarity; vaDescriptionOptions.Rectangle = false; uint matrixSizeRows = 25; uint.TryParse(dataMatrixConfigs[i].MatrixSize.Split('X')[0], out matrixSizeRows); vaDescriptionOptions.Rows = matrixSizeRows; DataMatrixSizeOptions vaSizeOptions = new DataMatrixSizeOptions(); vaSizeOptions.MaximumSize = 250; vaSizeOptions.MinimumSize = 50; vaSizeOptions.QuietZoneWidth = 0; DataMatrixSearchOptions vaSearchOptions = new DataMatrixSearchOptions(); vaSearchOptions.CellFilterMode = DataMatrixCellFilterMode.AutoDetect; vaSearchOptions.CellSampleSize = dataMatrixConfigs[0].CellSize; vaSearchOptions.DemodulationMode = DataMatrixDemodulationMode.AutoDetect; vaSearchOptions.EdgeThreshold = 30; vaSearchOptions.InitialSearchVectorWidth = 5; vaSearchOptions.MaximumIterations = 150; vaSearchOptions.RotationMode = DataMatrixRotationMode.Unlimited; vaSearchOptions.SkewDegreesAllowed = 5; vaSearchOptions.SkipLocation = false; // Reads the data matrix from the image. vaDataMatrixReport = Algorithms.ReadDataMatrixBarcode(image, roiDM, DataMatrixGradingMode.None, vaDescriptionOptions, vaSizeOptions, vaSearchOptions); if (vaDataMatrixReport.Found) { image.Overlays.Default.AddPolygon(new PolygonContour(vaDataMatrixReport.Corners), Rgb32Value.RedColor, DrawingMode.DrawValue); } dataMatrixInfo += string.Format("{0},", vaDataMatrixReport.StringData); roiDM.Dispose(); } dataMatrixInfo = dataMatrixInfo.Substring(0, dataMatrixInfo.Length - 1); // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(dataMatrixInfo); }
/// <summary> /// Process QR code with coordinate /// </summary> /// <param name="image"></param> /// <param name="userProgram"></param> /// <returns>split with ','</returns> public string ProcessQRCoordinate(VisionImage image, UserProgram userProgram) { string qRInfo = string.Empty; TemplateConfig templateConfig = userProgram.TemplateConfig; List <QRConfig> qRConfigs = userProgram.QRConfigs; // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(3, 1); // Creates a new, empty region of interest. Roi roiFullRange = new Roi(); // Creates a new RotatedRectangleContour using the given values. PointContour vaCenter = new PointContour(1405.5, 954); RotatedRectangleContour vaRotatedRect = new RotatedRectangleContour(vaCenter, 1661, 1184, 0); RectangleContour rectangle = new RectangleContour(templateConfig.Rectangle.Left, templateConfig.Rectangle.Top, templateConfig.Rectangle.Width, templateConfig.Rectangle.Height); roiFullRange.Add(rectangle); // MatchPattern Grayscale string vaTemplateFile = templateConfig.TemplatePath; MatchMode vaMode = MatchMode.RotationInvariant; bool vaSubpixelVal = false; int[] minAngleVals = { -20, 0 }; int[] maxAngleVals = { 20, 0 }; int vaNumMatchesRequested = 1; double vaMinMatchScore = 800; double vaOffsetX = 0; double vaOffsetY = 0; pmResults = IVA_MatchPattern(image, ivaData, vaTemplateFile, vaMode, vaSubpixelVal, minAngleVals, maxAngleVals, vaNumMatchesRequested, vaMinMatchScore, roiFullRange, vaOffsetX, vaOffsetY, 0); if (pmResults.Count < 1) { return(string.Empty); } foreach (PatternMatch match in pmResults) { image.Overlays.Default.AddPolygon(new PolygonContour(match.Corners), Rgb32Value.RedColor); } roiFullRange.Dispose(); // Set Coordinate System int vaCoordSystemIndex = 0; int stepIndexOrigin = 0; int resultIndexOrigin = 1; int stepIndexAngle = 0; int resultIndexAngle = 3; double refSysOriginX = templateConfig.Position.X; double refSysOriginY = templateConfig.Position.Y; double refSysAngle = 0; AxisOrientation refSysAxisOrientation = AxisOrientation.Direct; int vaCoordSystemType = 3; IVA_CoordSys(vaCoordSystemIndex, stepIndexOrigin, resultIndexOrigin, stepIndexAngle, resultIndexAngle, refSysOriginX, refSysOriginY, refSysAngle, refSysAxisOrientation, vaCoordSystemType, ivaData); for (int i = 0; i < qRConfigs.Count; i++) { // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new RectangleContour using the given values. RectangleContour vaRect = new RectangleContour(qRConfigs[i].Rectangle.Left, qRConfigs[i].Rectangle.Top, qRConfigs[i].Rectangle.Width, qRConfigs[i].Rectangle.Height); roi.Add(vaRect); // Reposition the region of interest based on the coordinate system. int coordSystemIndex = 0; Algorithms.TransformRoi(roi, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex], ivaData.MeasurementSystems[coordSystemIndex])); // Read QR Code QRDescriptionOptions vaQROptions = new QRDescriptionOptions(); vaQROptions.Dimensions = qRConfigs[i].QRDimension; vaQROptions.MirrorMode = QRMirrorMode.AutoDetect; vaQROptions.ModelType = QRModelType.AutoDetect; vaQROptions.Polarity = qRConfigs[i].Polarity; QRSizeOptions vaQRSizeOptions = new QRSizeOptions(3, 15); QRSearchOptions vaQRSearchOptions = new QRSearchOptions(); vaQRSearchOptions.CellFilterMode = QRCellFilterMode.AutoDetect; vaQRSearchOptions.CellSampleSize = qRConfigs[i].CellSize; vaQRSearchOptions.DemodulationMode = QRDemodulationMode.AutoDetect; vaQRSearchOptions.EdgeThreshold = 30; vaQRSearchOptions.RotationMode = QRRotationMode.Unlimited; vaQRSearchOptions.SkewDegreesAllowed = 5; vaQRSearchOptions.SkipLocation = false; vaQRCode = Algorithms.ReadQRCode(image, roi, vaQROptions, vaQRSizeOptions, vaQRSearchOptions); if (vaQRCode.Found) { image.Overlays.Default.AddPolygon(new PolygonContour(vaQRCode.Corners), Rgb32Value.RedColor, DrawingMode.DrawValue); } System.Text.ASCIIEncoding vaASCIIEncoding = new System.Text.ASCIIEncoding(); vaQRCodeData = vaASCIIEncoding.GetString(vaQRCode.GetData()); qRInfo += string.Format("{0},", vaQRCodeData); roi.Dispose(); } if (!string.IsNullOrEmpty(qRInfo)) { qRInfo = qRInfo.Substring(0, qRInfo.Length - 1); } // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(qRInfo); }
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); }
public PaletteType ProcessImage(VisionImage image) { //vaParticleReportCalibrated = new ParticleMeasurementsReport(); // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(15, 0); // Image Buffer: Push Functions.IVA_PushBuffer(ivaData, image, 0); // Operators: NOR Image Algorithms.Nor(image, Functions.IVA_GetBuffer(ivaData, 0), image); // 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); } // Thresholds an image into 2 classes by using local thresholding. LocalThresholdOptions vaLocalThresholdOptions = new LocalThresholdOptions(); vaLocalThresholdOptions.DeviationWeight = 1; vaLocalThresholdOptions.Method = LocalThresholdMethod.BackgroundCorrection; vaLocalThresholdOptions.ParticleType = ParticleType.Dark; vaLocalThresholdOptions.ReplaceValue = 1; vaLocalThresholdOptions.WindowHeight = 35; vaLocalThresholdOptions.WindowWidth = 35; Algorithms.LocalThreshold(image, image, vaLocalThresholdOptions); // Basic Morphology - Applies morphological transformations to binary images. int[] vaCoefficients = { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; StructuringElement vaStructElem = new StructuringElement(3, 3, vaCoefficients); vaStructElem.Shape = StructuringElementShape.Square; // Applies morphological transformations Algorithms.Morphology(image, image, MorphologyMethod.GradientOut, vaStructElem); // Advanced Morphology: Fill Holes Algorithms.FillHoles(image, image, Connectivity.Connectivity8); // 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.Hexagon; // Filters particles based on their size. Algorithms.RemoveParticle(image, image, 6, SizeToKeep.KeepLarge, Connectivity.Connectivity8, vaStructElem2); // Advanced Morphology: Remove Border Objects - Eliminates particles touching the border of the image. Algorithms.RejectBorder(image, image, Connectivity.Connectivity8); // Lookup Table: Equalize // Calculates the histogram of the image and redistributes pixel values // accross the desired range to maintain the same pixel value distribution. Range equalizeRange = new Range(0, 255); if (image.Type != ImageType.U8) { equalizeRange.Maximum = 0; } Algorithms.Equalize(image, image, null, equalizeRange, null); // Image Buffer: Push Functions.IVA_PushBuffer(ivaData, image, 1); // 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.BoundingRectLeft, MeasurementType.BoundingRectTop, MeasurementType.BoundingRectRight, MeasurementType.BoundingRectBottom, MeasurementType.MaxFeretDiameter }); Collection <MeasurementType> vaCalibratedMeasurements = new Collection <MeasurementType>(new MeasurementType[] {}); //IVA_Particle(image, Connectivity.Connectivity4, null, null, null, 10, out vaParticleReport, out vaParticleReportCalibrated); IVA_Particle(image, Connectivity.Connectivity4, vaPixelMeasurements, vaCalibratedMeasurements, ivaData, 10, out vaParticleReport, out vaParticleReportCalibrated); // Image Buffer: Pop Algorithms.Copy(Functions.IVA_GetBuffer(ivaData, 1), image); // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new RectangleContour using the given values. RectangleContour vaRect = new RectangleContour(0, 0, image.Width, image.Height); roi.Add(vaRect); // Classifies all the objects located in the given ROI. string vaClassifierFilePath = "C:\\DATA\\#hasilscan3\\Particle Classifier.clf"; vaClassifierReports = IVA_Classify(image, roi, vaClassifierFilePath); VisGrainTypeCollection colType = new VisGrainTypeCollection(vaClassifierReports); roi.Dispose(); // Image Buffer: Pop Algorithms.Copy(Functions.IVA_GetBuffer(ivaData, 1), image); // Creates a new, empty region of interest. Roi roi2 = new Roi(); // Creates a new RectangleContour using the given values. RectangleContour vaRect2 = new RectangleContour(0, 0, image.Width, image.Height); roi2.Add(vaRect2); // Classifies all the objects located in the given ROI. string vaClassifierFilePath2 = "C:\\DATA\\#hasilscan3\\Ukuran Classifier.clf"; vaClassifierReports2 = IVA_Classify(image, roi2, vaClassifierFilePath2); VisGrainSizeCollection colSize = new VisGrainSizeCollection(vaClassifierReports2); GrainResults = new VisGrainDataCollection(vaClassifierReports, vaClassifierReports2, ListShape); roi2.Dispose(); // Dispose the IVA_Data structure. ivaData.Dispose(); // Return the palette type of the final image. return(PaletteType.Binary); }
public string Process1DBarcode(VisionImage image, TemplateConfig templateConfig, List <BarcodeConfig> barcodeConfigs) { // Initialize the IVA_Data structure to pass results and coordinate systems. IVA_Data ivaData = new IVA_Data(5, 1); // Creates a new, empty region of interest. Roi roi = new Roi(); // Creates a new RotatedRectangleContour using the given values. //PointContour vaCenter = new PointContour(1294, 972); //RotatedRectangleContour vaRotatedRect = new RotatedRectangleContour(vaCenter, 2548, 1904, 0); // Creates a new RectangleContour using the given values. RectangleContour vaRotatedRect = new RectangleContour(templateConfig.Rectangle.Left, templateConfig.Rectangle.Top, templateConfig.Rectangle.Width, templateConfig.Rectangle.Height); roi.Add(vaRotatedRect); image.Overlays.Default.AddRoi(roi); // MatchPattern Grayscale MatchMode vaMode = MatchMode.RotationInvariant; bool vaSubpixelVal = false; int[] minAngleVals = { -30, 0 }; int[] maxAngleVals = { 30, 0 }; int vaNumMatchesRequested = 1; double vaMinMatchScore = 800; double vaOffsetX = 0; double vaOffsetY = 0; pmResults = IVA_MatchPattern(image, ivaData, templateConfig.TemplatePath, vaMode, vaSubpixelVal, minAngleVals, maxAngleVals, vaNumMatchesRequested, vaMinMatchScore, roi, vaOffsetX, vaOffsetY, 0); foreach (PatternMatch match in pmResults) { image.Overlays.Default.AddPolygon(new PolygonContour(match.Corners), Rgb32Value.RedColor); } roi.Dispose(); // Set Coordinate System int vaCoordSystemIndex = 0; int stepIndexOrigin = 0; int resultIndexOrigin = 1; int stepIndexAngle = 0; int resultIndexAngle = 3; double refSysOriginX = templateConfig.Position.X; double refSysOriginY = templateConfig.Position.Y; double refSysAngle = 0; AxisOrientation refSysAxisOrientation = AxisOrientation.Direct; int vaCoordSystemType = 3; IVA_CoordSys(vaCoordSystemIndex, stepIndexOrigin, resultIndexOrigin, stepIndexAngle, resultIndexAngle, refSysOriginX, refSysOriginY, refSysAngle, refSysAxisOrientation, vaCoordSystemType, ivaData); string barcodeInfo = ""; for (int i = 0; i < barcodeConfigs.Count; i++) { Roi roiBarcode = new Roi(); RectangleContour vaRect = new RectangleContour(barcodeConfigs[i].Rectangle.Left, barcodeConfigs[i].Rectangle.Top, barcodeConfigs[i].Rectangle.Width, barcodeConfigs[i].Rectangle.Height); roiBarcode.Add(vaRect); // Reposition the region of interest based on the coordinate system. int coordSystemIndex = 0; Algorithms.TransformRoi(roiBarcode, new CoordinateTransform(ivaData.baseCoordinateSystems[coordSystemIndex], ivaData.MeasurementSystems[coordSystemIndex])); image.Overlays.Default.AddRoi(roiBarcode); // Reads the barcode from the image. BarcodeReport vaBarcode = Algorithms.ReadBarcode(image, barcodeConfigs[i].Type, roiBarcode, false); barcodeInfo += string.Format("{0},", vaBarcode.Text); roiBarcode.Dispose(); } barcodeInfo = barcodeInfo.Substring(0, barcodeInfo.Length - 1); // Dispose the IVA_Data structure. ivaData.Dispose(); return(barcodeInfo); }