<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.camera.AxisCamera;
import edu.wpi.first.wpilibj.image.BinaryImage;
import edu.wpi.first.wpilibj.image.ColorImage;
import edu.wpi.first.wpilibj.image.ParticleAnalysisReport;
import edu.wpi.first.wpilibj.image.CriteriaCollection;
import edu.wpi.first.wpilibj.image.NIVision.MeasurementType;

/**
 *
 * @author Rajath
 * find targets
 */
public class ImageProcessing {

    ParticleAnalysisReport particles[];
    Physics imageCalculations;
    CriteriaCollection criteriaCollection = new CriteriaCollection();
    ParticleAnalysisReport bottomTarget, topTarget, middleTarget;
    int numberOfDegreesInVerticalFieldOfView = 33;
    final int numberOfPixelsVerticalInFieldOfView = 480;
    final int numberOfPixelsHorizontalInFieldOfView = 640;
    final int heightToTheTopOfTheTopTarget = 118;
    final int heightToBottomOfTopTarget = 100;
    int PixelsFromLevelToBottomOfTopTarget = 0;
    int PixelsFromLevelToTopOfTopTarget = 0;
    boolean isLookingAtTopTarget = false;
    int cameraHeight = 49;

    public ImageProcessing() {
        criteriaCollection.addCriteria(
                MeasurementType.IMAQ_MT_BOUNDING_RECT_WIDTH, 30, 400, false);
        criteriaCollection.addCriteria(
                MeasurementType.IMAQ_MT_BOUNDING_RECT_HEIGHT, 40, 400, false);
        imageCalculations = new Physics(true);
    }

    public void getPixelsFromLevelToBottomOfTopTarget(
            ParticleAnalysisReport particle) {
        PixelsFromLevelToBottomOfTopTarget =
                numberOfPixelsVerticalInFieldOfView - particle.center_mass_y
                - (particle.boundingRectHeight / 2);
    }

    public void getPixelsFromLevelToTopOfTopTarget(
            ParticleAnalysisReport particle) {
        PixelsFromLevelToTopOfTopTarget =
                numberOfPixelsVerticalInFieldOfView - particle.center_mass_y
                - (particle.boundingRectHeight / 2);
    }

    public double getPhi(int PixelsFromLevelToTopOfTopTarget) {
        double phi =
                (PixelsFromLevelToTopOfTopTarget /
                    numberOfPixelsVerticalInFieldOfView)
                * numberOfDegreesInVerticalFieldOfView;
        return phi;
    }

    public double getTheta(int PixelsFromLevelToBottomOfTopTarget) {
        double theta =
                (PixelsFromLevelToBottomOfTopTarget /
                    numberOfPixelsVerticalInFieldOfView)
                * numberOfDegreesInVerticalFieldOfView;

        return theta;
    }

    public double getHypotneuse1(double angle) {
        double opposite1 = heightToTheTopOfTheTopTarget - cameraHeight;
        double hypotneuse_1 =
                opposite1
                / MathX.sin(getPhi(PixelsFromLevelToTopOfTopTarget));
        return hypotneuse_1;
    }

    public double getHypotneuse0(double angle) {

        double opposite0 = heightToBottomOfTopTarget - cameraHeight;
        double hypotneuse_0 =
                opposite0
                / MathX.sin(getTheta(PixelsFromLevelToBottomOfTopTarget));
        return hypotneuse_0;
    }

    public boolean isLookingAtTopTarget() {
        double adjacent1 =
                MathX.cos(getPhi(PixelsFromLevelToTopOfTopTarget))
                * getHypotneuse1(getPhi(PixelsFromLevelToTopOfTopTarget));
        double adjacent0 =
                MathX.cos(getTheta(PixelsFromLevelToBottomOfTopTarget))
                * getHypotneuse0(getTheta(PixelsFromLevelToBottomOfTopTarget));
        System.out.println("Adjacent0 : " + adjacent0);
        System.out.println("Adjacent1 : " + adjacent1);
        if (adjacent0 == adjacent1) {
            return true;
        } else {
            return false;
        }
    }

    public void idTopTarget(ParticleAnalysisReport[] particles) {
        for (int i = 0; i < particles.length; i++) {
            ParticleAnalysisReport particle = particles[i];

            getPixelsFromLevelToTopOfTopTarget(particle);
            getPixelsFromLevelToBottomOfTopTarget(particle);

            isLookingAtTopTarget = isLookingAtTopTarget();
        }
    }

    public void getTheParticles(AxisCamera cam) throws Exception {
         //get image from the camera
        ColorImage colorImg = cam.getImage();
        //seperate the light and dark image
        BinaryImage binImg = colorImg.thresholdRGB(0, 42, 71, 255, 0, 255);
        colorImg.free();
        //remove the small objects
        BinaryImage clnImg = binImg.removeSmallObjects(false, 2);
        //fill the rectangles that were created
        BinaryImage convexHullImg =
                clnImg.convexHull(false);
        BinaryImage filteredImg =
                convexHullImg.particleFilter(criteriaCollection);
        particles = filteredImg.getOrderedParticleAnalysisReports();
        organizeParticles(particles, getTotalXCenter(particles),
                getTotalYCenter(particles));
        binImg.free();
        clnImg.free();
        convexHullImg.free();
        filteredImg.free();
    }

    public static ParticleAnalysisReport getTopMost(
            ParticleAnalysisReport[] particles) {
        ParticleAnalysisReport particle = particles[0];
        for (int i = 0; i < particles.length; i++) {
            if (particle.center_mass_y < particles[i].center_mass_y) {
                particle = particles[i];
            }
        }
        return particle;
    }

    public int getTotalXCenter(ParticleAnalysisReport[] particles) {
        int averageHeight = 0;

        if (particles.length == 0) {
            averageHeight = -1;
        } else {
            for (int i = 0; i < particles.length; i++) {
                averageHeight += particles[i].center_mass_x;
            }<commit_msg>separate the logic from the implementation details in getTheParticles<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.camera.AxisCamera;
import edu.wpi.first.wpilibj.image.BinaryImage;
import edu.wpi.first.wpilibj.image.ColorImage;
import edu.wpi.first.wpilibj.image.ParticleAnalysisReport;
import edu.wpi.first.wpilibj.image.CriteriaCollection;
import edu.wpi.first.wpilibj.image.NIVision.MeasurementType;

/**
 *
 * @author Rajath
 * find targets
 */
public class ImageProcessing {

    ParticleAnalysisReport particles[];
    Physics imageCalculations;
    CriteriaCollection criteriaCollection = new CriteriaCollection();
    ParticleAnalysisReport bottomTarget, topTarget, middleTarget;
    int numberOfDegreesInVerticalFieldOfView = 33;
    final int numberOfPixelsVerticalInFieldOfView = 480;
    final int numberOfPixelsHorizontalInFieldOfView = 640;
    final int heightToTheTopOfTheTopTarget = 118;
    final int heightToBottomOfTopTarget = 100;
    int PixelsFromLevelToBottomOfTopTarget = 0;
    int PixelsFromLevelToTopOfTopTarget = 0;
    boolean isLookingAtTopTarget = false;
    int cameraHeight = 49;

    public ImageProcessing() {
        criteriaCollection.addCriteria(
                MeasurementType.IMAQ_MT_BOUNDING_RECT_WIDTH, 30, 400, false);
        criteriaCollection.addCriteria(
                MeasurementType.IMAQ_MT_BOUNDING_RECT_HEIGHT, 40, 400, false);
        imageCalculations = new Physics(true);
    }

    public void getPixelsFromLevelToBottomOfTopTarget(
            ParticleAnalysisReport particle) {
        PixelsFromLevelToBottomOfTopTarget =
                numberOfPixelsVerticalInFieldOfView - particle.center_mass_y
                - (particle.boundingRectHeight / 2);
    }

    public void getPixelsFromLevelToTopOfTopTarget(
            ParticleAnalysisReport particle) {
        PixelsFromLevelToTopOfTopTarget =
                numberOfPixelsVerticalInFieldOfView - particle.center_mass_y
                - (particle.boundingRectHeight / 2);
    }

    public double getPhi(int PixelsFromLevelToTopOfTopTarget) {
        double phi =
                (PixelsFromLevelToTopOfTopTarget /
                    numberOfPixelsVerticalInFieldOfView)
                * numberOfDegreesInVerticalFieldOfView;
        return phi;
    }

    public double getTheta(int PixelsFromLevelToBottomOfTopTarget) {
        double theta =
                (PixelsFromLevelToBottomOfTopTarget /
                    numberOfPixelsVerticalInFieldOfView)
                * numberOfDegreesInVerticalFieldOfView;

        return theta;
    }

    public double getHypotneuse1(double angle) {
        double opposite1 = heightToTheTopOfTheTopTarget - cameraHeight;
        double hypotneuse_1 =
                opposite1
                / MathX.sin(getPhi(PixelsFromLevelToTopOfTopTarget));
        return hypotneuse_1;
    }

    public double getHypotneuse0(double angle) {

        double opposite0 = heightToBottomOfTopTarget - cameraHeight;
        double hypotneuse_0 =
                opposite0
                / MathX.sin(getTheta(PixelsFromLevelToBottomOfTopTarget));
        return hypotneuse_0;
    }

    public boolean isLookingAtTopTarget() {
        double adjacent1 =
                MathX.cos(getPhi(PixelsFromLevelToTopOfTopTarget))
                * getHypotneuse1(getPhi(PixelsFromLevelToTopOfTopTarget));
        double adjacent0 =
                MathX.cos(getTheta(PixelsFromLevelToBottomOfTopTarget))
                * getHypotneuse0(getTheta(PixelsFromLevelToBottomOfTopTarget));
        System.out.println("Adjacent0 : " + adjacent0);
        System.out.println("Adjacent1 : " + adjacent1);
        if (adjacent0 == adjacent1) {
            return true;
        } else {
            return false;
        }
    }

    public void idTopTarget(ParticleAnalysisReport[] particles) {
        for (int i = 0; i < particles.length; i++) {
            ParticleAnalysisReport particle = particles[i];

            getPixelsFromLevelToTopOfTopTarget(particle);
            getPixelsFromLevelToBottomOfTopTarget(particle);

            isLookingAtTopTarget = isLookingAtTopTarget();
        }
    }

    public void getTheParticles(AxisCamera camera) throws Exception {
        int erosionCount = 2;
        // true means use connectivity 8, true means connectivity 4
        boolean connectivity8Or4 = false;
        ColorImage colorImage;
        BinaryImage binaryImage;
        BinaryImage cleanImage;
        BinaryImage convexHullImage;
        BinaryImage filteredImage;

        colorImage = camera.getImage();
        //seperate the light and dark image
        binaryImage = colorImage.thresholdRGB(0, 42, 71, 255, 0, 255);
        cleanImage = binaryImage.removeSmallObjects(
                connectivity8Or4, erosionCount);
        //fill the rectangles that were created
        convexHullImage = cleanImage.convexHull(connectivity8Or4);
        filteredImage = convexHullImage.particleFilter(criteriaCollection);
        particles = filteredImage.getOrderedParticleAnalysisReports();
        organizeParticles(particles, getTotalXCenter(particles),
                getTotalYCenter(particles));

        colorImage.free();
        binaryImage.free();
        cleanImage.free();
        convexHullImage.free();
        filteredImage.free();
    }

    public static ParticleAnalysisReport getTopMost(
            ParticleAnalysisReport[] particles) {
        ParticleAnalysisReport particle = particles[0];
        for (int i = 0; i < particles.length; i++) {
            if (particle.center_mass_y < particles[i].center_mass_y) {
                particle = particles[i];
            }
        }
        return particle;
    }

    public int getTotalXCenter(ParticleAnalysisReport[] particles) {
        int averageHeight = 0;

        if (particles.length == 0) {
            averageHeight = -1;
        } else {
            for (int i = 0; i < particles.length; i++) {
                averageHeight += particles[i].center_mass_x;
            }
{'fraction_non_alphanumeric': 0.07271815446339017, 'fraction_numerical': 0.01237044466733534, 'mean_word_length': 2.58985898589859, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6015734', 'n_tokens_mistral': 3480, 'n_tokens_neox': 3232, 'n_words': 636}