I was looking around and I did find the CriteriaCollection and particleFilter.
Here is the code below. Do you think this will be able to track the targets. Do you think that I should rearrange anything to make it work better? Thanks again for your help.
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
}
bImg.removeLargeObjects(false, 2);
bImg.convexHull(false);
BinaryImage filteredImage = bImg.particleFilter(cc);
ParticleAnalysisReport[] reports = bImg.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;
}