Как получить доступ к параметру в классе C ++ - PullRequest
0 голосов
/ 25 сентября 2019

Я хочу изменить параметры серфинга во время работы функции компьютерной томографии, но я не знаю, как мне это сделать.cv :: xfeatures2d :: SurfFeatureDetector :: create (гессиан, октава, октавный слой);

как я могу сделать это динамически?Я попробовал какое-то решение, но я не мог сделать то, что я хочу.Я хочу изменить параметры серфинга во время выполнения функции компьютерной томографии, но я не знаю, как мне это сделать.cv :: xfeatures2d :: SurfFeatureDetector :: create (гессиан, октава, октавный слой);

как я могу сделать это динамически?Я попробовал какое-то решение, но я не смог сделать то, что хотел.

Я хочу изменить параметры прибоя во время работы функции компьютерной томографии, но я не знаю, как мне это сделать.cv :: xfeatures2d :: SurfFeatureDetector :: create (гессиан, октава, октавный слой);

как я могу сделать это динамически?Я попробовал какое-то решение, но я не смог сделать то, что я хотел.

#include "Stitcher.hpp"

Stitcher::Stitcher(cv::Ptr<cv::FeatureDetector> detector,
                   cv::Ptr<cv::DescriptorExtractor> extractor,
                   cv::Ptr<cv::DescriptorMatcher> matcher)
    : detector_(detector)
    , extractor_(extractor)
    , matcher_(matcher)
{
}

void Stitcher::computeHomography(const cv::Mat& image1, const cv::Mat& image2, cv::Mat& homography) const
{
    cv::Mat gray_image1, gray_image2;

    cv::cvtColor(image1, gray_image1, CV_RGB2GRAY);
    cv::cvtColor(image2, gray_image2, CV_RGB2GRAY);

    std::vector<cv::KeyPoint> keypoints_object, keypoints_scene;

    detector_->detect(gray_image1, keypoints_object);
    detector_->detect(gray_image2, keypoints_scene);

    //-- Step 2: Calculate descriptors (feature vectors)
    cv::Mat descriptors_object, descriptors_scene;

    extractor_->compute(gray_image1, keypoints_object, descriptors_object);
    extractor_->compute(gray_image2, keypoints_scene, descriptors_scene);

    //-- Step 3: Matching descriptor vectors using FLANN matcher
    std::vector<cv::DMatch> matches;
    matcher_->match(descriptors_object, descriptors_scene, matches);


    std::cout << "Descriptors object = " << descriptors_object.rows << std::endl;
    std::cout << "Descriptors scene = " << descriptors_scene.rows << std::endl;

    //-- Use only "good" matches (i.e. whose distance is less than 3*min_dist )
    std::vector<cv::DMatch > good_matches;
    // Normalize distances
    cv::Mat distances;
    for (const auto& m : matches)
    {
        distances.push_back(m.distance);
    }

    std::cout << "Normalizing distances..." << std::endl;
    // Normalize distances to have a [0,1] range
    cv::normalize(distances, distances, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
    std::cout << "Normalizing distances...DONE" << std::endl;
    // Store the normalized distance on corresponding pair
    for (int i = 0; i < distances.rows; ++i)
    {
        matches[i].distance = distances.at<float>(i, 0);
    }
    // Remove duplicates
    auto comparator = [](const cv::DMatch& a, const cv::DMatch& b)
    {
        return (a.distance < b.distance);
    };
    std::sort(matches.begin(), matches.end(), comparator);

    const int MATR_SIZE = matches.size();
    for (int i = 0; i < MATR_SIZE - 1; ++i)
    {
        for (int j = i + 1; j < MATR_SIZE; ++j)
        {
            // If the left keypoint or right keypoint occurred again
            if (matches[i].queryIdx == matches[j].queryIdx ||
                    matches[i].trainIdx == matches[j].trainIdx)
            {
                // Set to 1 since this is the highest possible value
                matches[j].distance = 1.0;
            }
        }
    }
    // end remove duplicates
    std::cout << "Searching for good matches..." << std::endl;
    for (int i = 0; i < descriptors_object.rows; i++)
    {
        if (matches[i].distance < 0.4)     // TODO Set threshold parameter
        {
            good_matches.push_back(matches[i]);
        }
    }

    std::cout << "Good matches = " << good_matches.size() << std::endl;
    std::vector<cv::Point2f> obj;
    std::vector<cv::Point2f> scene;

    for (int i = 0; i < good_matches.size(); i++)
    {
        //-- Get the keypoints from the good matches
        obj.push_back(keypoints_object[good_matches[i].queryIdx].pt);
        scene.push_back(keypoints_scene[good_matches[i].trainIdx].pt);
    }
    // Find the Homography Matrix
    std::cout << "Finding homography..." << std::endl;
    homography = cv::findHomography(obj, scene, CV_RANSAC);
    std::cout << "Find the Homography Matrix = \n" << homography << std::endl;
}

Брошюровщик: hpp:

#ifndef STITCHER_HPP
#define STITCHER_HPP

#include <opencv2/opencv.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>

class Stitcher
{
public:
    Stitcher(cv::Ptr<cv::FeatureDetector>, cv::Ptr<cv::DescriptorExtractor>, cv::Ptr<cv::DescriptorMatcher>);
    ~Stitcher() = default;
    void computeHomography(const cv::Mat&, const cv::Mat&, cv::Mat&) const;
    cv::Mat stitch(const cv::Mat&, const cv::Mat&, cv::Mat&, const cv::Mat&) const;
private:
    cv::Ptr<cv::FeatureDetector> detector_;
    cv::Ptr<cv::DescriptorExtractor> extractor_;
    cv::Ptr<cv::DescriptorMatcher> matcher_;
};

#endif  // STITCHER_HPP
...