34 #include <visp3/ustk_template_tracking/usDenseTracker2D.h>
36 #include <visp3/core/vpImageTools.h>
38 #include <visp3/core/vpImageFilter.h>
58 vpImageTools::extract(I, m_template, R);
59 vpImageTools::extract(I, m_region, R);
61 m_height = m_template.getHeight();
62 m_width = m_template.getWidth();
63 m_size = m_template.getSize();
64 s_desired.resize(m_size);
65 s_current.resize(m_size);
66 m_LI.resize(m_size, 3);
69 vpImageFilter::getGradY(m_template, m_gradX);
70 vpImageFilter::getGradX(m_template, m_gradY);
73 double u0 = m_target.getHeight() / 2.0;
74 double v0 = m_target.getWidth() / 2.0;
78 for (
unsigned int u = 0; u < m_height; ++u)
79 for (
unsigned int v = 0; v < m_width; ++v) {
80 s_desired[u * m_width + v] = m_template[u][v];
81 s_current[u * m_width + v] = m_region[u][v];
82 m_LI[u * m_width + v][0] = m_gradX[u][v];
83 m_LI[u * m_width + v][1] = m_gradY[u][v];
84 m_LI[u * m_width + v][2] =
85 (
static_cast<double>(u) - u0) * m_gradY[u][v] - (
static_cast<double>(v) - v0) * m_gradX[u][v];
88 m_LI_inverse = m_LI.pseudoInverse();
101 unsigned int max_iter = 40;
105 double drms = 1.0e-5;
107 while ((i < max_iter) && (std::abs(rms - rms0) > drms)) {
109 vpImageTools::extract(I, m_region, m_target);
112 for (
unsigned int u = 0; u < m_height; ++u)
113 for (
unsigned int v = 0; v < m_width; ++v)
114 s_current[u * m_width + v] = m_region[u][v];
117 vpColVector e = s_current - s_desired;
118 vpColVector v = -gain * m_LI_inverse * e;
121 rms = e.frobeniusNorm() / m_size;
124 double alpha = m_target.getOrientation();
125 double dx = v[0] * cos(alpha) + v[1] * sin(alpha);
126 double dy = v[1] * cos(alpha) - v[0] * sin(alpha);
131 vpImagePoint(m_target.getCenter().get_i() + gain * dx, m_target.getCenter().get_j() + gain * dy));
132 m_target.setOrientation(alpha + gain * da);
146 throw(vpException(vpException::fatalError,
"usDenseTracker2D not initialized !"));
void update(const vpImage< unsigned char > &I)
Tracking method, to call at every new frame to track.
void init(const vpImage< unsigned char > &I, const vpRectOriented &R)
Initialisation of the tracker : to call to set the region to track (R) in the image (I) before starti...
vpImage< unsigned char > & getTemplate()
vpRectOriented getTarget() const
To call after update() at each new frame, to get the position of the ROI in the last acquired frame.
vpImage< unsigned char > & getRegion()