use of boofcv.alg.distort.brown.LensDistortionBrown in project BoofCV by lessthanoptimal.
the class VisualizeSquareBinaryFiducial method process.
public void process(String nameImage, @Nullable String nameIntrinsic) {
CameraPinholeBrown intrinsic = nameIntrinsic == null ? null : (CameraPinholeBrown) CalibrationIO.load(nameIntrinsic);
GrayF32 input = Objects.requireNonNull(UtilImageIO.loadImage(nameImage, GrayF32.class));
var undistorted = new GrayF32(input.width, input.height);
InputToBinary<GrayF32> inputToBinary = FactoryThresholdBinary.globalOtsu(0, 255, 1.0, true, GrayF32.class);
Detector detector = new Detector(gridWidth, borderWidth, inputToBinary);
detector.setLengthSide(0.1);
if (intrinsic != null) {
CameraPinholeBrown paramUndist = new CameraPinholeBrown();
ImageDistort<GrayF32, GrayF32> undistorter = LensDistortionOps.changeCameraModel(AdjustmentType.EXPAND, BorderType.EXTENDED, intrinsic, new CameraPinhole(intrinsic), paramUndist, ImageType.single(GrayF32.class));
detector.configure(new LensDistortionBrown(paramUndist), paramUndist.width, paramUndist.height, false);
undistorter.apply(input, undistorted);
detector.process(undistorted);
} else {
detector.process(input);
}
System.out.println("Total Found: " + detector.squares.size());
DogArray<FoundFiducial> fiducials = detector.getFound();
int N = Math.min(20, detector.squares.size());
ListDisplayPanel squares = new ListDisplayPanel();
for (int i = 0; i < N; i++) {
squares.addImage(VisualizeBinaryData.renderBinary(detector.squares.get(i), false, null), " " + i);
squares.addImage(ConvertBufferedImage.convertTo(detector.squaresGray.get(i), null), " " + i);
}
BufferedImage output = new BufferedImage(input.width, input.height, BufferedImage.TYPE_INT_RGB);
ConvertBufferedImage.convertTo(input, output);
Graphics2D g2 = output.createGraphics();
g2.setColor(Color.RED);
g2.setStroke(new BasicStroke(2));
for (int i = 0; i < N; i++) {
VisualizeShapes.drawArrowSubPixel(fiducials.get(i).distortedPixels, 3, 1, g2);
}
ShowImages.showWindow(output, "Binary", true);
ShowImages.showWindow(squares, "Candidates", true);
}
use of boofcv.alg.distort.brown.LensDistortionBrown 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;
CameraPinholeBrown intrinsic = intrinsicPath == null ? null : (CameraPinholeBrown) 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 = Objects.requireNonNull(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 = Objects.requireNonNull(media.openVideo(filePath, ImageType.single(GrayU8.class)));
sequence.setLoop(true);
}
intrinsic = handleIntrinsic(intrinsic, sequence.getWidth(), sequence.getHeight());
} 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);
throw new RuntimeException("Stupid null check");
}
intrinsic = handleIntrinsic(intrinsic, buffered.getWidth(), buffered.getHeight());
}
Objects.requireNonNull(intrinsic);
Objects.requireNonNull(buffered);
var gui = new ImagePanel();
gui.setPreferredSize(new Dimension(intrinsic.width, intrinsic.height));
ShowImages.showWindow(gui, "Fiducial Detector", true);
detector.setLensDistortion(new LensDistortionBrown(intrinsic), intrinsic.width, intrinsic.height);
if (sequence != null) {
processStream(intrinsic, sequence, gui, pause);
} else {
processImage(intrinsic, buffered, gui);
}
}
use of boofcv.alg.distort.brown.LensDistortionBrown in project BoofCV by lessthanoptimal.
the class CameraToEquirectangular_F64 method setCameraModel.
public void setCameraModel(CameraPinholeBrown camera) {
Point2Transform2_F64 pixelToNormalized = new LensDistortionBrown(camera).undistort_F64(true, false);
setCameraModel(camera.width, camera.height, pixelToNormalized);
}
use of boofcv.alg.distort.brown.LensDistortionBrown in project BoofCV by lessthanoptimal.
the class BundleToRectificationStereoParameters method setView1.
/**
* Specifies lens parameters for view-1. This is done independently since often the same view is compared against
* multiple other views
*/
public void setView1(BundleAdjustmentCamera bundle1, int width, int height) {
BoofMiscOps.checkTrue(width > 0);
BoofMiscOps.checkTrue(height > 0);
BundleAdjustmentOps.convert(bundle1, width, height, intrinsic1);
PerspectiveOps.pinholeToMatrix(intrinsic1, K1);
intrinsic1.width = width;
intrinsic1.height = height;
Point2Transform2_F64 p_to_p = new LensDistortionBrown(intrinsic1).undistort_F64(true, true);
view1_dist_to_undist = new PointToPixelTransform_F64(p_to_p);
}
use of boofcv.alg.distort.brown.LensDistortionBrown in project BoofCV by lessthanoptimal.
the class GenericFiducialDetectorChecks method checkCenter.
@Test
void checkCenter() {
// It's not specified if the center should be undistorted or distorted. Just make it easier by
// using undistorted
CameraPinholeBrown intrinsic = loadDistortion(false);
LensDistortionBrown lensDistorted = new LensDistortionBrown(intrinsic);
for (ImageType type : types) {
ImageBase image = renderImage(intrinsic, type);
FiducialDetector detector = createDetector(type);
detector.setLensDistortion(lensDistorted, image.width, image.height);
// ShowImages.showBlocking(image, "asdfasdf", 5_000);
detect(detector, image);
assertTrue(detector.totalFound() >= 1);
assertTrue(detector.is3D());
for (int i = 0; i < detector.totalFound(); i++) {
Se3_F64 fidToCam = new Se3_F64();
Point2D_F64 found = new Point2D_F64();
detector.getFiducialToCamera(i, fidToCam);
detector.getCenter(i, found);
Point2D_F64 rendered = new Point2D_F64();
WorldToCameraToPixel worldToPixel = PerspectiveOps.createWorldToPixel(lensDistorted, fidToCam);
worldToPixel.transform(new Point3D_F64(0, 0, 0), rendered);
// see if the reprojected is near the pixel location
assertEquals(0.0, rendered.distance(found), pixelAndProjectedTol);
}
}
}
Aggregations