Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rainyl committed Jan 20, 2025
1 parent 5b517a1 commit 6117929
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/dartcv/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ analyzer:
constant_identifier_names: ignore
missing_required_param: error
missing_return: error
non_constant_identifier_names: ignore
parameter_assignments: error
linter:
rules:
Expand Down
49 changes: 25 additions & 24 deletions packages/dartcv/lib/src/calib3d/calib3d.dart
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ Mat getDefaultNewCameraMatrix(InputArray cameraMatrix, {Size? imgsize, bool cent
}

// int cv::recoverPose (InputArray E, InputArray points1, InputArray points2, InputArray cameraMatrix, OutputArray R, OutputArray t, double distanceThresh, InputOutputArray mask=noArray(), OutputArray triangulatedPoints=noArray())
(int rval, Mat R, Mat t, Mat triangulatedPoints) recoverPoseWithCameraMatrix(
(int rval, Mat R, Mat t, Mat triangulatedPoints) recoverPoseCameraMatrix(
InputArray E,
InputArray points1,
InputArray points2,
Expand Down Expand Up @@ -974,26 +974,27 @@ Mat getDefaultNewCameraMatrix(InputArray cameraMatrix, {Size? imgsize, bool cent
}

// void cv::reprojectImageTo3D (InputArray disparity, OutputArray _3dImage, InputArray Q, bool handleMissingValues=false, int ddepth=-1)
Mat reprojectImageTo3D(
InputArray disparity,
InputArray Q, {
OutputArray? out3dImage,
bool handleMissingValues = false,
int ddepth = -1,
}) {
out3dImage ??= Mat.empty();
cvRun(
() => ccalib3d.cv_reprojectImageTo3D(
disparity.ref,
out3dImage!.ref,
Q.ref,
handleMissingValues,
ddepth,
ffi.nullptr,
),
);
return out3dImage;
}
// TODO: add Stereo
// Mat reprojectImageTo3D(
// InputArray disparity,
// InputArray Q, {
// OutputArray? out3dImage,
// bool handleMissingValues = false,
// int ddepth = -1,
// }) {
// out3dImage ??= Mat.empty();
// cvRun(
// () => ccalib3d.cv_reprojectImageTo3D(
// disparity.ref,
// out3dImage!.ref,
// Q.ref,
// handleMissingValues,
// ddepth,
// ffi.nullptr,
// ),
// );
// return out3dImage;
// }

// void cv::Rodrigues (InputArray src, OutputArray dst, OutputArray jacobian=noArray())
Mat Rodrigues(
Expand Down Expand Up @@ -1118,7 +1119,7 @@ double sampsonDistance(InputArray pt1, InputArray pt2, InputArray F) =>
/// Finds an object pose from 3D-2D point correspondences.
///
/// https://docs.opencv.org/4.11.0/d9/d0c/group__calib3d.html#ga624af8a6641b9bdb487f63f694e8bb90
(int rval, VecMat rvecs, VecMat tvecs, Mat rvec, Mat tvec, Mat reprojectionError) solvePnPGeneric(
(int rval, VecMat rvecs, VecMat tvecs, Mat reprojectionError) solvePnPGeneric(
InputArray objectPoints,
InputArray imagePoints,
InputArray cameraMatrix,
Expand Down Expand Up @@ -1156,7 +1157,7 @@ double sampsonDistance(InputArray pt1, InputArray pt2, InputArray F) =>
);
final rval = prval.value;
calloc.free(prval);
return (rval, rvecs, tvecs, rvec, tvec, reprojectionError);
return (rval, rvecs, tvecs, reprojectionError);
}

/// Finds an object pose from 3D-2D point correspondences using the RANSAC scheme.
Expand Down Expand Up @@ -1593,7 +1594,7 @@ Mat undistortPoints(
/// stereo correspondence algorithm
///
/// https://docs.opencv.org/4.11.0/d9/d0c/group__calib3d.html#ga214b498b8d01d0417e0d08be64c54eb5
/// TODO: add Stereo mathcers
/// TODO: add Stereo matchers
// void validateDisparity(
// InputOutputArray disparity,
// InputArray cost,
Expand Down
167 changes: 165 additions & 2 deletions packages/dartcv/test/calib3d/calib3d_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ void main() async {
}
});

test('cv.recoverPose', () {
test('cv.recoverPoseCameraMatrix', () {
final essential = cv.Mat.from2DList(
[
[1.503247056657373e-16, -7.074103796034695e-16, -7.781514175638166e-16],
Expand Down Expand Up @@ -882,12 +882,72 @@ void main() async {
cv.MatType.CV_64FC1,
);

final (rval, r, t, _) = cv.recoverPoseWithCameraMatrix(essential, p1, p2, k);
final (rval, r, t, _) = cv.recoverPoseCameraMatrix(essential, p1, p2, k);
expect(rval, 0);
expect(r.isEmpty, false);
expect(t.isEmpty, false);
});

test('cv.recoverPose', () {
final points1 = cv.Mat.from2DList(
[
<double>[150, 200],
<double>[130, 210],
<double>[120, 230],
<double>[110, 250],
<double>[200, 100],
<double>[210, 120],
<double>[230, 140],
<double>[250, 160],
],
cv.MatType.CV_64FC1,
);

final points2 = cv.Mat.from2DList(
[
<double>[152, 202],
<double>[132, 212],
<double>[122, 232],
<double>[112, 252],
<double>[202, 102],
<double>[212, 122],
<double>[232, 142],
<double>[252, 162],
],
cv.MatType.CV_64FC1,
);

final K = cv.Mat.from2DList(
[
<double>[1000, 0, 320],
<double>[0, 1000, 240],
<double>[0, 0, 1],
],
cv.MatType.CV_64FC1,
);

final E = cv.findEssentialMatCameraMatrix(points1, points2, K, method: cv.FM_RANSAC);
final (rval, r, t) = cv.recoverPose(E, points1, points2);
expect(rval, 4);
expect(r.isEmpty, false);
expect(t.isEmpty, false);
});

test('cv.RQDecomp3x3', () {
final K = cv.Mat.from2DList(
[
<double>[1000, 0, 320],
<double>[0, 1000, 240],
<double>[0, 0, 1],
],
cv.MatType.CV_64FC1,
);
final (rval, R, Q) = cv.RQDecomp3x3(K);
expect(rval, cv.Vec3d(0, 0, 0));
expect(R.isEmpty, false);
expect(Q.isEmpty, false);
});

test('cv.Rodrigues', () {
const double angle = 45;
final cos = math.cos(angle * math.pi / 180);
Expand All @@ -913,6 +973,70 @@ void main() async {
expect(jacobian.cols, 3);
});

test('cv.sampsonDistance', () {
final points1 = cv.Mat.from2DList(
[
<double>[150, 200, 1],
<double>[130, 210, 1],
<double>[120, 230, 1],
<double>[110, 250, 1],
],
cv.MatType.CV_64FC1,
);
final points2 = cv.Mat.from2DList(
[
<double>[152, 202, 1],
<double>[132, 212, 1],
<double>[122, 232, 1],
<double>[112, 252, 1],
],
cv.MatType.CV_64FC1,
);
final F = cv.Mat.from2DList(
[
<double>[1.292e-6, 3.303e-5, -0.004],
<double>[-3.299e-5, 1.120e-6, 0.017],
<double>[0.004, -0.017, 1],
],
cv.MatType.CV_64FC1,
);
final sampsonDistances = cv.sampsonDistance(points1, points2, F);
expect(sampsonDistances, closeTo(4034.6767, 1e-3));
});

test('cv.solveP3P', () {
final objectPoints = cv.Mat.from2DList(
[
<double>[0, 0, 0],
<double>[1, 0, 0],
<double>[0, 1, 0],
],
cv.MatType.CV_64FC1,
);
final imagePoints = cv.Mat.from2DList(
[
<double>[320, 240],
<double>[400, 240],
<double>[320, 320],
],
cv.MatType.CV_64FC1,
);
final cameraMatrix = cv.Mat.from2DList(
[
<double>[800, 0, 320],
<double>[0, 800, 240],
<double>[0, 0, 1],
],
cv.MatType.CV_64FC1,
);
final distCoeffs = cv.Mat.zeros(1, 4, cv.MatType.CV_64FC1);
final (ret, rvecs, tvecs) =
cv.solveP3P(objectPoints, imagePoints, cameraMatrix, distCoeffs, cv.SOLVEPNP_P3P);
expect(ret, 2);
expect(rvecs.isEmpty, false);
expect(tvecs.isEmpty, false);
});

test('cv.solvePnP', () async {
final rvec = cv.Mat.fromList(3, 1, cv.MatType.CV_32FC1, <double>[0, 0, 0]);
final tvec = cv.Mat.fromList(3, 1, cv.MatType.CV_32FC1, <double>[0, 0, 0]);
Expand Down Expand Up @@ -946,6 +1070,45 @@ void main() async {
expect(tv.isEmpty, false);
});

test('cv.solvePnPGeneric', () {
final objectPoints = cv.Mat.from2DList(
[
<double>[0, 0, 0],
<double>[1, 0, 0],
<double>[0, 1, 0],
<double>[1, 1, 0],
<double>[0.5, 0.5, 1],
<double>[0, 0.5, 1]
],
cv.MatType.CV_64FC1,
);
final imagePoints = cv.Mat.from2DList(
[
<double>[320, 240],
<double>[400, 240],
<double>[320, 320],
<double>[400, 320],
<double>[360, 270],
<double>[300, 250]
],
cv.MatType.CV_64FC1,
);
final cameraMatrix = cv.Mat.from2DList(
[
<double>[800, 0, 320],
<double>[0, 800, 240],
<double>[0, 0, 1],
],
cv.MatType.CV_64FC1,
);
final distCoeffs = cv.Mat.zeros(1, 4, cv.MatType.CV_64FC1);
final (ret, rvecs, tvecs, err) = cv.solvePnPGeneric(objectPoints, imagePoints, cameraMatrix, distCoeffs);
expect(ret, 1);
expect(rvecs.length, 1);
expect(tvecs.length, 1);
expect(err.isEmpty, false);
});

test('cv.triangulatePoints', () {
final projMat1 = cv.Mat.zeros(3, 4, cv.MatType.CV_64FC1);
final projMat2 = cv.Mat.zeros(3, 4, cv.MatType.CV_64FC1);
Expand Down

0 comments on commit 6117929

Please sign in to comment.