001    /*----------------------------------------------------------------------------*/
002    /* Copyright (c) FIRST 2008-2012. All Rights Reserved.                        */
003    /* Open Source Software - may be modified and shared by FRC teams. The code   */
004    /* must be accompanied by the FIRST BSD license file in the root directory of */
005    /* the project.                                                               */
006    /*----------------------------------------------------------------------------*/
007    package edu.wpi.first.wpilibj.image;
008    
009    /**
010     * Class to store commonly used information about a particle.
011     * @author dtjones
012     */
013    public class ParticleAnalysisReport {
014    
015        /**
016         * The height of the image in pixels.
017         */
018        public final int imageHeight;
019        /**
020         * The width of the image in pixels.
021         */
022        public final int imageWidth;
023        /** X-coordinate of the point representing the average position of the
024         * total particle mass, assuming every point in the particle has a constant density */
025        public final int center_mass_x;                     // MeasurementType: IMAQ_MT_CENTER_OF_MASS_X
026        /** Y-coordinate of the point representing the average position of the
027         * total particle mass, assuming every point in the particle has a constant density */
028        public final int center_mass_y;                     // MeasurementType: IMAQ_MT_CENTER_OF_MASS_Y
029        /**
030         * Center of mass x value normalized to -1.0 to +1.0 range.
031         */
032        public final double center_mass_x_normalized;
033        /**
034         * Center of mass y value normalized to -1.0 to +1.0 range.
035         */
036        public final double center_mass_y_normalized;
037        /** Area of the particle */
038        public final double particleArea;                           // MeasurementType: IMAQ_MT_AREA
039        /** Bounding Rectangle */
040        public final int boundingRectLeft;                          // left/top/width/height
041        /** Bounding Rectangle */
042        public final int boundingRectTop;
043        /** Bounding Rectangle */
044        public final int boundingRectWidth;
045        /** Bounding Rectangle */
046        public final int boundingRectHeight;
047        /** Percentage of the particle Area covering the Image Area. */
048        public final double particleToImagePercent;         // MeasurementType: IMAQ_MT_AREA_BY_IMAGE_AREA
049        /** Percentage of the particle Area in relation to its Particle and Holes Area */
050        public final double particleQuality;                        // MeasurementType: IMAQ_MT_AREA_BY_PARTICLE_AND_HOLES_AREA
051    
052        ParticleAnalysisReport(BinaryImage image, int index) throws NIVisionException {
053            imageHeight = image.getHeight();
054            imageWidth = image.getWidth();
055            center_mass_x = (int) NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_CENTER_OF_MASS_X);
056            center_mass_y = (int) NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_CENTER_OF_MASS_Y);
057            center_mass_x_normalized = (2.0 * center_mass_x / imageWidth) - 1.0;
058            center_mass_y_normalized = (2.0 * center_mass_y / imageHeight) - 1.0;
059            particleArea = NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_AREA);
060            boundingRectLeft = (int) NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_BOUNDING_RECT_LEFT);
061            boundingRectTop = (int) NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_BOUNDING_RECT_TOP);
062            boundingRectWidth = (int) NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_BOUNDING_RECT_WIDTH);
063            boundingRectHeight = (int) NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_BOUNDING_RECT_HEIGHT);
064            particleToImagePercent = NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_AREA_BY_IMAGE_AREA);
065            particleQuality = NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_AREA_BY_PARTICLE_AND_HOLES_AREA);
066        }
067    
068        static double getParticleToImagePercent(BinaryImage image, int index) throws NIVisionException {
069            return NIVision.MeasureParticle(image.image, index, false, NIVision.MeasurementType.IMAQ_MT_AREA_BY_IMAGE_AREA);
070        }
071    
072        /**
073         * Get string representation of the particle analysis report.
074         * @return A string representation of the particle analysis report.
075         */
076        public String toString() {
077            return "Particle Report: \n" +
078                    "    Image Height    : " + imageHeight + "\n" +
079                    "    Image Width     : " + imageWidth + "\n" +
080                    "    Center of mass  : ( " + center_mass_x + " , " + center_mass_y + " )\n" +
081                    "      normalized    : ( " + center_mass_x_normalized + " , " + center_mass_y_normalized + " )\n" +
082                    "    Area            : " + particleArea + "\n" +
083                    "      percent       : " + particleToImagePercent + "\n" +
084                    "    Bounding Rect   : ( " + boundingRectLeft + " , " + boundingRectTop + " ) " + boundingRectWidth + "*" + boundingRectHeight + "\n" +
085                    "    Quality         : " + particleQuality + "\n";
086        }
087    }