View Single Post
  #9   Spotlight this post!  
Unread 25-02-2012, 01:35
questionsigotem questionsigotem is offline
Registered User
FRC #1647
Team Role: Programmer
 
Join Date: Feb 2012
Rookie Year: 2009
Location: New Jersey
Posts: 14
questionsigotem is an unknown quantity at this point
Re: Tracking Multiple Targets

Ok I think I fixed the code. So the code will declare the binary images and such. Then add values for the rectangles. After that it will try to get a color image from the camera and set the binary image to the image that the camera got and look for the color specified. It will then fill in the boxes and remove the small boxes. After that it will filter the particles based on the criteria collection and then order the 4 biggest particles in size (i think smallest to biggest). Then if reports is bigger than 0 it will go through and find the lowest square on the y axis (bottom target). Then it'll find the center of that target. Does that seem about right? If I were to take out the for loop. would it then be able to track all 4 targets because I would then not be restricting it to just look at the bottom half of the image.

Code:
    
//[%Camera]
    public int cameraDirection(boolean ledIsBlue) {
        //printAll();
        output.updateLCD();
        BinaryImage bImg;
        int lowestSquare = 0;
        int lowSquareIndex = 0;
        int cameraDirection = 0; //is it right
	cc = new CriteriaCollection();
	cc.addCriteria(NIVision.MeasurementType.IMAQ_MT_BOUNDING_RECT_WIDTH, 30, 400,false);
	cc.addCriteria(NIVision.MeasurementType.IMAQ_MT_BOUNDING_RECT_HEIGHT,40,400,false);
	
        //System.out.println("It Worked!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        try {
            ColorImage img = camera.getImage();
	    
            //System.out.println("Got IMAGE!!!!");
            if (ledIsBlue) {
                bImg = img.thresholdHSL(126, 155, 0, 255, 150, 255);
                //BLUE
                System.out.println("IS BLUE");
            } else {
                bImg = img.thresholdHSL(85, 128, 0, 255, 150, 255);
                //GREEN
            }
	    
	    NIVision.convexHull(bImg.image, bImg.image, 0);
	    bImg.removeSmallObjects(false, 2);
	   
	    BinaryImage filteredImage = bImg.particleFilter(cc);
	    
            ParticleAnalysisReport[] reports = filteredImage.getOrderedParticleAnalysisReports(4);//Was 1

            if (reports.length > 0) {
                //System.out.println("REPORTS > 0");
                for (int i = 0; i < reports.length; i++) {
                    if (reports[i].imageHeight > minSquareHeight && reports[i].center_mass_y > lowestSquare) {
                        lowestSquare = reports[i].center_mass_y;
                        lowSquareIndex = i;
                        //System.out.println("Target: " + i + "  " + reports[i].center_mass_x + "," + reports[i].center_mass_y);
                    }
                }
            }
	 
            if (reports[lowSquareIndex].center_mass_x < 0.9 * (WIDTH / 2)) {
                //Was reports[lowSquareIndex]. instead of 0
                System.out.println("LEFT");
                centered = 0;
                direction = -1;
            } else if (reports[lowSquareIndex].center_mass_x > 1.1 * (WIDTH / 2)) {
                //.center_mass_y before
                //Was reports[lowSquareIndex]. instead of 0
                System.out.println("RIGHT");
                centered  = 0;
                direction = 1;
            } else if ((reports[lowSquareIndex].center_mass_x > 0.9 * (WIDTH / 2)) && (reports[lowSquareIndex].center_mass_x < 1.1 * (WIDTH / 2))) {
                //Was reports[lowSquareIndex]. instead of 0
                System.out.println("CENTERED");
                centered += 1;
                direction = 0;
            } else {
                System.out.println("NO TARGET");
                centered = 0;
                direction = 0;
            }
	    
            img.free();
            bImg.free();
            //System.out.println("FREED IMAGES");
	    
        } catch (AxisCameraException ex) {
            System.out.println("Axis Camera Exception");
        } catch (NIVisionException ex) {
            System.out.println("NI Vision Exception");
        }
        //printAll();
        output.updateLCD();
	
        return direction;
    }
Reply With Quote