Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I would like to write match_info.num_inliers into csv file using cpp code. When I use my code, only one number is writen in the test.csv file as shown in the code. How can I write all the match_info.num_inliers numbers into the csv file? Thank you in advance.

#ifndef ESTIMATION_INTERNAL_H_
#define ESTIMATION_INTERNAL_H_
#include <combine_grids/merging_pipeline.h>
#include <fstream>
#include <cassert>
#include <opencv2/core/utility.hpp>
#include <opencv2/core/version.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/stitching/detail/matchers.hpp>
#ifdef HAVE_OPENCV_XFEATURES2D
#include <opencv2/xfeatures2d/nonfree.hpp>
#endif
namespace combine_grids
namespace internal
#if CV_VERSION_MAJOR >= 4
static inline cv::Ptr<cv::Feature2D> chooseFeatureFinder(FeatureType type)
  switch (type) {
    case FeatureType::AKAZE:
      return cv::AKAZE::create();
    case FeatureType::ORB:
      return cv::ORB::create();
    case FeatureType::SURF:
#ifdef HAVE_OPENCV_XFEATURES2D
      return xfeatures2d::SURF::create();
#else
      return cv::AKAZE::create();
#endif
  assert(false);
  return {};
#else  // (CV_VERSION_MAJOR < 4)
static inline cv::Ptr<cv::detail::FeaturesFinder>
chooseFeatureFinder(FeatureType type)
  switch (type) {
    case FeatureType::AKAZE:
      return cv::makePtr<cv::detail::AKAZEFeaturesFinder>();
    case FeatureType::ORB:
      return cv::makePtr<cv::detail::OrbFeaturesFinder>();
    case FeatureType::SURF:
      return cv::makePtr<cv::detail::SurfFeaturesFinder>();
  assert(false);
  return {};
#endif  // CV_VERSION_MAJOR >= 4
static inline void writeDebugMatchingInfo(
    const std::vector<cv::Mat>& images,
    const std::vector<cv::detail::ImageFeatures>& image_features,
    const std::vector<cv::detail::MatchesInfo>& pairwise_matches)
  for (auto& match_info : pairwise_matches) {
    if (match_info.H.empty() ||
        match_info.src_img_idx >= match_info.dst_img_idx) {
      continue;
    std::cout << match_info.src_img_idx << " " << match_info.dst_img_idx
              << std::endl
              << "features: "
              << image_features[size_t(match_info.src_img_idx)].keypoints.size()
              << image_features[size_t(match_info.dst_img_idx)].keypoints.size()
              << std::endl
              << "matches: " << match_info.matches.size() << std::endl
              << "inliers: " << match_info.num_inliers << std::endl
              << "inliers/matches ratio: "
              << match_info.num_inliers / double(match_info.matches.size())
              << std::endl
              << "confidence: " << match_info.confidence << std::endl
              << match_info.H << std::endl;
    cv::Mat img;
    // draw all matches
    cv::drawMatches(images[size_t(match_info.src_img_idx)],
                    image_features[size_t(match_info.src_img_idx)].keypoints,
                    images[size_t(match_info.dst_img_idx)],
                    image_features[size_t(match_info.dst_img_idx)].keypoints,
                    match_info.matches, img);
    cv::imwrite(std::to_string(match_info.src_img_idx) + "_" +
                    std::to_string(match_info.dst_img_idx) + "_matches.png",
                img);
    // draw inliers only
    cv::drawMatches(
        images[size_t(match_info.src_img_idx)],
        image_features[size_t(match_info.src_img_idx)].keypoints,
        images[size_t(match_info.dst_img_idx)],
        image_features[size_t(match_info.dst_img_idx)].keypoints,
        match_info.matches, img, cv::Scalar::all(-1), cv::Scalar::all(-1),
        *reinterpret_cast<const std::vector<char>*>(&match_info.inliers_mask));
    cv::imwrite(std::to_string(match_info.src_img_idx) + "_" +
                    std::to_string(match_info.dst_img_idx) +
                    "_matches_inliers.png",
                img);
    std::ofstream myfile;
    myfile.open("test.csv");
    myfile << match_info.num_inliers << std::endl;
}  // namespace internal
}  // namespace combine_grids
#endif  // ESTIMATION_INTERNAL_H_

The source code that I share is from https://github.com/hrnr/m-explore.git. Im just adding this code at the bottom of the source code.

std::ofstream myfile;
myfile.open("test.csv");
myfile << match_info.num_inliers << std::endl;
                Each time you open the file, you destroy the current contents of it. Please read more about the open modes for streams. A decent book or two should also have been helpful.
– Some programmer dude
                Aug 12, 2022 at 9:08
                Thank you for the information, do you know how to publish the 'match_info.num_inliers' into new topic using ROS?
– AmirulJ
                Aug 12, 2022 at 9:18
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.