mercredi 21 juillet 2021

How to avoid including OpenCV to my header?

Hope all's well for y'all. I just have a rookie-level question on C++ coding, hope to collect some ideas. I am writing a image-processing model which is to be wrapped to a C-based DLL API.

/* myModel.h */

#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect.hpp"
#include "nlohmann/json.hpp"
#include <variant>

class myModel
{
   public:

      myModel();

      loadCascadeClassifier( std::string fpath );

      struct DetectedResults
        {
            bool is_detected,
            cv::Rect bounding_box;
            cv::Point center;
        };

      myModel::DetectedResults detect( cv::Mat& input_image );

   private:

      std::vector< cv::Rect > box_stack;

      cv::CascadeClassifier Classifier;

      nlohmann::json json_params;

      // some other members...
}


/* myModel.cpp */

// Implementations...

The code can work without problem, but including myModel.h in other projects becomes troublesome. I realize that including resources in myModel.h may not be a good practice, since:

  1. other projects using myModel.cpp /.h need to have the same include/libraries and linkers.
  2. re-compilation takes time once myModel.h changed
  3. I don't know but is it good to use included objects as input arguments?

Therefore I researched some articles like this., which indicates to avoid inclusion in header by either:

Forward Declaration

Not applicable in my case. Looks like I cannot forward-declare incomplete-type classes for OpenCV objects, according to this discussion

Private Implementation

Should I try this one? Trying to figure out how to apply this.

This is not to seek for a solution of a bug, so any thoughts from you in any form will be welcomed. I'd be happy to learn how C++ pros tackle with such problem. Thanks in advance!

TL;DR How to avoid including resources in header myModel.h?

Aucun commentaire:

Enregistrer un commentaire