A method applying OpenCV for essential matrix estimation to one of the built-in scenes
| 1170 | |
| 1171 | // A method applying OpenCV for essential matrix estimation to one of the built-in scenes |
| 1172 | void opencvEssentialMatrixFitting( |
| 1173 | double ransac_confidence_, |
| 1174 | double threshold_, |
| 1175 | const std::string &test_scene_, |
| 1176 | bool draw_results_) |
| 1177 | { |
| 1178 | std::cout << "Processed scene = '" << test_scene_ << "'."; |
| 1179 | |
| 1180 | // Load the images of the current test scene |
| 1181 | cv::Mat image1 = cv::imread("../data/essential_matrix/" + test_scene_ + "1.png"); |
| 1182 | cv::Mat image2 = cv::imread("../data/essential_matrix/" + test_scene_ + "2.png"); |
| 1183 | if (image1.cols == 0) |
| 1184 | { |
| 1185 | image1 = cv::imread("../data/essential_matrix/" + test_scene_ + "1.jpg"); |
| 1186 | image2 = cv::imread("../data/essential_matrix/" + test_scene_ + "2.jpg"); |
| 1187 | } |
| 1188 | |
| 1189 | if (image1.cols == 0) |
| 1190 | { |
| 1191 | std::cerr << "A problem occured when loading the images for test scene " << test_scene_ << "."; |
| 1192 | return; |
| 1193 | } |
| 1194 | |
| 1195 | cv::Mat points; // The point correspondences, each is of format x1 y1 x2 y2 |
| 1196 | Eigen::Matrix3d intrinsics_source, // The intrinsic parameters of the source camera |
| 1197 | intrinsics_destination; // The intrinsic parameters of the destination camera |
| 1198 | |
| 1199 | // A function loading the points from files |
| 1200 | readPoints<4>("../data/essential_matrix/" + test_scene_ + "_pts.txt", |
| 1201 | points); |
| 1202 | |
| 1203 | // Loading the intrinsic camera matrices |
| 1204 | static const std::string source_intrinsics_path = "../data/essential_matrix/" + test_scene_ + "1.K"; |
| 1205 | if (!gcransac::utils::loadMatrix<double, 3, 3>(source_intrinsics_path, |
| 1206 | intrinsics_source)) |
| 1207 | { |
| 1208 | std::cerr << "An error occured when loading the intrinsics camera matrix from " << source_intrinsics_path << "."; |
| 1209 | return; |
| 1210 | } |
| 1211 | |
| 1212 | static const std::string destination_intrinsics_path = "../data/essential_matrix/" + test_scene_ + "2.K"; |
| 1213 | if (!gcransac::utils::loadMatrix<double, 3, 3>(destination_intrinsics_path, |
| 1214 | intrinsics_destination)) |
| 1215 | { |
| 1216 | std::cerr << "An error occured when loading the intrinsics camera matrix from " << destination_intrinsics_path << "."; |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | // Normalize the point coordinates by the intrinsic matrices |
| 1221 | cv::Mat normalized_points(points.size(), CV_64F); |
| 1222 | gcransac::utils::normalizeCorrespondences(points, |
| 1223 | intrinsics_source, |
| 1224 | intrinsics_destination, |
| 1225 | normalized_points); |
| 1226 | |
| 1227 | cv::Mat cv_intrinsics_source(3, 3, CV_64F); |
| 1228 | cv_intrinsics_source.at<double>(0, 0) = intrinsics_source(0, 0); |
| 1229 | cv_intrinsics_source.at<double>(0, 1) = intrinsics_source(0, 1); |