Here is updated code. I changed the way I used NIVision methods. I believe I freed all the images that I needed to. I haven't changed it to look at all the particles yet.
Code:
//[%Camera]
public int cameraDirection(boolean ledIsBlue) {
//printAll();
output.updateLCD();
BinaryImage bImgThreshold;
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) {
bImgThreshold = img.thresholdHSL(126, 155, 0, 255, 150, 255);
//BLUE
System.out.println("IS BLUE");
} else {
bImgThreshold = img.thresholdHSL(85, 128, 0, 255, 150, 255);
//GREEN
}
BinaryImage bigObjectsImage = bImgThreshold.removeSmallObjects(false, 2); //Remove small artifacts
BinaryImage convexHullImage = bigObjectsImage.convexHull(false); //Fill in occluded rectangles
BinaryImage filteredImage = convexHullImage.particleFilter(cc); //Find filled in rectangles
ParticleAnalysisReport[] reports = filteredImage.getOrderedParticleAnalysisReports();//Gets a list of results
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)) {
System.out.println("LEFT");
centered = 0;
direction = -1;
} else if (reports[lowSquareIndex].center_mass_x > 1.1 * (WIDTH / 2)) {
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();
bImgThreshold.free();
convexHullImage.free();
bigObjectsImage.free();
filteredImage.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;
}