123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- #ifndef __OPENCV_OBJDETECT_LINEMOD_HPP__
- #define __OPENCV_OBJDETECT_LINEMOD_HPP__
- #include "opencv2/core.hpp"
- #include <map>
- namespace cv {
- namespace linemod {
- struct CV_EXPORTS Feature
- {
- int x;
- int y;
- int label;
- Feature() : x(0), y(0), label(0) {}
- Feature(int x, int y, int label);
- void read(const FileNode& fn);
- void write(FileStorage& fs) const;
- };
- inline Feature::Feature(int _x, int _y, int _label) : x(_x), y(_y), label(_label) {}
- struct CV_EXPORTS Template
- {
- int width;
- int height;
- int pyramid_level;
- std::vector<Feature> features;
- void read(const FileNode& fn);
- void write(FileStorage& fs) const;
- };
- class QuantizedPyramid
- {
- public:
-
- virtual ~QuantizedPyramid() {}
-
- virtual void quantize(Mat& dst) const =0;
-
- virtual bool extractTemplate(Template& templ) const =0;
-
- virtual void pyrDown() =0;
- protected:
-
- struct Candidate
- {
- Candidate(int x, int y, int label, float score);
-
- bool operator<(const Candidate& rhs) const
- {
- return score > rhs.score;
- }
- Feature f;
- float score;
- };
-
- static void selectScatteredFeatures(const std::vector<Candidate>& candidates,
- std::vector<Feature>& features,
- size_t num_features, float distance);
- };
- inline QuantizedPyramid::Candidate::Candidate(int x, int y, int label, float _score) : f(x, y, label), score(_score) {}
- class CV_EXPORTS Modality
- {
- public:
-
- virtual ~Modality() {}
-
- Ptr<QuantizedPyramid> process(const Mat& src,
- const Mat& mask = Mat()) const
- {
- return processImpl(src, mask);
- }
- virtual String name() const =0;
- virtual void read(const FileNode& fn) =0;
- virtual void write(FileStorage& fs) const =0;
-
- static Ptr<Modality> create(const String& modality_type);
-
- static Ptr<Modality> create(const FileNode& fn);
- protected:
-
- virtual Ptr<QuantizedPyramid> processImpl(const Mat& src,
- const Mat& mask) const =0;
- };
- class CV_EXPORTS ColorGradient : public Modality
- {
- public:
-
- ColorGradient();
-
- ColorGradient(float weak_threshold, size_t num_features, float strong_threshold);
- virtual String name() const;
- virtual void read(const FileNode& fn);
- virtual void write(FileStorage& fs) const;
- float weak_threshold;
- size_t num_features;
- float strong_threshold;
- protected:
- virtual Ptr<QuantizedPyramid> processImpl(const Mat& src,
- const Mat& mask) const;
- };
- class CV_EXPORTS DepthNormal : public Modality
- {
- public:
-
- DepthNormal();
-
- DepthNormal(int distance_threshold, int difference_threshold, size_t num_features,
- int extract_threshold);
- virtual String name() const;
- virtual void read(const FileNode& fn);
- virtual void write(FileStorage& fs) const;
- int distance_threshold;
- int difference_threshold;
- size_t num_features;
- int extract_threshold;
- protected:
- virtual Ptr<QuantizedPyramid> processImpl(const Mat& src,
- const Mat& mask) const;
- };
- void colormap(const Mat& quantized, Mat& dst);
- struct CV_EXPORTS Match
- {
- Match()
- {
- }
- Match(int x, int y, float similarity, const String& class_id, int template_id);
-
- bool operator<(const Match& rhs) const
- {
-
- if (similarity != rhs.similarity)
- return similarity > rhs.similarity;
- else
- return template_id < rhs.template_id;
- }
- bool operator==(const Match& rhs) const
- {
- return x == rhs.x && y == rhs.y && similarity == rhs.similarity && class_id == rhs.class_id;
- }
- int x;
- int y;
- float similarity;
- String class_id;
- int template_id;
- };
- inline
- Match::Match(int _x, int _y, float _similarity, const String& _class_id, int _template_id)
- : x(_x), y(_y), similarity(_similarity), class_id(_class_id), template_id(_template_id)
- {}
- class CV_EXPORTS Detector
- {
- public:
-
- Detector();
-
- Detector(const std::vector< Ptr<Modality> >& modalities, const std::vector<int>& T_pyramid);
-
- void match(const std::vector<Mat>& sources, float threshold, std::vector<Match>& matches,
- const std::vector<String>& class_ids = std::vector<String>(),
- OutputArrayOfArrays quantized_images = noArray(),
- const std::vector<Mat>& masks = std::vector<Mat>()) const;
-
- int addTemplate(const std::vector<Mat>& sources, const String& class_id,
- const Mat& object_mask, Rect* bounding_box = NULL);
-
- int addSyntheticTemplate(const std::vector<Template>& templates, const String& class_id);
-
- const std::vector< Ptr<Modality> >& getModalities() const { return modalities; }
-
- int getT(int pyramid_level) const { return T_at_level[pyramid_level]; }
-
- int pyramidLevels() const { return pyramid_levels; }
-
- const std::vector<Template>& getTemplates(const String& class_id, int template_id) const;
- int numTemplates() const;
- int numTemplates(const String& class_id) const;
- int numClasses() const { return static_cast<int>(class_templates.size()); }
- std::vector<String> classIds() const;
- void read(const FileNode& fn);
- void write(FileStorage& fs) const;
- String readClass(const FileNode& fn, const String &class_id_override = "");
- void writeClass(const String& class_id, FileStorage& fs) const;
- void readClasses(const std::vector<String>& class_ids,
- const String& format = "templates_%s.yml.gz");
- void writeClasses(const String& format = "templates_%s.yml.gz") const;
- protected:
- std::vector< Ptr<Modality> > modalities;
- int pyramid_levels;
- std::vector<int> T_at_level;
- typedef std::vector<Template> TemplatePyramid;
- typedef std::map<String, std::vector<TemplatePyramid> > TemplatesMap;
- TemplatesMap class_templates;
- typedef std::vector<Mat> LinearMemories;
-
- typedef std::vector< std::vector<LinearMemories> > LinearMemoryPyramid;
- void matchClass(const LinearMemoryPyramid& lm_pyramid,
- const std::vector<Size>& sizes,
- float threshold, std::vector<Match>& matches,
- const String& class_id,
- const std::vector<TemplatePyramid>& template_pyramids) const;
- };
- CV_EXPORTS Ptr<Detector> getDefaultLINE();
- CV_EXPORTS Ptr<Detector> getDefaultLINEMOD();
- }
- }
- #endif
|