34#include <visp3/core/vpPolygon.h>
35#include <visp3/mbt/vpMbtDistanceKltCylinder.h>
36#include <visp3/mbt/vpMbtDistanceKltPoints.h>
38#if defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
40#if defined(VISP_HAVE_CLIPPER)
44#if defined(__APPLE__) && defined(__MACH__)
45#include <TargetConditionals.h>
54 : c0Mo(), p1Ext(), p2Ext(), cylinder(), circle1(), circle2(), initPoints(), initPoints3D(), curPoints(),
55 curPointsInd(), nbPointsCur(0), nbPointsInit(0), minNbPoint(4), enoughPoints(false), cam(),
85 circle1.setWorldCoordinates(ABC[0], ABC[1], ABC[2], p1.
get_oX(), p1.
get_oY(), p1.
get_oZ(), r);
86 circle2.setWorldCoordinates(ABC[0], ABC[1], ABC[2], p2.
get_oX(), p2.
get_oY(), p2.
get_oZ(), r);
89 cylinder.setWorldCoordinates(ABC[0], ABC[1], ABC[2], (p1.
get_oX() + p2.
get_oX()) / 2.0,
104 cylinder.changeFrame(cMo);
109 initPoints = std::map<int, vpImagePoint>();
110 initPoints3D = std::map<int, vpPoint>();
111 curPoints = std::map<int, vpImagePoint>();
112 curPointsInd = std::map<int, int>();
114 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i++) {
117 _tracker.
getFeature(
static_cast<int>(i),
id, x_tmp, y_tmp);
122 if (
static_cast<unsigned int>(y_tmp) <
hiddenface->getMbScanLineRenderer().getPrimitiveIDs().getHeight() &&
123 static_cast<unsigned int>(x_tmp) <
hiddenface->getMbScanLineRenderer().getPrimitiveIDs().getWidth()) {
125 if (
hiddenface->getMbScanLineRenderer().getPrimitiveIDs()[
static_cast<unsigned int>(y_tmp)][
static_cast<unsigned int>(x_tmp)] ==
133 std::vector<vpImagePoint> roi;
146 double xm = 0, ym = 0;
148 double Z = computeZ(xm, ym);
150#ifdef TARGET_OS_IPHONE
151 initPoints[
static_cast<int>(id)] =
vpImagePoint(y_tmp, x_tmp);
152 curPoints[
static_cast<int>(id)] =
vpImagePoint(y_tmp, x_tmp);
153 curPointsInd[
static_cast<int>(id)] =
static_cast<int>(i);
157 curPointsInd[id] =
static_cast<int>(i);
163 p.setWorldCoordinates(xm * Z, ym * Z, Z);
164#ifdef TARGET_OS_IPHONE
165 initPoints3D[
static_cast<int>(id)] = p;
167 initPoints3D[id] = p;
175 if (nbPointsCur >= minNbPoint)
178 enoughPoints =
false;
197 curPoints = std::map<int, vpImagePoint>();
198 curPointsInd = std::map<int, int>();
200 for (
unsigned int i = 0; i < static_cast<unsigned int>(_tracker.
getNbFeatures()); i++) {
201 _tracker.
getFeature(
static_cast<int>(i),
id, x, y);
202 if (isTrackedFeature(
static_cast<int>(
id))) {
203#ifdef TARGET_OS_IPHONE
204 curPoints[
static_cast<int>(id)] =
vpImagePoint(
static_cast<double>(y),
static_cast<double>(x));
205 curPointsInd[
static_cast<int>(id)] =
static_cast<int>(i);
207 curPoints[id] =
vpImagePoint(
static_cast<double>(y),
static_cast<double>(x));
208 curPointsInd[id] =
static_cast<int>(i);
214 if (nbPointsCur >= minNbPoint)
217 enoughPoints =
false;
232 std::map<int, vpImagePoint> tmp;
233 std::map<int, int> tmp2;
234 unsigned int nbSupp = 0;
238 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
239 for (; iter != curPoints.end(); ++iter) {
240 if (_w[k] > threshold_outlier && _w[k + 1] > threshold_outlier) {
242 tmp[iter->first] =
vpImagePoint(iter->second.get_i(), iter->second.get_j());
243 tmp2[iter->first] = curPointsInd[iter->first];
248 initPoints.erase(iter->first);
255 curPoints = std::map<int, vpImagePoint>();
256 curPointsInd = std::map<int, int>();
260 if (nbPointsCur >= minNbPoint)
263 enoughPoints =
false;
281 unsigned int index_ = 0;
283 cylinder.changeFrame(_cMc0 * c0Mo);
285 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
286 for (; iter != curPoints.end(); ++iter) {
288 double i_cur(iter->second.get_i()), j_cur(iter->second.get_j());
290 double x_cur(0), y_cur(0);
297 double x0_transform(p0.
get_x()), y0_transform(p0.
get_y());
299 double Z = computeZ(x_cur, y_cur);
301 if (
vpMath::isNaN(Z) || Z < std::numeric_limits<double>::epsilon()) {
307 _J[2 * index_][0] = 0;
308 _J[2 * index_][1] = 0;
309 _J[2 * index_][2] = 0;
310 _J[2 * index_][3] = 0;
311 _J[2 * index_][4] = 0;
312 _J[2 * index_][5] = 0;
314 _J[2 * index_ + 1][0] = 0;
315 _J[2 * index_ + 1][1] = 0;
316 _J[2 * index_ + 1][2] = 0;
317 _J[2 * index_ + 1][3] = 0;
318 _J[2 * index_ + 1][4] = 0;
319 _J[2 * index_ + 1][5] = 0;
321 _R[2 * index_] = (x0_transform - x_cur);
322 _R[2 * index_ + 1] = (y0_transform - y_cur);
326 double invZ = 1.0 / Z;
328 _J[2 * index_][0] = -invZ;
329 _J[2 * index_][1] = 0;
330 _J[2 * index_][2] = x_cur * invZ;
331 _J[2 * index_][3] = x_cur * y_cur;
332 _J[2 * index_][4] = -(1 + x_cur * x_cur);
333 _J[2 * index_][5] = y_cur;
335 _J[2 * index_ + 1][0] = 0;
336 _J[2 * index_ + 1][1] = -invZ;
337 _J[2 * index_ + 1][2] = y_cur * invZ;
338 _J[2 * index_ + 1][3] = (1 + y_cur * y_cur);
339 _J[2 * index_ + 1][4] = -y_cur * x_cur;
340 _J[2 * index_ + 1][5] = -x_cur;
342 _R[2 * index_] = (x0_transform - x_cur);
343 _R[2 * index_ + 1] = (y0_transform - y_cur);
356bool vpMbtDistanceKltCylinder::isTrackedFeature(
int _id)
358 std::map<int, vpImagePoint>::iterator iter = initPoints.find(_id);
359 if (iter != initPoints.end())
376 unsigned char nb,
unsigned int shiftBorder)
378 int width = mask.cols;
379 int height = mask.rows;
384 int i_min, i_max, j_min, j_max;
385 std::vector<vpImagePoint> roi;
388 double shiftBorder_d =
static_cast<double>(shiftBorder);
389#if defined(VISP_HAVE_CLIPPER)
390 std::vector<vpImagePoint> roi_offset;
392 ClipperLib::Path path;
393 for (std::vector<vpImagePoint>::const_iterator it = roi.begin(); it != roi.end(); ++it) {
394 path.push_back(ClipperLib::IntPoint((ClipperLib::cInt)it->get_u(), (ClipperLib::cInt)it->get_v()));
397 ClipperLib::Paths solution;
398 ClipperLib::ClipperOffset co;
399 co.AddPath(path, ClipperLib::jtRound, ClipperLib::etClosedPolygon);
400 co.Execute(solution, -shiftBorder_d);
403 if (!solution.empty()) {
404 size_t index_max = 0;
406 if (solution.size() > 1) {
410 for (
size_t i = 0; i < solution.size(); i++) {
411 std::vector<vpImagePoint> corners;
413 for (
size_t j = 0; j < solution[i].size(); j++) {
414 corners.push_back(
vpImagePoint(
static_cast<double>(solution[i][j].Y),
static_cast<double>(solution[i][j].X)));
418 if (polygon_area.
getArea() > max_area) {
419 max_area = polygon_area.
getArea();
425 for (
size_t i = 0; i < solution[index_max].size(); i++) {
426 roi_offset.push_back(
vpImagePoint(
static_cast<double>(solution[index_max][i].Y),
static_cast<double>(solution[index_max][i].X)));
437#if defined(VISP_HAVE_CLIPPER)
444 if (i_min > height) {
447 if (i_max > height) {
457 for (
int i = i_min; i < i_max; i++) {
458 double i_d =
static_cast<double>(i);
460 for (
int j = j_min; j < j_max; j++) {
461 double j_d =
static_cast<double>(j);
463#if defined(VISP_HAVE_CLIPPER)
466 mask.ptr<uchar>(i)[j] = nb;
469 if (shiftBorder != 0) {
475 mask.at<
unsigned char>(i, j) = nb;
480 mask.at<
unsigned char>(i, j) = nb;
497 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
498 for (; iter != curPoints.end(); ++iter) {
501 iP.
set_i(
static_cast<double>(iter->second.get_i()));
502 iP.
set_j(
static_cast<double>(iter->second.get_j()));
508 std::stringstream ss;
521 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
522 for (; iter != curPoints.end(); ++iter) {
525 iP.
set_i(
static_cast<double>(iter->second.get_i()));
526 iP.
set_j(
static_cast<double>(iter->second.get_j()));
532 std::stringstream ss;
544 for (
size_t i = 0; i < models.size(); i++) {
557 for (
size_t i = 0; i < models.size(); i++) {
572 std::vector<std::vector<double> > features;
574 std::map<int, vpImagePoint>::const_iterator iter = curPoints.begin();
575 for (; iter != curPoints.end(); ++iter) {
578 iP.
set_i(
static_cast<double>(iter->second.get_i()));
579 iP.
set_j(
static_cast<double>(iter->second.get_j()));
585 std::vector<double> params = { 1,
588 features.push_back(params);
605 std::vector<std::vector<double> > models;
610 circle1.changeFrame(cMo);
611 circle2.changeFrame(cMo);
612 cylinder.changeFrame(cMo);
615 circle1.projection();
618 std::cout <<
"Problem projection circle 1";
621 circle2.projection();
624 std::cout <<
"Problem projection circle 2";
627 cylinder.projection();
637 double i11, i12, i21, i22, j11, j12, j21, j22;
652 std::vector<double> params1 = { 0,
654 models.push_back(params1);
656 std::vector<double> params2 = { 0,
659 models.push_back(params1);
660 models.push_back(params2);
670double vpMbtDistanceKltCylinder::computeZ(
const double &x,
const double &y)
684#elif !defined(VISP_BUILD_SHARED_LIBS)
687void dummy_vpMbtDistanceKltCylinder() { }
Generic class defining intrinsic camera parameters.
static void computeIntersectionPoint(const vpCircle &circle, const vpCameraParameters &cam, const double &rho, const double &theta, double &i, double &j)
Implementation of column vector and the associated operations.
Class to define RGB colors available for display functionalities.
double computeZ(double x, double y) const
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void set_ij(double ii, double jj)
Definition of the vpImage class member functions.
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
int getNbFeatures() const
Get the number of current features.
void getFeature(const int &index, long &id, float &x, float &y) const
static bool isNaN(double value)
static int round(double x)
Implementation of a matrix and operations on matrices.
void buildFrom(const vpPoint &p1, const vpPoint &p2, const double &r)
vpMbtDistanceKltCylinder()
void removeOutliers(const vpColVector &weight, const double &threshold_outlier)
unsigned int computeNbDetectedCurrent(const vpKltOpencv &_tracker)
std::vector< std::vector< double > > getFeaturesForDisplay()
void computeInteractionMatrixAndResidu(const vpHomogeneousMatrix &cMc0, vpColVector &_R, vpMatrix &_J)
virtual ~vpMbtDistanceKltCylinder()
bool useScanLine
Use scanline rendering.
void updateMask(cv::Mat &mask, unsigned char _nb=255, unsigned int _shiftBorder=0)
void init(const vpKltOpencv &_tracker, const vpHomogeneousMatrix &cMo)
std::vector< std::vector< double > > getModelForDisplay(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam)
void displayPrimitive(const vpImage< unsigned char > &_I)
std::vector< int > listIndicesCylinderBBox
Pointer to the polygon that define a face.
void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
vpMbHiddenFaces< vpMbtPolygon > * hiddenface
Pointer to the list of faces.
static void convertLine(const vpCameraParameters &cam, const double &rho_m, const double &theta_m, double &rho_p, double &theta_p)
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
double get_oX() const
Get the point oX coordinate in the object frame.
double get_y() const
Get the point y coordinate in the image plane.
double get_oZ() const
Get the point oZ coordinate in the object frame.
double get_x() const
Get the point x coordinate in the image plane.
double get_oY() const
Get the point oY coordinate in the object frame.
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &cP) const VP_OVERRIDE
static void getMinMaxRoi(const std::vector< vpImagePoint > &roi, int &i_min, int &i_max, int &j_min, int &j_max)
Defines a generic 2D polygon.
vpPolygon & buildFrom(const std::vector< vpImagePoint > &corners, const bool &create_convex_hull=false)
bool isInside(const vpImagePoint &iP, const PointInPolygonMethod &method=PnPolyRayCasting) const