Bennett
06-02-2012, 18:56
With this code we get an image from the camera but we have an issue with trying to get the camera to track and print out the results, any ideas? thanks.
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.DriverStationLCD;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.camera.AxisCamera;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.image.*;
import edu.wpi.first.wpilibj.templates.commands.CommandBa se;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class RobotTemplate extends IterativeRobot {
AxisCamera camera;
BinaryImage sensorimage;
CriteriaCollection cc;
public void robotInit() {
String camip = "10.29.77.11";
camera = AxisCamera.getInstance(camip);
camera.writeResolution(AxisCamera.ResolutionT.k640 x480);
camera.writeRotation(AxisCamera.RotationT.k180);
cc = new CriteriaCollection(); // create the criteria for the particle filter
cc.addCriteria(NIVision.MeasurementType.IMAQ_MT_BO UNDING_RECT_WIDTH, 30, 400, false);
cc.addCriteria(NIVision.MeasurementType.IMAQ_MT_BO UNDING_RECT_HEIGHT, 40, 400, false);
while (true) {
try {
ColorImage image; // next 2 lines read image from flash on cRIO
image = new RGBImage("/10ft2.jpg");
BinaryImage thresholdImage = image.thresholdRGB(0, 0, 0, 0, 0, 0); // keep only black objects
BinaryImage bigObjectsImage = thresholdImage.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(); // get list of results
for (int i = 0; i < reports.length; i++) { // print results
ParticleAnalysisReport r = reports[i];
System.out.println("Particle: " + i + ": Center of mass x: " + r.center_mass_x);
DriverStationLCD.getInstance().println(DriverStati onLCD.Line.kMain6, 1, "Particle: " + i + ": Center of mass x: " + r.center_mass_x);
DriverStationLCD.getInstance().updateLCD();
}
System.out.println(filteredImage.getNumberParticle s() + " " + Timer.getFPGATimestamp());
DriverStationLCD.getInstance().println(DriverStati onLCD.Line.kMain6, 1, filteredImage.getNumberParticles() + " " + Timer.getFPGATimestamp());
DriverStationLCD.getInstance().updateLCD();
/**
* all images in Java must be freed after they are used since they are allocated out
* of C data structures. Not calling free() will cause the memory to accumulate over
* each pass of this loop.
*/
filteredImage.free();
convexHullImage.free();
bigObjectsImage.free();
thresholdImage.free();
image.free();
} catch (NIVisionException ex) {
}
}
// freeing the image. A.K.A. letting the image go so we can have more images
//sensorimage = camera.getImage();
CommandBase.init();
}
public void autonomousInit() {
// schedule the autonomous command (example)
//autonomousCommand.start();
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
Scheduler.getInstance().run();
}
public void teleopInit() {
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
//autonomousCommand.cancel();
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
Scheduler.getInstance().run();
}
}
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.DriverStationLCD;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.camera.AxisCamera;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.image.*;
import edu.wpi.first.wpilibj.templates.commands.CommandBa se;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class RobotTemplate extends IterativeRobot {
AxisCamera camera;
BinaryImage sensorimage;
CriteriaCollection cc;
public void robotInit() {
String camip = "10.29.77.11";
camera = AxisCamera.getInstance(camip);
camera.writeResolution(AxisCamera.ResolutionT.k640 x480);
camera.writeRotation(AxisCamera.RotationT.k180);
cc = new CriteriaCollection(); // create the criteria for the particle filter
cc.addCriteria(NIVision.MeasurementType.IMAQ_MT_BO UNDING_RECT_WIDTH, 30, 400, false);
cc.addCriteria(NIVision.MeasurementType.IMAQ_MT_BO UNDING_RECT_HEIGHT, 40, 400, false);
while (true) {
try {
ColorImage image; // next 2 lines read image from flash on cRIO
image = new RGBImage("/10ft2.jpg");
BinaryImage thresholdImage = image.thresholdRGB(0, 0, 0, 0, 0, 0); // keep only black objects
BinaryImage bigObjectsImage = thresholdImage.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(); // get list of results
for (int i = 0; i < reports.length; i++) { // print results
ParticleAnalysisReport r = reports[i];
System.out.println("Particle: " + i + ": Center of mass x: " + r.center_mass_x);
DriverStationLCD.getInstance().println(DriverStati onLCD.Line.kMain6, 1, "Particle: " + i + ": Center of mass x: " + r.center_mass_x);
DriverStationLCD.getInstance().updateLCD();
}
System.out.println(filteredImage.getNumberParticle s() + " " + Timer.getFPGATimestamp());
DriverStationLCD.getInstance().println(DriverStati onLCD.Line.kMain6, 1, filteredImage.getNumberParticles() + " " + Timer.getFPGATimestamp());
DriverStationLCD.getInstance().updateLCD();
/**
* all images in Java must be freed after they are used since they are allocated out
* of C data structures. Not calling free() will cause the memory to accumulate over
* each pass of this loop.
*/
filteredImage.free();
convexHullImage.free();
bigObjectsImage.free();
thresholdImage.free();
image.free();
} catch (NIVisionException ex) {
}
}
// freeing the image. A.K.A. letting the image go so we can have more images
//sensorimage = camera.getImage();
CommandBase.init();
}
public void autonomousInit() {
// schedule the autonomous command (example)
//autonomousCommand.start();
}
/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
Scheduler.getInstance().run();
}
public void teleopInit() {
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
//autonomousCommand.cancel();
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
Scheduler.getInstance().run();
}
}