#include <fstream>
#include <opencv2/opencv.hpp>
#include "json.hpp"
#include "readCalibParameters.h"
int main() {
std::string jsonFilePath = std::string(SRCDIR) + "/stereo_calibration.json";
std::vector<cv::Matx33d> K;
std::vector<cv::Vec<double, 14>> k;
std::vector<cv::Vec3d> cam_rvecs;
std::vector<cv::Vec3d> cam_tvecs;
readCalibParameters(jsonFilePath, K, k, cam_rvecs, cam_tvecs);
std::vector<cv::Point3d> Q = {cv::Point3d(0.4, -0.02, 2.3)};
std::cout << "Q: " << Q << std::endl;
std::vector<cv::Point2d> q0, q1;
cv::projectPoints(Q, cam_rvecs[0], cam_tvecs[0], K[0], k[0], q0);
cv::projectPoints(Q, cam_rvecs[1], cam_tvecs[1], K[1], k[1], q1);
std::cout << "q0: " << q0[0] << std::endl;
std::cout << "q1: " << q1[0] << std::endl;
std::vector<cv::Point2d> q0u, q1u;
cv::undistortPoints(q0, q0u, K[0], k[0], cv::noArray(), K[0]);
cv::undistortPoints(q1, q1u, K[1], k[1], cv::noArray(), K[1]);
cv::Matx33d R0, R1;
cv::Rodrigues(cam_rvecs[0], R0);
cv::Rodrigues(cam_rvecs[1], R1);
cv::Matx33d tx(0.0, -cam_tvecs[1][2], cam_tvecs[1][1],
cam_tvecs[1][2], 0.0, -cam_tvecs[1][0],
-cam_tvecs[1][1], cam_tvecs[1][0], 0.0);
cv::Matx33d E = tx * R1;
cv::Matx33d F = K[1].t().inv() * E * K[0].inv();
std::cout << "q1u^T * F * q0u: "
<< cv::Vec3d(q1u[0].x, q1u[0].y, 1.0).t() * F *
cv::Vec3d(q0u[0].x, q0u[0].y, 1.0)
<< std::endl;
cv::Mat Rt0, Rt1;
cv::hconcat(R0, cam_tvecs[0], Rt0);
cv::hconcat(R1, cam_tvecs[1], Rt1);
cv::Matx34d P0 = cv::Mat(K[0] * Rt0);
cv::Matx34d P1 = cv::Mat(K[1] * Rt1);
std::vector<cv::Point2d> q0c, q1c;
cv::correctMatches(F, q0u, q1u, q0c, q1c);
cv::Vec4d Qhom;
cv::triangulatePoints(P0, P1, q0c, q1c, Qhom);
cv::Point3d Qtriang{Qhom(0) / Qhom(3), Qhom(1) / Qhom(3), Qhom(2) / Qhom(3)};
std::cout << "Qtriang: " << Qtriang << std::endl;
cv::Matx33d H0, H1;
cv::Matx34d P0rect, P1rect;
cv::Matx44d Qdtd;
cv::Size frameSize(5120, 3840);
cv::stereoRectify(K[0], k[0], K[1], k[1], frameSize, R1, cam_tvecs[1], H0, H1,
P0rect, P1rect, Qdtd);
cv::Mat map0X, map0Y, map1X, map1Y;
cv::initUndistortRectifyMap(K[0], k[0], H0, P0rect, frameSize, CV_32F, map0X,
map0Y);
cv::initUndistortRectifyMap(K[1], k[1], H1, P1rect, frameSize, CV_32F, map1X,
map1Y);
cv::Matx33f H0invx = cv::Matx33f(cv::Mat(H0.t()));
const float disp = 5.0;
const double row = 2000;
const double col = 500;
cv::Vec4f Qih = Qdtd * cv::Vec4f(col, row, disp, 1.0);
cv::Point3f Qdisp =
H0invx * cv::Point3f(Qih[0] / Qih[3], Qih[1] / Qih[3], Qih[2] / Qih[3]);
std::ifstream fileStream(jsonFilePath);
nlohmann::json jsonStruct;
try {
fileStream >> jsonStruct;
} catch (...) {
return -1;
}
int nPoses = jsonStruct["Calibration"]["poses"].size();
std::vector<cv::Vec3d> pose_rvecs(nPoses);
std::vector<cv::Vec3d> pose_tvecs(nPoses);
for (int i = 0; i < nPoses; ++i) {
auto poseI = jsonStruct["Calibration"]["poses"][i]["transform"];
auto rot = poseI["rotation"];
pose_rvecs[i] = {rot["rx"], rot["ry"], rot["rz"]};
auto t = poseI["translation"];
pose_tvecs[i] = cv::Vec3d(t["x"], t["y"], t["z"]);
}
int nTargets = jsonStruct["Calibration"]["targets"].size();
std::vector<std::vector<cv::Point3d>> target_objPoints(nTargets);
for (int i = 0; i < nTargets; ++i) {
auto objPointsI = jsonStruct["Calibration"]["targets"][i]["objectPoints"];
for (size_t j = 0; j < objPointsI.size(); ++j) {
auto P = objPointsI[j];
target_objPoints[i].emplace_back(P["x"], P["y"], P["z"]);
}
}
std::vector<cv::Vec3d> target_rvecs(nTargets);
std::vector<cv::Vec3d> target_tvecs(nTargets);
for (int i = 0; i < nTargets; ++i) {
auto poseI = jsonStruct["Calibration"]["targets"][i]["transform"];
auto rot = poseI["rotation"];
target_rvecs[i] = {rot["rx"], rot["ry"], rot["rz"]};
auto t = poseI["translation"];
target_tvecs[i] = cv::Vec3d(t["x"], t["y"], t["z"]);
}
const int pointId = 128;
const int targetId = 0;
const int poseId = 5;
const int camId = 1;
cv::Vec3d rvec, tvec;
cv::composeRT(target_rvecs[targetId], target_tvecs[targetId],
pose_rvecs[poseId], pose_tvecs[poseId], rvec, tvec);
cv::composeRT(rvec, tvec, cam_rvecs[camId], cam_tvecs[camId], rvec, tvec);
std::vector<cv::Point3d> P = {target_objPoints[targetId][pointId]};
std::vector<cv::Point2d> p;
cv::projectPoints(P, rvec, tvec, K[camId], k[camId], p);
std::cout << "p = " << p << std::endl;
}