mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 00:03:03 +04:00
Merge branch 4.x
This commit is contained in:
@@ -152,6 +152,15 @@ TEST_F(fisheyeTest, distortUndistortPoints)
|
||||
|
||||
TEST_F(fisheyeTest, undistortImage)
|
||||
{
|
||||
// we use it to reduce patch size for images in testdata
|
||||
auto throwAwayHalf = [](Mat img)
|
||||
{
|
||||
int whalf = img.cols / 2, hhalf = img.rows / 2;
|
||||
Rect tl(0, 0, whalf, hhalf), br(whalf, hhalf, whalf, hhalf);
|
||||
img(tl) = 0;
|
||||
img(br) = 0;
|
||||
};
|
||||
|
||||
cv::Matx33d theK = this->K;
|
||||
cv::Mat theD = cv::Mat(this->D);
|
||||
std::string file = combine(datasets_repository_path, "/calib-3_stereo_from_JY/left/stereo_pair_014.jpg");
|
||||
@@ -161,32 +170,41 @@ TEST_F(fisheyeTest, undistortImage)
|
||||
newK(0, 0) = 100;
|
||||
newK(1, 1) = 100;
|
||||
cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
|
||||
cv::Mat correct = cv::imread(combine(datasets_repository_path, "new_f_100.png"));
|
||||
if (correct.empty())
|
||||
CV_Assert(cv::imwrite(combine(datasets_repository_path, "new_f_100.png"), undistorted));
|
||||
else
|
||||
EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
|
||||
std::string imageFilename = combine(datasets_repository_path, "new_f_100.png");
|
||||
cv::Mat correct = cv::imread(imageFilename);
|
||||
ASSERT_FALSE(correct.empty()) << "Correct image " << imageFilename.c_str() << " can not be read" << std::endl;
|
||||
|
||||
throwAwayHalf(correct);
|
||||
throwAwayHalf(undistorted);
|
||||
|
||||
EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
|
||||
}
|
||||
{
|
||||
double balance = 1.0;
|
||||
cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);
|
||||
cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
|
||||
cv::Mat correct = cv::imread(combine(datasets_repository_path, "balance_1.0.png"));
|
||||
if (correct.empty())
|
||||
CV_Assert(cv::imwrite(combine(datasets_repository_path, "balance_1.0.png"), undistorted));
|
||||
else
|
||||
EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
|
||||
std::string imageFilename = combine(datasets_repository_path, "balance_1.0.png");
|
||||
cv::Mat correct = cv::imread(imageFilename);
|
||||
ASSERT_FALSE(correct.empty()) << "Correct image " << imageFilename.c_str() << " can not be read" << std::endl;
|
||||
|
||||
throwAwayHalf(correct);
|
||||
throwAwayHalf(undistorted);
|
||||
|
||||
EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
|
||||
}
|
||||
|
||||
{
|
||||
double balance = 0.0;
|
||||
cv::fisheye::estimateNewCameraMatrixForUndistortRectify(theK, theD, distorted.size(), cv::noArray(), newK, balance);
|
||||
cv::fisheye::undistortImage(distorted, undistorted, theK, theD, newK);
|
||||
cv::Mat correct = cv::imread(combine(datasets_repository_path, "balance_0.0.png"));
|
||||
if (correct.empty())
|
||||
CV_Assert(cv::imwrite(combine(datasets_repository_path, "balance_0.0.png"), undistorted));
|
||||
else
|
||||
EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
|
||||
std::string imageFilename = combine(datasets_repository_path, "balance_0.0.png");
|
||||
cv::Mat correct = cv::imread(imageFilename);
|
||||
ASSERT_FALSE(correct.empty()) << "Correct image " << imageFilename.c_str() << " can not be read" << std::endl;
|
||||
|
||||
throwAwayHalf(correct);
|
||||
throwAwayHalf(undistorted);
|
||||
|
||||
EXPECT_MAT_NEAR(correct, undistorted, 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,7 +306,9 @@ TEST_F(fisheyeTest, undistortAndDistortImage)
|
||||
EXPECT_MAT_NEAR(dist_point_4, dist_point_4_gt, 1e-2);
|
||||
EXPECT_MAT_NEAR(dist_point_5, dist_point_5_gt, 1e-2);
|
||||
|
||||
CV_Assert(cv::imwrite(combine(datasets_repository_path, "new_distortion.png"), image_projected));
|
||||
// Add the "--test_debug" to arguments for file output
|
||||
if (cvtest::debugLevel > 0)
|
||||
cv::imwrite(combine(datasets_repository_path, "new_distortion.png"), image_projected);
|
||||
}
|
||||
|
||||
TEST_F(fisheyeTest, jacobians)
|
||||
@@ -619,19 +639,19 @@ TEST_F(fisheyeTest, stereoRectify)
|
||||
0.002076471801477729, 0.006463478587068991, 0.9999769555891836
|
||||
);
|
||||
cv::Matx34d P1_ref(
|
||||
420.8551870450913, 0, 586.501617798451, 0,
|
||||
0, 420.8551870450913, 374.7667511986098, 0,
|
||||
420.9684016542647, 0, 586.3059567784627, 0,
|
||||
0, 420.9684016542647, 374.8571836462291, 0,
|
||||
0, 0, 1, 0
|
||||
);
|
||||
cv::Matx34d P2_ref(
|
||||
420.8551870450913, 0, 586.501617798451, -41.77758076597302,
|
||||
0, 420.8551870450913, 374.7667511986098, 0,
|
||||
420.9684016542647, 0, 586.3059567784627, -41.78881938824554,
|
||||
0, 420.9684016542647, 374.8571836462291, 0,
|
||||
0, 0, 1, 0
|
||||
);
|
||||
cv::Matx44d Q_ref(
|
||||
1, 0, 0, -586.501617798451,
|
||||
0, 1, 0, -374.7667511986098,
|
||||
0, 0, 0, 420.8551870450913,
|
||||
1, 0, 0, -586.3059567784627,
|
||||
0, 1, 0, -374.8571836462291,
|
||||
0, 0, 0, 420.9684016542647,
|
||||
0, 0, 10.07370889670733, -0
|
||||
);
|
||||
|
||||
@@ -686,7 +706,9 @@ TEST_F(fisheyeTest, stereoRectify)
|
||||
cv::Mat rectification;
|
||||
merge4(l, r, lundist, rundist, rectification);
|
||||
|
||||
cv::imwrite(cv::format("fisheye_rectification_AB_%03d.png", i), rectification);
|
||||
// Add the "--test_debug" to arguments for file output
|
||||
if (cvtest::debugLevel > 0)
|
||||
cv::imwrite(cv::format("fisheye_rectification_AB_%03d.png", i), rectification);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -861,6 +883,115 @@ TEST_F(fisheyeTest, CalibrationWithDifferentPointsNumber)
|
||||
cv::noArray(), cv::noArray(), flag, cv::TermCriteria(3, 20, 1e-6));
|
||||
}
|
||||
|
||||
#if 0 // not ported: #22519
|
||||
TEST_F(fisheyeTest, stereoCalibrateWithPerViewTransformations)
|
||||
{
|
||||
const int n_images = 34;
|
||||
|
||||
const std::string folder = combine(datasets_repository_path, "calib-3_stereo_from_JY");
|
||||
|
||||
std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
|
||||
std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
|
||||
std::vector<std::vector<cv::Point3d> > objectPoints(n_images);
|
||||
|
||||
cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
|
||||
CV_Assert(fs_left.isOpened());
|
||||
for(int i = 0; i < n_images; ++i)
|
||||
fs_left[cv::format("image_%d", i )] >> leftPoints[i];
|
||||
fs_left.release();
|
||||
|
||||
cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);
|
||||
CV_Assert(fs_right.isOpened());
|
||||
for(int i = 0; i < n_images; ++i)
|
||||
fs_right[cv::format("image_%d", i )] >> rightPoints[i];
|
||||
fs_right.release();
|
||||
|
||||
cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
|
||||
CV_Assert(fs_object.isOpened());
|
||||
for(int i = 0; i < n_images; ++i)
|
||||
fs_object[cv::format("image_%d", i )] >> objectPoints[i];
|
||||
fs_object.release();
|
||||
|
||||
cv::Matx33d K1, K2, theR;
|
||||
cv::Vec3d theT;
|
||||
cv::Vec4d D1, D2;
|
||||
|
||||
std::vector<cv::Mat> rvecs, tvecs;
|
||||
|
||||
int flag = 0;
|
||||
flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
|
||||
flag |= cv::fisheye::CALIB_CHECK_COND;
|
||||
flag |= cv::fisheye::CALIB_FIX_SKEW;
|
||||
|
||||
double rmsErrorStereoCalib = cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,
|
||||
K1, D1, K2, D2, imageSize, theR, theT, rvecs, tvecs, flag,
|
||||
cv::TermCriteria(3, 12, 0));
|
||||
|
||||
std::vector<cv::Point2d> reprojectedImgPts[2] = {std::vector<cv::Point2d>(n_images), std::vector<cv::Point2d>(n_images)};
|
||||
size_t totalPoints = 0;
|
||||
double totalMSError[2] = { 0, 0 };
|
||||
for( size_t i = 0; i < n_images; i++ )
|
||||
{
|
||||
cv::Matx33d viewRotMat1, viewRotMat2;
|
||||
cv::Vec3d viewT1, viewT2;
|
||||
cv::Mat rVec;
|
||||
cv::Rodrigues( rvecs[i], rVec );
|
||||
rVec.convertTo(viewRotMat1, CV_64F);
|
||||
tvecs[i].convertTo(viewT1, CV_64F);
|
||||
|
||||
viewRotMat2 = theR * viewRotMat1;
|
||||
cv::Vec3d T2t = theR * viewT1;
|
||||
viewT2 = T2t + theT;
|
||||
|
||||
cv::Vec3d viewRotVec1, viewRotVec2;
|
||||
cv::Rodrigues(viewRotMat1, viewRotVec1);
|
||||
cv::Rodrigues(viewRotMat2, viewRotVec2);
|
||||
|
||||
double alpha1 = K1(0, 1) / K1(0, 0);
|
||||
double alpha2 = K2(0, 1) / K2(0, 0);
|
||||
cv::fisheye::projectPoints(objectPoints[i], reprojectedImgPts[0], viewRotVec1, viewT1, K1, D1, alpha1);
|
||||
cv::fisheye::projectPoints(objectPoints[i], reprojectedImgPts[1], viewRotVec2, viewT2, K2, D2, alpha2);
|
||||
|
||||
double viewMSError[2] = {
|
||||
cv::norm(leftPoints[i], reprojectedImgPts[0], cv::NORM_L2SQR),
|
||||
cv::norm(rightPoints[i], reprojectedImgPts[1], cv::NORM_L2SQR)
|
||||
};
|
||||
|
||||
size_t n = objectPoints[i].size();
|
||||
totalMSError[0] += viewMSError[0];
|
||||
totalMSError[1] += viewMSError[1];
|
||||
totalPoints += n;
|
||||
}
|
||||
double rmsErrorFromReprojectedImgPts = std::sqrt((totalMSError[0] + totalMSError[1]) / (2 * totalPoints));
|
||||
|
||||
cv::Matx33d R_correct( 0.9975587205950972, 0.06953016383322372, 0.006492709911733523,
|
||||
-0.06956823121068059, 0.9975601387249519, 0.005833595226966235,
|
||||
-0.006071257768382089, -0.006271040135405457, 0.9999619062167968);
|
||||
cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);
|
||||
cv::Matx33d K1_correct (561.195925927249, 0, 621.282400272412,
|
||||
0, 562.849402029712, 380.555455380889,
|
||||
0, 0, 1);
|
||||
|
||||
cv::Matx33d K2_correct (560.395452535348, 0, 678.971652040359,
|
||||
0, 561.90171021422, 380.401340535339,
|
||||
0, 0, 1);
|
||||
|
||||
cv::Vec4d D1_correct (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);
|
||||
cv::Vec4d D2_correct (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);
|
||||
|
||||
EXPECT_MAT_NEAR(theR, R_correct, 1e-10);
|
||||
EXPECT_MAT_NEAR(theT, T_correct, 1e-10);
|
||||
|
||||
EXPECT_MAT_NEAR(K1, K1_correct, 1e-10);
|
||||
EXPECT_MAT_NEAR(K2, K2_correct, 1e-10);
|
||||
|
||||
EXPECT_MAT_NEAR(D1, D1_correct, 1e-10);
|
||||
EXPECT_MAT_NEAR(D2, D2_correct, 1e-10);
|
||||
|
||||
EXPECT_NEAR(rmsErrorStereoCalib, rmsErrorFromReprojectedImgPts, 1e-4);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_F(fisheyeTest, estimateNewCameraMatrixForUndistortRectify)
|
||||
{
|
||||
cv::Size size(1920, 1080);
|
||||
@@ -880,13 +1011,13 @@ TEST_F(fisheyeTest, estimateNewCameraMatrixForUndistortRectify)
|
||||
|
||||
cv::Mat K_new_truth(3, 3, cv::DataType<double>::type);
|
||||
|
||||
K_new_truth.at<double>(0, 0) = 387.4809086880343;
|
||||
K_new_truth.at<double>(0, 0) = 387.5118215642316;
|
||||
K_new_truth.at<double>(0, 1) = 0.0;
|
||||
K_new_truth.at<double>(0, 2) = 1036.669802754649;
|
||||
K_new_truth.at<double>(0, 2) = 1033.936556777084;
|
||||
|
||||
K_new_truth.at<double>(1, 0) = 0.0;
|
||||
K_new_truth.at<double>(1, 1) = 373.6375700303157;
|
||||
K_new_truth.at<double>(1, 2) = 538.8373261247601;
|
||||
K_new_truth.at<double>(1, 1) = 373.6673784974842;
|
||||
K_new_truth.at<double>(1, 2) = 538.794152656429;
|
||||
|
||||
K_new_truth.at<double>(2, 0) = 0.0;
|
||||
K_new_truth.at<double>(2, 1) = 0.0;
|
||||
|
||||
Reference in New Issue
Block a user