////////////////////////////////////////////////////////////////////////////////
        //
        // 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);
        }
Ejemplo n.º 2
0
        private static void IVA_Particle(VisionImage image,
                                         Connectivity connectivity,
                                         Collection <MeasurementType> pPixelMeasurements,
                                         Collection <MeasurementType> pCalibratedMeasurements,
                                         IVA_Data ivaData,
                                         int stepIndex,
                                         out ParticleMeasurementsReport partReport,
                                         out ParticleMeasurementsReport partReportCal)
        {
            // Computes the requested pixel measurements.
            if (pPixelMeasurements.Count != 0)
            {
                partReport = Algorithms.ParticleMeasurements(image, pPixelMeasurements, connectivity, ParticleMeasurementsCalibrationMode.Pixel);
            }
            else
            {
                partReport = new ParticleMeasurementsReport();
            }

            // Computes the requested calibrated measurements.
            if (pCalibratedMeasurements.Count != 0)
            {
                partReportCal = Algorithms.ParticleMeasurements(image, pCalibratedMeasurements, connectivity, ParticleMeasurementsCalibrationMode.Calibrated);
            }
            else
            {
                partReportCal = new ParticleMeasurementsReport();
            }

            // Computes the center of mass of each particle to log as results.
            ParticleMeasurementsReport   centerOfMass;
            Collection <MeasurementType> centerOfMassMeasurements = new Collection <MeasurementType>();

            centerOfMassMeasurements.Add(MeasurementType.CenterOfMassX);
            centerOfMassMeasurements.Add(MeasurementType.CenterOfMassY);

            if ((image.InfoTypes & InfoTypes.Calibration) != 0)
            {
                centerOfMass = Algorithms.ParticleMeasurements(image, centerOfMassMeasurements, connectivity, ParticleMeasurementsCalibrationMode.Both);
            }
            else
            {
                centerOfMass = Algorithms.ParticleMeasurements(image, centerOfMassMeasurements, connectivity, ParticleMeasurementsCalibrationMode.Pixel);
            }

            // Delete all the results of this step (from a previous iteration)
            Functions.IVA_DisposeStepResults(ivaData, stepIndex);

            ivaData.stepResults[stepIndex].results.Add(new IVA_Result("Object #", centerOfMass.PixelMeasurements.GetLength(0)));

            if (centerOfMass.PixelMeasurements.GetLength(0) > 0)
            {
                for (int i = 0; i < centerOfMass.PixelMeasurements.GetLength(0); ++i)
                {
                    ivaData.stepResults[stepIndex].results.Add(new IVA_Result(String.Format("Particle {0}.X Position (Pix.)", i + 1), centerOfMass.PixelMeasurements[i, 0]));
                    ivaData.stepResults[stepIndex].results.Add(new IVA_Result(String.Format("Particle {0}.Y Position (Pix.)", i + 1), centerOfMass.PixelMeasurements[i, 1]));

                    // If the image is calibrated, also store the real world coordinates.
                    if ((image.InfoTypes & InfoTypes.Calibration) != 0)
                    {
                        ivaData.stepResults[stepIndex].results.Add(new IVA_Result(String.Format("Particle {0}.X Position (Calibrated)", i + 1), centerOfMass.CalibratedMeasurements[i, 0]));
                        ivaData.stepResults[stepIndex].results.Add(new IVA_Result(String.Format("Particle {0}.Y Position (Calibrated)", i + 1), centerOfMass.CalibratedMeasurements[i, 1]));
                    }
                }
            }
        }