// This file is part of OpenCV project. // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html // This code is also subject to the license terms in the LICENSE_WillowGarage.md file found in this module's directory #ifndef __OPENCV_RGBD_LINEMOD_HPP__ #define __OPENCV_RGBD_LINEMOD_HPP__ #include "opencv2/core.hpp" #include /****************************************************************************************\ * LINE-MOD * \****************************************************************************************/ namespace cv { namespace linemod { //! @addtogroup rgbd //! @{ /** * \brief Discriminant feature described by its location and label. */ struct CV_EXPORTS_W_SIMPLE Feature { CV_PROP_RW int x; ///< x offset CV_PROP_RW int y; ///< y offset CV_PROP_RW int label; ///< Quantization CV_WRAP Feature() : x(0), y(0), label(0) {} CV_WRAP 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_W_SIMPLE Template { CV_PROP int width; CV_PROP int height; CV_PROP int pyramid_level; CV_PROP std::vector features; void read(const FileNode& fn); void write(FileStorage& fs) const; }; /** * \brief Represents a modality operating over an image pyramid. */ class CV_EXPORTS_W QuantizedPyramid { public: // Virtual destructor virtual ~QuantizedPyramid() {} /** * \brief Compute quantized image at current pyramid level for online detection. * * \param[out] dst The destination 8-bit image. For each pixel at most one bit is set, * representing its classification. */ CV_WRAP virtual void quantize(CV_OUT Mat& dst) const =0; /** * \brief Extract most discriminant features at current pyramid level to form a new template. * * \param[out] templ The new template. */ CV_WRAP virtual bool extractTemplate(CV_OUT Template& templ) const =0; /** * \brief Go to the next pyramid level. * * \todo Allow pyramid scale factor other than 2 */ CV_WRAP virtual void pyrDown() =0; protected: /// Candidate feature with a score struct Candidate { Candidate(int x, int y, int label, float score); /// Sort candidates with high score to the front bool operator<(const Candidate& rhs) const { return score > rhs.score; } Feature f; float score; }; /** * \brief Choose candidate features so that they are not bunched together. * * \param[in] candidates Candidate features sorted by score. * \param[out] features Destination vector of selected features. * \param[in] num_features Number of candidates to select. * \param[in] distance Hint for desired distance between features. */ static void selectScatteredFeatures(const std::vector& candidates, std::vector& 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) {} /** * \brief Interface for modalities that plug into the LINE template matching representation. * * \todo Max response, to allow optimization of summing (255/MAX) features as uint8 */ class CV_EXPORTS_W Modality { public: // Virtual destructor virtual ~Modality() {} /** * \brief Form a quantized image pyramid from a source image. * * \param[in] src The source image. Type depends on the modality. * \param[in] mask Optional mask. If not empty, unmasked pixels are set to zero * in quantized image and cannot be extracted as features. */ CV_WRAP Ptr process(const Mat& src, const Mat& mask = Mat()) const { return processImpl(src, mask); } CV_WRAP virtual String name() const =0; CV_WRAP virtual void read(const FileNode& fn) =0; virtual void write(FileStorage& fs) const =0; /** * \brief Create modality by name. * * The following modality types are supported: * - "ColorGradient" * - "DepthNormal" */ CV_WRAP static Ptr create(const String& modality_type); /** * \brief Load a modality from file. */ CV_WRAP static Ptr create(const FileNode& fn); protected: // Indirection is because process() has a default parameter. virtual Ptr processImpl(const Mat& src, const Mat& mask) const =0; }; /** * \brief Modality that computes quantized gradient orientations from a color image. */ class CV_EXPORTS_W ColorGradient : public Modality { public: /** * \brief Default constructor. Uses reasonable default parameter values. */ ColorGradient(); /** * \brief Constructor. * * \param weak_threshold When quantizing, discard gradients with magnitude less than this. * \param num_features How many features a template must contain. * \param strong_threshold Consider as candidate features only gradients whose norms are * larger than this. */ ColorGradient(float weak_threshold, size_t num_features, float strong_threshold); CV_WRAP static Ptr create(float weak_threshold, size_t num_features, float strong_threshold); virtual String name() const CV_OVERRIDE; virtual void read(const FileNode& fn) CV_OVERRIDE; virtual void write(FileStorage& fs) const CV_OVERRIDE; CV_PROP float weak_threshold; CV_PROP size_t num_features; CV_PROP float strong_threshold; protected: virtual Ptr processImpl(const Mat& src, const Mat& mask) const CV_OVERRIDE; }; /** * \brief Modality that computes quantized surface normals from a dense depth map. */ class CV_EXPORTS_W DepthNormal : public Modality { public: /** * \brief Default constructor. Uses reasonable default parameter values. */ DepthNormal(); /** * \brief Constructor. * * \param distance_threshold Ignore pixels beyond this distance. * \param difference_threshold When computing normals, ignore contributions of pixels whose * depth difference with the central pixel is above this threshold. * \param num_features How many features a template must contain. * \param extract_threshold Consider as candidate feature only if there are no differing * orientations within a distance of extract_threshold. */ DepthNormal(int distance_threshold, int difference_threshold, size_t num_features, int extract_threshold); CV_WRAP static Ptr create(int distance_threshold, int difference_threshold, size_t num_features, int extract_threshold); virtual String name() const CV_OVERRIDE; virtual void read(const FileNode& fn) CV_OVERRIDE; virtual void write(FileStorage& fs) const CV_OVERRIDE; CV_PROP int distance_threshold; CV_PROP int difference_threshold; CV_PROP size_t num_features; CV_PROP int extract_threshold; protected: virtual Ptr processImpl(const Mat& src, const Mat& mask) const CV_OVERRIDE; }; /** * \brief Debug function to colormap a quantized image for viewing. */ CV_EXPORTS_W void colormap(const Mat& quantized, CV_OUT Mat& dst); /** * \brief Debug function to draw linemod features * @param img * @param templates see @ref Detector::addTemplate * @param tl template bbox top-left offset see @ref Detector::addTemplate * @param size marker size see @ref cv::drawMarker */ CV_EXPORTS_W void drawFeatures(InputOutputArray img, const std::vector