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/*----------------------------------------------------------------------------*/
007package edu.wpi.first.wpilibj.image;
008
009import com.ni.vision.NIVision;
010
011/**
012 * Class to store commonly used information about a particle.
013 * @author dtjones
014 */
015public class ParticleAnalysisReport {
016
017    /**
018     * The height of the image in pixels.
019     */
020    public final int imageHeight;
021    /**
022     * The width of the image in pixels.
023     */
024    public final int imageWidth;
025    /** X-coordinate of the point representing the average position of the
026     * total particle mass, assuming every point in the particle has a constant density */
027    public final int center_mass_x;                     // MeasurementType: IMAQ_MT_CENTER_OF_MASS_X
028    /** Y-coordinate of the point representing the average position of the
029     * total particle mass, assuming every point in the particle has a constant density */
030    public final int center_mass_y;                     // MeasurementType: IMAQ_MT_CENTER_OF_MASS_Y
031    /**
032     * Center of mass x value normalized to -1.0 to +1.0 range.
033     */
034    public final double center_mass_x_normalized;
035    /**
036     * Center of mass y value normalized to -1.0 to +1.0 range.
037     */
038    public final double center_mass_y_normalized;
039    /** Area of the particle */
040    public final double particleArea;                           // MeasurementType: IMAQ_MT_AREA
041    /** Bounding Rectangle */
042    public final int boundingRectLeft;                          // left/top/width/height
043    /** Bounding Rectangle */
044    public final int boundingRectTop;
045    /** Bounding Rectangle */
046    public final int boundingRectWidth;
047    /** Bounding Rectangle */
048    public final int boundingRectHeight;
049    /** Percentage of the particle Area covering the Image Area. */
050    public final double particleToImagePercent;         // MeasurementType: IMAQ_MT_AREA_BY_IMAGE_AREA
051    /** Percentage of the particle Area in relation to its Particle and Holes Area */
052    public final double particleQuality;                        // MeasurementType: IMAQ_MT_AREA_BY_PARTICLE_AND_HOLES_AREA
053
054    ParticleAnalysisReport(BinaryImage image, int index) throws NIVisionException {
055        imageHeight = image.getHeight();
056        imageWidth = image.getWidth();
057        center_mass_x = (int) NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_CENTER_OF_MASS_X);
058        center_mass_y = (int) NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_CENTER_OF_MASS_Y);
059        center_mass_x_normalized = (2.0 * center_mass_x / imageWidth) - 1.0;
060        center_mass_y_normalized = (2.0 * center_mass_y / imageHeight) - 1.0;
061        particleArea = NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_AREA);
062        boundingRectLeft = (int) NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_BOUNDING_RECT_LEFT);
063        boundingRectTop = (int) NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_BOUNDING_RECT_TOP);
064        boundingRectWidth = (int) NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_BOUNDING_RECT_WIDTH);
065        boundingRectHeight = (int) NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_BOUNDING_RECT_HEIGHT);
066        particleToImagePercent = NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_AREA_BY_IMAGE_AREA);
067        particleQuality = NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_AREA_BY_PARTICLE_AND_HOLES_AREA);
068    }
069
070    static double getParticleToImagePercent(BinaryImage image, int index) throws NIVisionException {
071        return NIVision.imaqMeasureParticle(image.image, index, 0, NIVision.MeasurementType.MT_AREA_BY_IMAGE_AREA);
072    }
073
074    /**
075     * Get string representation of the particle analysis report.
076     * @return A string representation of the particle analysis report.
077     */
078    public String toString() {
079        return "Particle Report: \n" +
080               "    Image Height    : " + imageHeight + "\n" +
081               "    Image Width     : " + imageWidth + "\n" +
082               "    Center of mass  : ( " + center_mass_x + " , " + center_mass_y + " )\n" +
083               "      normalized    : ( " + center_mass_x_normalized + " , " + center_mass_y_normalized + " )\n" +
084               "    Area            : " + particleArea + "\n" +
085               "      percent       : " + particleToImagePercent + "\n" +
086               "    Bounding Rect   : ( " + boundingRectLeft + " , " + boundingRectTop + " ) " + boundingRectWidth + "*" + boundingRectHeight + "\n" +
087               "    Quality         : " + particleQuality + "\n";
088    }
089}