39 #include <visp3/mbt/vpMbtDistanceKltPoints.h>
40 #include <visp3/core/vpPolygon.h>
42 #if defined(VISP_HAVE_MODULE_KLT) && (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100))
44 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
45 # include <TargetConditionals.h>
53 : H(), N(), N_cur(), invd0(1.), cRc0_0n(), initPoints(), curPoints(), curPointsInd(),
54 nbPointsCur(0), nbPointsInit(0), minNbPoint(4), enoughPoints(false), dt(1.), d0(1.),
55 cam(), isTrackedKltPoints(true), polygon(NULL), hiddenface(NULL), useScanLine(false)
57 initPoints = std::map<int, vpImagePoint>();
58 curPoints = std::map<int, vpImagePoint>();
59 curPointsInd = std::map<int, int>();
82 initPoints = std::map<int, vpImagePoint>();
83 curPoints = std::map<int, vpImagePoint>();
84 curPointsInd = std::map<int, int>();
85 std::vector<vpImagePoint> roi;
88 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i ++){
97 if((
unsigned int)y_tmp < hiddenface->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
98 (
unsigned int)x_tmp < hiddenface->getMbScanLineRenderer().getPrimitiveIDs().getWidth() &&
111 curPointsInd[(int)
id] = (
int)i;
115 curPointsInd[id] = (int)i;
122 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
123 else enoughPoints =
false;
129 N = plan.getNormal();
149 curPoints = std::map<int, vpImagePoint>();
150 curPointsInd = std::map<int, int>();
152 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i++){
154 if(isTrackedFeature((
int)
id)){
156 curPoints[(int)
id] =
vpImagePoint(static_cast<double>(y),
static_cast<double>(x));
157 curPointsInd[(int)
id] = (
int)i;
159 curPoints[id] =
vpImagePoint(static_cast<double>(y),static_cast<double>(x));
160 curPointsInd[id] = (int)i;
166 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
167 else enoughPoints =
false;
185 unsigned int index_ = 0;
187 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
188 for( ; iter != curPoints.end(); iter++){
190 double i_cur(iter->second.get_i()), j_cur(iter->second.get_j());
192 double x_cur(0), y_cur(0);
199 double x0_transform, y0_transform ;
200 computeP_mu_t(x0, y0, x0_transform, y0_transform, H );
202 double invZ = compute_1_over_Z(x_cur, y_cur);
204 _J[2*index_][0] = - invZ;
206 _J[2*index_][2] = x_cur * invZ;
207 _J[2*index_][3] = x_cur * y_cur;
208 _J[2*index_][4] = -(1+x_cur*x_cur);
209 _J[2*index_][5] = y_cur;
211 _J[2*index_+1][0] = 0;
212 _J[2*index_+1][1] = - invZ;
213 _J[2*index_+1][2] = y_cur * invZ;
214 _J[2*index_+1][3] = (1+y_cur*y_cur);
215 _J[2*index_+1][4] = - y_cur * x_cur;
216 _J[2*index_+1][5] = - x_cur;
218 _R[2*index_] = (x0_transform - x_cur);
219 _R[2*index_+1] = (y0_transform - y_cur);
225 vpMbtDistanceKltPoints::compute_1_over_Z(
const double x,
const double y)
227 double num = cRc0_0n[0] * x + cRc0_0n[1] * y + cRc0_0n[2];
228 double den = -(d0 - dt);
245 vpMbtDistanceKltPoints::computeP_mu_t(
const double x_in,
const double y_in,
double& x_out,
double& y_out,
const vpMatrix& _cHc0)
247 double p_mu_t_2 = x_in * _cHc0[2][0] + y_in * _cHc0[2][1] + _cHc0[2][2];
249 if( fabs(p_mu_t_2) < std::numeric_limits<double>::epsilon()){
255 x_out = (x_in * _cHc0[0][0] + y_in * _cHc0[0][1] + _cHc0[0][2]) / p_mu_t_2;
256 y_out = (x_in * _cHc0[1][0] + y_in * _cHc0[1][1] + _cHc0[1][2]) / p_mu_t_2;
282 vpGEMM(ctransc0, N, -invd0, cRc0, 1.0, cHc0, VP_GEMM_B_T);
305 for (
unsigned int i = 0; i < 3; i += 1){
306 dt += ctransc0[i] * (N_cur[i]);
318 vpMbtDistanceKltPoints::isTrackedFeature(
const int _id)
328 std::map<int, vpImagePoint>::iterator iter = initPoints.find(_id);
329 if(iter != initPoints.end())
345 #
if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
350 unsigned char nb,
unsigned int shiftBorder)
352 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
353 int width = mask.cols;
354 int height = mask.rows;
356 int width = mask->width;
357 int height = mask->height;
360 int i_min, i_max, j_min, j_max;
361 std::vector<vpImagePoint> roi;
379 double shiftBorder_d = (double) shiftBorder;
380 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
381 for(
int i=i_min; i< i_max; i++){
382 double i_d = (double) i;
383 for(
int j=j_min; j< j_max; j++){
384 double j_d = (double) j;
385 if(shiftBorder != 0){
391 mask.at<
unsigned char>(i,j) = nb;
396 mask.at<
unsigned char>(i,j) = nb;
402 unsigned char* ptrData = (
unsigned char*)mask->imageData + i_min*mask->widthStep+j_min;
403 for(
int i=i_min; i< i_max; i++){
404 double i_d = (double) i;
405 for(
int j=j_min; j< j_max; j++){
406 double j_d = (double) j;
407 if(shiftBorder != 0){
428 ptrData += mask->widthStep - j_max + j_min;
443 std::map<int, vpImagePoint> tmp;
444 std::map<int, int> tmp2;
445 unsigned int nbSupp = 0;
449 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
450 for( ; iter != curPoints.end(); iter++){
451 if(_w[k] > threshold_outlier && _w[k+1] > threshold_outlier){
453 tmp[iter->first] =
vpImagePoint(iter->second.get_i(), iter->second.get_j());
454 tmp2[iter->first] = curPointsInd[iter->first];
459 initPoints.erase(iter->first);
468 if(nbPointsCur >= minNbPoint) enoughPoints =
true;
469 else enoughPoints =
false;
481 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
482 for( ; iter != curPoints.end(); iter++){
485 iP.
set_i(static_cast<double>(iter->second.get_i()));
486 iP.
set_j(static_cast<double>(iter->second.get_j()));
493 sprintf(ide,
"%ld", static_cast<long int>(
id));
506 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
507 for( ; iter != curPoints.end(); iter++){
510 iP.
set_i(static_cast<double>(iter->second.get_i()));
511 iP.
set_j(static_cast<double>(iter->second.get_j()));
518 sprintf(ide,
"%ld", static_cast<long int>(
id));
525 const vpColor col,
const unsigned int thickness,
const bool displayFullModel)
529 std::vector<std::pair<vpPoint,unsigned int> > roi;
532 for (
unsigned int j = 0; j < roi.size(); j += 1){
541 std::vector<std::pair<vpPoint, vpPoint> > linesLst;
546 linesLst.push_back(std::make_pair(roi[j].first,roi[(j+1)%roi.size()].first));
548 for(
unsigned int i = 0 ; i < linesLst.size() ; i++){
549 linesLst[i].first.project();
550 linesLst[i].second.project();
564 const vpColor col,
const unsigned int thickness,
const bool displayFullModel)
568 std::vector<std::pair<vpPoint,unsigned int> > roi;
571 for (
unsigned int j = 0; j < roi.size(); j += 1){
580 std::vector<std::pair<vpPoint, vpPoint> > linesLst;
585 linesLst.push_back(std::make_pair(roi[j].first,roi[(j+1)%roi.size()].first));
587 for(
unsigned int i = 0 ; i < linesLst.size() ; i++){
588 linesLst[i].first.project();
589 linesLst[i].second.project();
600 #elif !defined(VISP_BUILD_SHARED_LIBS)
602 void dummy_vpMbtDistanceKltPoints() {};
Implementation of a matrix and operations on matrices.
void getRoiClipped(const vpCameraParameters &cam, std::vector< vpImagePoint > &roi)
void init(const vpKltOpencv &_tracker)
void removeOutliers(const vpColVector &weight, const double &threshold_outlier)
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Point coordinates conversion from normalized coordinates in meter to pixel coordinates ...
Class to define colors available for display functionnalities.
vpMbHiddenFaces< vpMbtPolygon > * hiddenface
Pointer to the list of faces.
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
virtual ~vpMbtDistanceKltPoints()
error that can be emited by ViSP classes.
vpMbScanLine & getMbScanLineRenderer()
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Point coordinates conversion from pixel coordinates to normalized coordinates in meter...
static int round(const double x)
static void getMinMaxRoi(const std::vector< vpImagePoint > &roi, int &i_min, int &i_max, int &j_min, int &j_max)
Implementation of a rotation matrix and operations on such kind of matrices.
bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method=PnPolyRayCasting) const
unsigned int computeNbDetectedCurrent(const vpKltOpencv &_tracker)
Implementation of an homography and operations on homographies.
void set_i(const double ii)
vpColVector & normalize()
vpPoint & getPoint(const unsigned int _index)
void displayPrimitive(const vpImage< unsigned char > &_I)
void getPolygonClipped(std::vector< std::pair< vpPoint, unsigned int > > &poly)
Generic class defining intrinsic camera parameters.
void extract(vpRotationMatrix &R) const
virtual bool isVisible(const vpHomogeneousMatrix &cMo, const double alpha, const bool &modulo=false, const vpCameraParameters &cam=vpCameraParameters(), const vpImage< unsigned char > &I=vpImage< unsigned char >())
void computeHomography(const vpHomogeneousMatrix &_cTc0, vpHomography &cHc0)
void set_j(const double jj)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void computeScanLineQuery(const vpPoint &a, const vpPoint &b, std::vector< std::pair< vpPoint, vpPoint > > &lines, const bool &displayResults=false)
Implementation of column vector and the associated operations.
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
void getFeature(const int &index, long &id, float &x, float &y) const
int getNbFeatures() const
Get the number of current features.
vpMbtPolygon * polygon
Pointer to the polygon that define a face.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
This class defines the container for a plane geometrical structure.
Class that consider the case of a translation vector.
void computeInteractionMatrixAndResidu(vpColVector &_R, vpMatrix &_J)
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor col, const unsigned int thickness=1, const bool displayFullModel=false)
bool useScanLine
Use scanline rendering.
void updateMask(cv::Mat &mask, unsigned char _nb=255, unsigned int _shiftBorder=0)