An example function showing how to fit calculate a 2D line to a set of 2D points by Graph-Cut RANSAC
| 702 | |
| 703 | // An example function showing how to fit calculate a 2D line to a set of 2D points by Graph-Cut RANSAC |
| 704 | void test2DLineFitting( |
| 705 | const std::string& image_path_, // The path where the ground truth pose can be found |
| 706 | const double confidence_, // The RANSAC confidence value |
| 707 | const double inlier_outlier_threshold_, // The used inlier-outlier threshold in GC-RANSAC. |
| 708 | const double spatial_coherence_weight_, // The weight of the spatial coherence term in the graph-cut energy minimization. |
| 709 | const double sphere_radius_, // The radius of the sphere used for determining the neighborhood-graph |
| 710 | const double minimum_inlier_ratio_for_sprt_) // An assumption about the minimum inlier ratio used for the SPRT test |
| 711 | { |
| 712 | // Load the image |
| 713 | cv::Mat image = cv::imread(image_path_), |
| 714 | image_gray; |
| 715 | |
| 716 | // Checking if the image has been loaded |
| 717 | if (image.empty()) |
| 718 | { |
| 719 | fprintf(stderr, "Image '%s' has not been loaded correctly.\n", image_path_.c_str()); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | // Applying Canny edge detection |
| 724 | constexpr double |
| 725 | low_threshold = 50, |
| 726 | max_low_threshold = 100, |
| 727 | kernel_size = 3, |
| 728 | ratio = 3; |
| 729 | |
| 730 | cv::Mat detected_edges; |
| 731 | cv::cvtColor(image, image_gray, cv::COLOR_BGR2GRAY); |
| 732 | cv::blur(image_gray, detected_edges, cv::Size(3, 3)); |
| 733 | |
| 734 | cv::Canny(detected_edges, |
| 735 | detected_edges, |
| 736 | low_threshold, |
| 737 | low_threshold * ratio, |
| 738 | kernel_size); |
| 739 | |
| 740 | // Storing the points for line fitting |
| 741 | cv::Mat points(0, 2, CV_64F), |
| 742 | point(1, 2, CV_64F); |
| 743 | for (size_t row = 0; row < detected_edges.rows; row += 1) |
| 744 | for (size_t col = 0; col < detected_edges.cols; col += 1) |
| 745 | if (detected_edges.at<uchar>(row, col) > std::numeric_limits<int>::epsilon()) |
| 746 | { |
| 747 | point.at<double>(0) = col; |
| 748 | point.at<double>(1) = row; |
| 749 | points.push_back(point); |
| 750 | } |
| 751 | |
| 752 | // Initialize the neighborhood used in Graph-cut RANSAC and, perhaps, |
| 753 | // in the sampler if NAPSAC or Progressive-NAPSAC sampling is applied. |
| 754 | std::chrono::time_point<std::chrono::system_clock> start, end; // Variables for time measurement |
| 755 | start = std::chrono::system_clock::now(); // The starting time of the neighborhood calculation |
| 756 | neighborhood::GridNeighborhoodGraph<2> neighborhood(&points, // The data points |
| 757 | { image.cols / 16.0, |
| 758 | image.rows / 16.0 }, |
| 759 | 16); // The radius of the neighborhood sphere used for determining the neighborhood structure |
| 760 | end = std::chrono::system_clock::now(); // The end time of the neighborhood calculation |
| 761 | std::chrono::duration<double> elapsed_seconds = end - start; // The elapsed time in seconds |
no test coverage detected