40 #include <visp3/core/vpPolygon.h> 41 #include <visp3/mbt/vpMbtDistanceKltPoints.h> 43 #if defined(VISP_HAVE_MODULE_KLT) && (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)) 45 #if defined(VISP_HAVE_CLIPPER) 46 #include <clipper.hpp> 49 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin) 50 #include <TargetConditionals.h> 58 : H(), N(), N_cur(), invd0(1.), cRc0_0n(), initPoints(
std::map<int,
vpImagePoint>()),
59 curPoints(
std::map<int,
vpImagePoint>()), curPointsInd(
std::map<int, int>()), nbPointsCur(0), nbPointsInit(0),
60 minNbPoint(4), enoughPoints(false), dt(1.), d0(1.), cam(), isTrackedKltPoints(true), polygon(NULL),
61 hiddenface(NULL), useScanLine(false)
83 initPoints = std::map<int, vpImagePoint>();
84 curPoints = std::map<int, vpImagePoint>();
85 curPointsInd = std::map<int, int>();
86 std::vector<vpImagePoint> roi;
89 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() &&
110 curPointsInd[(int)
id] = (
int)i;
114 curPointsInd[id] = (int)i;
119 nbPointsInit = (
unsigned int)initPoints.size();
120 nbPointsCur = (
unsigned int)curPoints.size();
122 if (nbPointsCur >= minNbPoint)
125 enoughPoints =
false;
131 N = plan.getNormal();
151 curPoints = std::map<int, vpImagePoint>();
152 curPointsInd = std::map<int, int>();
154 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i++) {
156 if (isTrackedFeature((
int)
id)) {
158 curPoints[(int)
id] =
vpImagePoint(static_cast<double>(y),
static_cast<double>(x));
159 curPointsInd[(int)
id] = (
int)i;
161 curPoints[id] =
vpImagePoint(static_cast<double>(y), static_cast<double>(x));
162 curPointsInd[id] = (int)i;
167 nbPointsCur = (
unsigned int)curPoints.size();
169 if (nbPointsCur >= minNbPoint)
172 enoughPoints =
false;
189 unsigned int index_ = 0;
191 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
192 for (; iter != curPoints.end(); ++iter) {
194 double i_cur(iter->second.get_i()), j_cur(iter->second.get_j());
196 double x_cur(0), y_cur(0);
205 computeP_mu_t(x0, y0, x0_transform, y0_transform, H);
207 double invZ = compute_1_over_Z(x_cur, y_cur);
209 _J[2 * index_][0] = -invZ;
210 _J[2 * index_][1] = 0;
211 _J[2 * index_][2] = x_cur * invZ;
212 _J[2 * index_][3] = x_cur * y_cur;
213 _J[2 * index_][4] = -(1 + x_cur * x_cur);
214 _J[2 * index_][5] = y_cur;
216 _J[2 * index_ + 1][0] = 0;
217 _J[2 * index_ + 1][1] = -invZ;
218 _J[2 * index_ + 1][2] = y_cur * invZ;
219 _J[2 * index_ + 1][3] = (1 + y_cur * y_cur);
220 _J[2 * index_ + 1][4] = -y_cur * x_cur;
221 _J[2 * index_ + 1][5] = -x_cur;
223 _R[2 * index_] = (x0_transform - x_cur);
224 _R[2 * index_ + 1] = (y0_transform - y_cur);
229 double vpMbtDistanceKltPoints::compute_1_over_Z(
const double x,
const double y)
231 double num = cRc0_0n[0] * x + cRc0_0n[1] * y + cRc0_0n[2];
232 double den = -(d0 - dt);
248 inline void vpMbtDistanceKltPoints::computeP_mu_t(
const double x_in,
const double y_in,
double &x_out,
double &y_out,
251 double p_mu_t_2 = x_in * _cHc0[2][0] + y_in * _cHc0[2][1] + _cHc0[2][2];
253 if (fabs(p_mu_t_2) < std::numeric_limits<double>::epsilon()) {
259 x_out = (x_in * _cHc0[0][0] + y_in * _cHc0[0][1] + _cHc0[0][2]) / p_mu_t_2;
260 y_out = (x_in * _cHc0[1][0] + y_in * _cHc0[1][1] + _cHc0[1][2]) / p_mu_t_2;
286 vpGEMM(ctransc0, N, -invd0, cRc0, 1.0, cHc0, VP_GEMM_B_T);
309 for (
unsigned int i = 0; i < 3; i += 1) {
310 dt += ctransc0[i] * (N_cur[i]);
321 bool vpMbtDistanceKltPoints::isTrackedFeature(
const int _id)
331 std::map<int, vpImagePoint>::iterator iter = initPoints.find(_id);
332 if (iter != initPoints.end())
348 #
if (VISP_HAVE_OPENCV_VERSION >= 0x020408)
353 unsigned char nb,
unsigned int shiftBorder)
355 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 356 int width = mask.cols;
357 int height = mask.rows;
359 int width = mask->width;
360 int height = mask->height;
363 int i_min, i_max, j_min, j_max;
364 std::vector<vpImagePoint> roi;
367 double shiftBorder_d = (double)shiftBorder;
369 #if defined(VISP_HAVE_CLIPPER) 370 std::vector<vpImagePoint> roi_offset;
372 ClipperLib::Path path;
373 for (std::vector<vpImagePoint>::const_iterator it = roi.begin(); it != roi.end(); ++it) {
374 path.push_back(ClipperLib::IntPoint((ClipperLib::cInt)it->get_u(), (ClipperLib::cInt)it->get_v()));
377 ClipperLib::Paths solution;
378 ClipperLib::ClipperOffset co;
379 co.AddPath(path, ClipperLib::jtRound, ClipperLib::etClosedPolygon);
380 co.Execute(solution, -shiftBorder_d);
383 if (!solution.empty()) {
384 size_t index_max = 0;
386 if (solution.size() > 1) {
390 for (
size_t i = 0; i < solution.size(); i++) {
391 std::vector<vpImagePoint> corners;
393 for (
size_t j = 0; j < solution[i].size(); j++) {
394 corners.push_back(
vpImagePoint((
double)(solution[i][j].Y), (
double)(solution[i][j].X)));
398 if (polygon_area.
getArea() > max_area) {
399 max_area = polygon_area.
getArea();
405 for (
size_t i = 0; i < solution[index_max].size(); i++) {
406 roi_offset.push_back(
vpImagePoint((
double)(solution[index_max][i].Y), (
double)(solution[index_max][i].X)));
416 #if defined(VISP_HAVE_CLIPPER) 423 if (i_min > height) {
426 if (i_max > height) {
436 #if (VISP_HAVE_OPENCV_VERSION >= 0x020408) 437 for (
int i = i_min; i < i_max; i++) {
438 double i_d = (double)i;
440 for (
int j = j_min; j < j_max; j++) {
441 double j_d = (double)j;
443 #if defined(VISP_HAVE_CLIPPER) 446 mask.ptr<uchar>(i)[j] = nb;
449 if (shiftBorder != 0) {
454 mask.at<
unsigned char>(i, j) = nb;
458 mask.at<
unsigned char>(i, j) = nb;
465 unsigned char *ptrData = (
unsigned char *)mask->imageData + i_min * mask->widthStep + j_min;
466 for (
int i = i_min; i < i_max; i++) {
467 double i_d = (double)i;
468 for (
int j = j_min; j < j_max; j++) {
469 double j_d = (double)j;
470 if (shiftBorder != 0) {
487 ptrData += mask->widthStep - j_max + j_min;
502 std::map<int, vpImagePoint> tmp;
503 std::map<int, int> tmp2;
504 unsigned int nbSupp = 0;
508 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
509 for (; iter != curPoints.end(); ++iter) {
510 if (_w[k] > threshold_outlier && _w[k + 1] > threshold_outlier) {
512 tmp[iter->first] =
vpImagePoint(iter->second.get_i(), iter->second.get_j());
513 tmp2[iter->first] = curPointsInd[iter->first];
517 initPoints.erase(iter->first);
526 if (nbPointsCur >= minNbPoint)
529 enoughPoints =
false;
540 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
541 for (; iter != curPoints.end(); ++iter) {
544 iP.
set_i(static_cast<double>(iter->second.get_i()));
545 iP.
set_j(static_cast<double>(iter->second.get_j()));
551 std::stringstream ss;
564 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
565 for (; iter != curPoints.end(); ++iter) {
568 iP.
set_i(static_cast<double>(iter->second.get_i()));
569 iP.
set_j(static_cast<double>(iter->second.get_j()));
575 std::stringstream ss;
583 const bool displayFullModel)
586 std::vector<std::pair<vpPoint, unsigned int> > roi;
589 for (
unsigned int j = 0; j < roi.size(); j += 1) {
598 std::vector<std::pair<vpPoint, vpPoint> > linesLst;
603 linesLst.push_back(std::make_pair(roi[j].first, roi[(j + 1) % roi.size()].first));
605 for (
unsigned int i = 0; i < linesLst.size(); i++) {
606 linesLst[i].first.project();
607 linesLst[i].second.project();
620 const bool displayFullModel)
623 std::vector<std::pair<vpPoint, unsigned int> > roi;
626 for (
unsigned int j = 0; j < roi.size(); j += 1) {
635 std::vector<std::pair<vpPoint, vpPoint> > linesLst;
640 linesLst.push_back(std::make_pair(roi[j].first, roi[(j + 1) % roi.size()].first));
642 for (
unsigned int i = 0; i < linesLst.size(); i++) {
643 linesLst[i].first.project();
644 linesLst[i].second.project();
655 #elif !defined(VISP_BUILD_SHARED_LIBS) 658 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.
int getNbFeatures() const
Get the number of current features.
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...
void extract(vpRotationMatrix &R) const
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.
unsigned int computeNbDetectedCurrent(const vpKltOpencv &_tracker)
Implementation of an homography and operations on homographies.
Defines a generic 2D polygon.
void set_i(const double ii)
vpColVector & normalize()
vpPoint & getPoint(const unsigned int _index)
void displayPrimitive(const vpImage< unsigned char > &_I)
bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method=PnPolyRayCasting) const
void getPolygonClipped(std::vector< std::pair< vpPoint, unsigned int > > &poly)
Generic class defining intrinsic camera parameters.
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 getFeature(const int &index, long &id, float &x, float &y) const
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 ...
vpMbtPolygon * polygon
Pointer to the polygon that define a face.
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)
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 buildFrom(const std::vector< vpImagePoint > &corners)
void set_ij(const double ii, const double jj)
bool useScanLine
Use scanline rendering.
void updateMask(cv::Mat &mask, unsigned char _nb=255, unsigned int _shiftBorder=0)