0

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;
AmirulJ
  • 118
  • 1
  • 11
  • 1
    Each time you open the file, you destroy the current contents of it. Please read more about [the open modes for streams](https://en.cppreference.com/w/cpp/io/ios_base/openmode). A [decent book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) should also have been helpful. – Some programmer dude Aug 12 '22 at 09: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 '22 at 09:18
  • You should start with some basic ROS samples and tutorials. – Fruchtzwerg Aug 14 '22 at 11:54

0 Answers0