use of boofcv.struct.calib.CameraPinhole in project BoofCV by lessthanoptimal.
the class BatchRemoveLensDistortion method process.
public void process() {
cancel = false;
System.out.println("AdjustmentType = " + adjustmentType);
System.out.println("rename = " + rename);
System.out.println("input pattern = " + inputPattern);
System.out.println("output dir = " + outputPath);
File fileOutputDir = new File(outputPath);
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");
}
}
CameraPinholeBrown param = CalibrationIO.load(pathIntrinsic);
CameraPinholeBrown paramAdj = new CameraPinholeBrown();
List<String> paths = UtilIO.listSmartImages(inputPattern, false);
if (paths.isEmpty())
System.out.println("No inputs found. Bad path or pattern? " + inputPattern);
System.out.println("Found a total of " + paths.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(outputPath, "intrinsicUndistorted.yaml").getAbsolutePath());
BufferedImage out = new BufferedImage(param.width, param.height, BufferedImage.TYPE_INT_RGB);
int numDigits = BoofMiscOps.numDigits(paths.size() - 1);
String format = "%0" + numDigits + "d";
for (int i = 0; i < paths.size(); i++) {
File file = new File(paths.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);
}
if (listener != null)
listener.loadedImage(orig, file.getName());
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(outputPath, nameOut).getAbsolutePath());
if (cancel) {
break;
}
}
if (listener != null)
listener.finishedConverting();
}
use of boofcv.struct.calib.CameraPinhole in project BoofCV by lessthanoptimal.
the class PerspectiveOps method estimatePinhole.
/**
* Given the transform from pixels to normalized image coordinates, create an approximate pinhole model
* for this camera. Assumes (cx,cy) is the image center and that there is no skew.
*
* @param pixelToNorm Pixel coordinates into normalized image coordinates
* @param width Input image's width
* @param height Input image's height
* @return Approximate pinhole camera
*/
public static CameraPinhole estimatePinhole(Point2Transform2_F64 pixelToNorm, int width, int height) {
Point2D_F64 normA = new Point2D_F64();
Point2D_F64 normB = new Point2D_F64();
Vector3D_F64 vectorA = new Vector3D_F64();
Vector3D_F64 vectorB = new Vector3D_F64();
pixelToNorm.compute(0, height / 2, normA);
pixelToNorm.compute(width, height / 2, normB);
vectorA.setTo(normA.x, normA.y, 1);
vectorB.setTo(normB.x, normB.y, 1);
double hfov = UtilVector3D_F64.acute(vectorA, vectorB);
pixelToNorm.compute(width / 2, 0, normA);
pixelToNorm.compute(width / 2, height, normB);
vectorA.setTo(normA.x, normA.y, 1);
vectorB.setTo(normB.x, normB.y, 1);
double vfov = UtilVector3D_F64.acute(vectorA, vectorB);
CameraPinhole intrinsic = new CameraPinhole();
intrinsic.width = width;
intrinsic.height = height;
intrinsic.skew = 0;
intrinsic.cx = width / 2;
intrinsic.cy = height / 2;
intrinsic.fx = intrinsic.cx / Math.tan(hfov / 2);
intrinsic.fy = intrinsic.cy / Math.tan(vfov / 2);
return intrinsic;
}
use of boofcv.struct.calib.CameraPinhole in project BoofCV by lessthanoptimal.
the class ExampleFisheyeToPinhole method main.
public static void main(String[] args) {
// Path to image data and calibration data
String fisheyePath = UtilIO.pathExample("fisheye/theta/");
// load the fisheye camera parameters
CameraUniversalOmni fisheyeModel = CalibrationIO.load(new File(fisheyePath, "front.yaml"));
// Specify what the pinhole camera should look like
CameraPinhole pinholeModel = new CameraPinhole(400, 400, 0, 300, 300, 600, 600);
// Create the transform from pinhole to fisheye views
LensDistortionNarrowFOV pinholeDistort = new LensDistortionPinhole(pinholeModel);
LensDistortionWideFOV fisheyeDistort = new LensDistortionUniversalOmni(fisheyeModel);
NarrowToWidePtoP_F32 transform = new NarrowToWidePtoP_F32(pinholeDistort, fisheyeDistort);
// Load fisheye RGB image
BufferedImage bufferedFisheye = UtilImageIO.loadImage(fisheyePath, "front_table.jpg");
Planar<GrayU8> fisheyeImage = ConvertBufferedImage.convertFrom(bufferedFisheye, true, ImageType.pl(3, GrayU8.class));
// Create the image distorter which will render the image
InterpolatePixel<Planar<GrayU8>> interp = FactoryInterpolation.createPixel(0, 255, InterpolationType.BILINEAR, BorderType.ZERO, fisheyeImage.getImageType());
ImageDistort<Planar<GrayU8>, Planar<GrayU8>> distorter = FactoryDistort.distort(false, interp, fisheyeImage.getImageType());
// Pass in the transform created above
distorter.setModel(new PointToPixelTransform_F32(transform));
// Render the image. The camera will have a rotation of 0 and will thus be looking straight forward
Planar<GrayU8> pinholeImage = fisheyeImage.createNew(pinholeModel.width, pinholeModel.height);
distorter.apply(fisheyeImage, pinholeImage);
BufferedImage bufferedPinhole0 = ConvertBufferedImage.convertTo(pinholeImage, null, true);
// rotate the virtual pinhole camera to the right
transform.setRotationWideToNarrow(ConvertRotation3D_F32.eulerToMatrix(EulerType.YXZ, 0.8f, 0, 0, null));
distorter.apply(fisheyeImage, pinholeImage);
BufferedImage bufferedPinhole1 = ConvertBufferedImage.convertTo(pinholeImage, null, true);
// Display the results
ListDisplayPanel panel = new ListDisplayPanel();
panel.addImage(bufferedPinhole0, "Pinehole Forward");
panel.addImage(bufferedPinhole1, "Pinehole Right");
panel.addImage(bufferedFisheye, "Fisheye");
panel.setPreferredSize(new Dimension(600, 450));
ShowImages.showWindow(panel, "Fisheye to Pinhole", true);
}
use of boofcv.struct.calib.CameraPinhole in project BoofCV by lessthanoptimal.
the class TestSelfCalibrationLinearDualQuadratic method geometry_no_rotation.
@Test
void geometry_no_rotation() {
CameraPinhole intrinsic = new CameraPinhole(400, 420, 0.1, 0, 0, 0, 0);
renderTranslationOnly(intrinsic);
SelfCalibrationLinearDualQuadratic alg = new SelfCalibrationLinearDualQuadratic(false);
addProjectives(alg);
assertEquals(GeometricResult.GEOMETRY_POOR, alg.solve());
}
use of boofcv.struct.calib.CameraPinhole in project BoofCV by lessthanoptimal.
the class TestSelfCalibrationLinearDualQuadratic method solve_ZeroCP_ZSkew.
@Test
void solve_ZeroCP_ZSkew() {
List<CameraPinhole> intrinsics = new ArrayList<>();
for (int i = 0; i < 4; i++) {
intrinsics.add(new CameraPinhole(400 + i * 5, 420, 0, 0, 0, 0, 0));
}
checkSolve(intrinsics, false, true, 4);
}
Aggregations