Example #1
0
        // CompareAvi compares each frame of two images to calculate the average
        // difference.  This is used to compare quality.
        private double CompareAvi(AviInputSession original, AviInputSession other)
        {
            double totalDiff = 0;

            using (VisionImage firstImage = new VisionImage(), secondImage = new VisionImage())
            {
                // Setup the images to store the frames.
                firstImage.Type  = ImageType.Rgb32;
                secondImage.Type = ImageType.Rgb32;

                // Calculate the difference between frames.
                for (uint curFrame = 0; curFrame < original.Frames; ++curFrame)
                {
                    // Read the frames.
                    original.ReadFrame(firstImage, curFrame);
                    other.ReadFrame(secondImage, curFrame);

                    // Resize first image to be same as second if needed.
                    if (firstImage.Width != secondImage.Width || firstImage.Height != secondImage.Height)
                    {
                        Algorithms.Resample(firstImage, firstImage, secondImage.Width, secondImage.Height, InterpolationMethod.Bilinear);
                    }

                    // Calculate the difference between the frames.
                    Algorithms.AbsoluteDifference(firstImage, secondImage, firstImage);
                    ColorHistogramReport colorReport = Algorithms.ColorHistogram(firstImage, 256, ColorMode.Hsl);
                    totalDiff += colorReport.Plane3.Mean;
                }
            }
            // Calculate quality from difference
            totalDiff = totalDiff / (double)original.Frames;
            double quality = 1000 - (totalDiff * 10);

            if (quality < 0)
            {
                quality = 0;
            }
            return(quality);
        }
Example #2
0
        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);
        }