use of boofcv.struct.calib.CameraPinholeRadial in project BoofCV by lessthanoptimal.
the class BatchRemoveLensDistortion method main.
public static void main(String[] args) {
String inputPath, regex, pathIntrinsic, outputDir;
AdjustmentType adjustmentType = AdjustmentType.FULL_VIEW;
boolean rename = false;
if (args.length >= 4) {
int numFlags = args.length - 4;
for (int i = 0; i < numFlags; i++) {
if (args[i].compareToIgnoreCase("-rename") == 0) {
rename = true;
} else if (args[i].compareToIgnoreCase("-EXPAND") == 0) {
adjustmentType = AdjustmentType.EXPAND;
} else if (args[i].compareToIgnoreCase("-FULL_VIEW") == 0) {
adjustmentType = AdjustmentType.FULL_VIEW;
} else {
System.err.println("Unknown flag " + args[i]);
}
}
inputPath = args[numFlags];
regex = args[numFlags + 1];
pathIntrinsic = args[numFlags + 2];
outputDir = args[numFlags + 3];
} else {
printHelpAndExit(args);
System.exit(0);
return;
}
System.out.println("AdjustmentType = " + adjustmentType);
System.out.println("rename = " + rename);
System.out.println("input path = " + inputPath);
System.out.println("name regex = " + regex);
System.out.println("output dir = " + outputDir);
File fileOutputDir = new File(outputDir);
if (!fileOutputDir.exists()) {
if (!fileOutputDir.mkdirs()) {
throw new RuntimeException("Output directory did not exist and failed to create it");
} else {
System.out.println(" created output directory");
}
}
CameraPinholeRadial param = CalibrationIO.load(pathIntrinsic);
CameraPinholeRadial paramAdj = new CameraPinholeRadial();
List<File> files = Arrays.asList(UtilIO.findMatches(new File(inputPath), regex));
Collections.sort(files);
System.out.println("Found a total of " + files.size() + " matching files");
Planar<GrayF32> distoredImg = new Planar<>(GrayF32.class, param.width, param.height, 3);
Planar<GrayF32> undistoredImg = new Planar<>(GrayF32.class, param.width, param.height, 3);
ImageDistort distort = LensDistortionOps.changeCameraModel(adjustmentType, BorderType.ZERO, param, new CameraPinhole(param), paramAdj, (ImageType) distoredImg.getImageType());
CalibrationIO.save(paramAdj, new File(outputDir, "intrinsicUndistorted.yaml").getAbsolutePath());
BufferedImage out = new BufferedImage(param.width, param.height, BufferedImage.TYPE_INT_RGB);
int numDigits = BoofMiscOps.numDigits(files.size() - 1);
String format = "%0" + numDigits + "d";
for (int i = 0; i < files.size(); i++) {
File file = files.get(i);
System.out.println("processing " + file.getName());
BufferedImage orig = UtilImageIO.loadImage(file.getAbsolutePath());
if (orig == null) {
throw new RuntimeException("Can't load file: " + file.getAbsolutePath());
}
if (orig.getWidth() != param.width || orig.getHeight() != param.height) {
System.err.println("intrinsic parameters and image size do not match!");
System.exit(-1);
}
ConvertBufferedImage.convertFromPlanar(orig, distoredImg, true, GrayF32.class);
distort.apply(distoredImg, undistoredImg);
ConvertBufferedImage.convertTo(undistoredImg, out, true);
String nameOut;
if (rename) {
nameOut = String.format("image" + format + ".png", i);
} else {
nameOut = file.getName().split("\\.")[0] + "_undistorted.png";
}
UtilImageIO.saveImage(out, new File(outputDir, nameOut).getAbsolutePath());
}
}
use of boofcv.struct.calib.CameraPinholeRadial in project BoofCV by lessthanoptimal.
the class FiducialDetection method process.
private void process() {
if (detector == null) {
System.err.println("Need to specify which fiducial you wish to detect");
System.exit(1);
}
if (outputPath != null) {
try {
outputFile = new PrintStream(outputPath);
outputFile.println("# Results from fiducial detection ");
outputFile.println("# These comments should include the data source and the algorithm used, but I'm busy.");
outputFile.println("# ");
outputFile.println("# <frame #> <number of fiducials> <fiducial id> <X> <Y> <Z> <Q1> <Q2> <Q3> <Q4> ...");
outputFile.println("# ");
outputFile.println("# The special Euclidean transform saved each fiducial is from fiducial to camera");
outputFile.println("# (X,Y,Z) is the translation and (Q1,Q2,Q3,Q4) specifies a quaternion");
outputFile.println("# ");
} catch (FileNotFoundException e) {
System.err.println("Failed to open output file.");
System.err.println(e.getMessage());
System.exit(1);
}
}
MediaManager media = DefaultMediaManager.INSTANCE;
CameraPinholeRadial intrinsic = intrinsicPath == null ? null : (CameraPinholeRadial) CalibrationIO.load(intrinsicPath);
SimpleImageSequence<GrayU8> sequence = null;
long pause = 0;
BufferedImage buffered = null;
if (inputType == InputType.VIDEO || inputType == InputType.WEBCAM) {
if (inputType == InputType.WEBCAM) {
String device = getCameraDeviceString();
sequence = media.openCamera(device, desiredWidth, desiredHeight, ImageType.single(GrayU8.class));
} else {
// just assume 30ms is appropriate. Should let the use specify this number
pause = 30;
sequence = media.openVideo(filePath, ImageType.single(GrayU8.class));
sequence.setLoop(true);
}
intrinsic = handleIntrinsic(intrinsic, sequence.getNextWidth(), sequence.getNextHeight());
} else {
buffered = UtilImageIO.loadImage(filePath);
if (buffered == null) {
System.err.println("Can't find image or it can't be read. " + filePath);
System.exit(1);
}
intrinsic = handleIntrinsic(intrinsic, buffered.getWidth(), buffered.getHeight());
}
ImagePanel gui = new ImagePanel();
gui.setPreferredSize(new Dimension(intrinsic.width, intrinsic.height));
ShowImages.showWindow(gui, "Fiducial Detector", true);
detector.setLensDistortion(new LensDistortionRadialTangential(intrinsic), intrinsic.width, intrinsic.height);
if (sequence != null) {
processStream(intrinsic, sequence, gui, pause);
} else {
processImage(intrinsic, buffered, gui);
}
}
use of boofcv.struct.calib.CameraPinholeRadial in project BoofCV by lessthanoptimal.
the class TestBaseDetectFiducialSquare method lensRemoval.
/**
* Ensure that lens distortion is being removed from the fiducial square. All check to make sure the class
* updates everything when init is called again with a different model.
*/
@Test
public void lensRemoval() {
List<Point2D_F64> expected = new ArrayList<>();
expected.add(new Point2D_F64(60, 300 + 120));
expected.add(new Point2D_F64(60, 300));
expected.add(new Point2D_F64(60 + 120, 300));
expected.add(new Point2D_F64(60 + 120, 300 + 120));
DetectCorner detector = new DetectCorner();
CameraPinholeRadial intrinsic = new CameraPinholeRadial(500, 500, 0, 320, 240, width, height).fsetRadial(-0.1, -0.05);
detectWithLensDistortion(expected, detector, intrinsic);
intrinsic = new CameraPinholeRadial(500, 500, 0, 320, 240, width, height).fsetRadial(0.1, 0.05);
detectWithLensDistortion(expected, detector, intrinsic);
}
use of boofcv.struct.calib.CameraPinholeRadial in project BoofCV by lessthanoptimal.
the class GenericQrCodeDetectorChecks method multipleMarkers.
/**
* See if it can detect multiple markers in the image at the same time
*/
@Test
public void multipleMarkers() {
QrCodeDetector<GrayF32> detector = createDetector();
CameraPinholeRadial model = CalibrationIO.load(getClass().getResource("calib/pinhole_radial.yaml"));
SimulatePlanarWorld simulator = new SimulatePlanarWorld();
simulator.setCamera(model);
simulator.resetScene();
Se3_F64 markerToWorld0 = new Se3_F64();
Se3_F64 markerToWorld1 = new Se3_F64();
simulator.addTarget(markerToWorld0, simulatedTargetWidth, generateMarker());
simulator.addTarget(markerToWorld1, simulatedTargetWidth, generateMarker());
markerToWorld0.T.set(0.2, 0, 0.6);
markerToWorld1.T.set(-0.2, 0, 0.6);
simulator.render();
detector.process(simulator.getOutput());
if (display) {
ShowImages.showWindow(simulator.getOutput(), "Foo", true);
BoofMiscOps.sleep(10000);
}
List<QrCode> detections = detector.getDetections();
assertEquals(2, detections.size());
}
use of boofcv.struct.calib.CameraPinholeRadial in project BoofCV by lessthanoptimal.
the class GenericQrCodeDetectorChecks method scale.
/**
* The marker zooming in and out of the frame
*/
@Test
public void scale() {
QrCodeDetector<GrayF32> detector = createDetector();
CameraPinholeRadial model = CalibrationIO.load(getClass().getResource("calib/pinhole_radial.yaml"));
SimulatePlanarWorld simulator = new SimulatePlanarWorld();
simulator.setCamera(model);
simulator.resetScene();
Se3_F64 markerToWorld = new Se3_F64();
simulator.addTarget(markerToWorld, simulatedTargetWidth, generateMarker());
markerToWorld.T.set(0, 0, 0.3);
for (int i = 0; i < 30; i++) {
renderAndCheck(detector, simulator);
markerToWorld.T.z += 0.03;
}
}
Aggregations