Skip to content

Commit

Permalink
format again
Browse files Browse the repository at this point in the history
  • Loading branch information
BytingBulldogs3539 committed May 5, 2024
1 parent b0216e1 commit 6b3cbd2
Showing 1 changed file with 65 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
import org.photonvision.vision.pipe.impl.FindBoardCornersPipe.FindBoardCornersPipeResult;

public class Calibrate3dPipe
extends
CVPipe<Calibrate3dPipe.CalibrationInput, CameraCalibrationCoefficients, Calibrate3dPipe.CalibratePipeParams> {
extends CVPipe<
Calibrate3dPipe.CalibrationInput,
CameraCalibrationCoefficients,
Calibrate3dPipe.CalibratePipeParams> {
public static class CalibrationInput {
final List<FindBoardCornersPipe.FindBoardCornersPipeResult> observations;
final FrameStaticProperties imageProps;
Expand All @@ -71,30 +73,33 @@ public CalibrationInput(
/**
* Runs the process for the pipe.
*
* @param in Input for pipe processing. In the format (Input image, object
* points, image points)
* @param in Input for pipe processing. In the format (Input image, object points, image points)
* @return Result of processing.
*/
@Override
protected CameraCalibrationCoefficients process(CalibrationInput in) {
var filteredIn = in.observations.stream()
.filter(
it -> it != null
&& it.imagePoints != null
&& it.objectPoints != null
&& it.size != null)
.collect(Collectors.toList());
var filteredIn =
in.observations.stream()
.filter(
it ->
it != null
&& it.imagePoints != null
&& it.objectPoints != null
&& it.size != null)
.collect(Collectors.toList());

CameraCalibrationCoefficients ret;
var start = System.nanoTime();
if (MrCalJNILoader.getInstance().isLoaded() && params.useMrCal) {
logger.debug("Calibrating with mrcal!");
ret = calibrateMrcal(
filteredIn, in.imageProps.horizontalFocalLength, in.imageProps.verticalFocalLength);
ret =
calibrateMrcal(
filteredIn, in.imageProps.horizontalFocalLength, in.imageProps.verticalFocalLength);
} else {
logger.debug("Calibrating with opencv!");
ret = calibrateOpenCV(
filteredIn, in.imageProps.horizontalFocalLength, in.imageProps.verticalFocalLength);
ret =
calibrateOpenCV(
filteredIn, in.imageProps.horizontalFocalLength, in.imageProps.verticalFocalLength);
}
var dt = System.nanoTime() - start;

Expand All @@ -109,8 +114,7 @@ protected CameraCalibrationCoefficients process(CalibrationInput in) {
+ "\ndistortionCoeffs:\n"
+ Arrays.toString(ret.distCoeffs.data)
+ "\n");
else
logger.info("Calibration failed! Review log for more details");
else logger.info("Calibration failed! Review log for more details");

return ret;
}
Expand All @@ -132,7 +136,7 @@ protected CameraCalibrationCoefficients calibrateOpenCV(
// initial camera matrix guess
double cx = (in.get(0).size.width / 2.0) - 0.5;
double cy = (in.get(0).size.width / 2.0) - 0.5;
cameraMatrix.put(0, 0, new double[] { fxGuess, 0, cx, 0, fyGuess, cy, 0, 0, 1 });
cameraMatrix.put(0, 0, new double[] {fxGuess, 0, cx, 0, fyGuess, cy, 0, 0, 1});

try {
// FindBoardCorners pipe outputs all the image points, object points, and frames
Expand Down Expand Up @@ -160,7 +164,8 @@ protected CameraCalibrationCoefficients calibrateOpenCV(
JsonMatOfDouble cameraMatrixMat = JsonMatOfDouble.fromMat(cameraMatrix);
JsonMatOfDouble distortionCoefficientsMat = JsonMatOfDouble.fromMat(distortionCoefficients);

var observations = createObservations(in, cameraMatrix, distortionCoefficients, rvecs, tvecs, null);
var observations =
createObservations(in, cameraMatrix, distortionCoefficients, rvecs, tvecs, null);

cameraMatrix.release();
distortionCoefficients.release();
Expand All @@ -180,42 +185,44 @@ protected CameraCalibrationCoefficients calibrateOpenCV(

protected CameraCalibrationCoefficients calibrateMrcal(
List<FindBoardCornersPipe.FindBoardCornersPipeResult> in, double fxGuess, double fyGuess) {
List<MatOfPoint2f> corner_locations = in.stream().map(it -> it.imagePoints).map(MatOfPoint2f::new)
.collect(Collectors.toList());
List<MatOfPoint2f> corner_locations =
in.stream().map(it -> it.imagePoints).map(MatOfPoint2f::new).collect(Collectors.toList());

int imageWidth = (int) in.get(0).size.width;
int imageHeight = (int) in.get(0).size.height;

MrCalResult result = MrCalJNI.calibrateCamera(
corner_locations,
params.boardWidth,
params.boardHeight,
params.squareSize,
imageWidth,
imageHeight,
(fxGuess + fyGuess) / 2.0);
MrCalResult result =
MrCalJNI.calibrateCamera(
corner_locations,
params.boardWidth,
params.boardHeight,
params.squareSize,
imageWidth,
imageHeight,
(fxGuess + fyGuess) / 2.0);

// intrinsics are fx fy cx cy from mrcal
JsonMatOfDouble cameraMatrixMat = new JsonMatOfDouble(
3,
3,
CvType.CV_64FC1,
new double[] {
// fx 0 cx
result.intrinsics[0],
0,
result.intrinsics[2],
// 0 fy cy
0,
result.intrinsics[1],
result.intrinsics[3],
// 0 0 1
0,
0,
1
});
JsonMatOfDouble distortionCoefficientsMat = new JsonMatOfDouble(1, 8, CvType.CV_64FC1,
Arrays.copyOfRange(result.intrinsics, 4, 12));
JsonMatOfDouble cameraMatrixMat =
new JsonMatOfDouble(
3,
3,
CvType.CV_64FC1,
new double[] {
// fx 0 cx
result.intrinsics[0],
0,
result.intrinsics[2],
// 0 fy cy
0,
result.intrinsics[1],
result.intrinsics[3],
// 0 0 1
0,
0,
1
});
JsonMatOfDouble distortionCoefficientsMat =
new JsonMatOfDouble(1, 8, CvType.CV_64FC1, Arrays.copyOfRange(result.intrinsics, 4, 12));

// Calculate optimized board poses manually. We get this for free from mrcal
// too, but that's not
Expand Down Expand Up @@ -258,13 +265,14 @@ protected CameraCalibrationCoefficients calibrateMrcal(
tvecs.add(tvec);
}

List<BoardObservation> observations = createObservations(
in,
cameraMatrixMat.getAsMat(),
distortionCoefficientsMat.getAsMatOfDouble(),
rvecs,
tvecs,
new double[] { result.warp_x, result.warp_y });
List<BoardObservation> observations =
createObservations(
in,
cameraMatrixMat.getAsMat(),
distortionCoefficientsMat.getAsMatOfDouble(),
rvecs,
tvecs,
new double[] {result.warp_x, result.warp_y});

rvecs.forEach(Mat::release);
tvecs.forEach(Mat::release);
Expand All @@ -273,7 +281,7 @@ protected CameraCalibrationCoefficients calibrateMrcal(
in.get(0).size,
cameraMatrixMat,
distortionCoefficientsMat,
new double[] { result.warp_x, result.warp_y },
new double[] {result.warp_x, result.warp_y},
observations,
new Size(params.boardWidth, params.boardHeight),
params.squareSize,
Expand Down

0 comments on commit 6b3cbd2

Please sign in to comment.