39 #include <visp3/vision/vpPlanarObjectDetector.h> 41 #if (VISP_HAVE_OPENCV_VERSION >= 0x020000) && \ 42 (VISP_HAVE_OPENCV_VERSION < 0x030000) // Require opencv >= 2.0.0 and < 3.0.0 44 #include <visp3/core/vpColor.h> 45 #include <visp3/core/vpDisplay.h> 46 #include <visp3/core/vpException.h> 47 #include <visp3/core/vpImageConvert.h> 48 #include <visp3/core/vpImagePoint.h> 49 #include <visp3/core/vpImageTools.h> 60 : fern(), homography(), H(), dst_corners(), isCorrect(false), ref_corners(), modelROI(), currentImagePoints(),
61 refImagePoints(), minNbMatching(10)
77 load(_dataFile, _objectName);
105 std::vector<vpImagePoint> ptsx(nbpt);
106 std::vector<vpImagePoint> ptsy(nbpt);
107 for (
unsigned int i = 0; i < nbpt; i++) {
108 ptsx[i] = ptsy[i] = ip[i];
111 for (
unsigned int i = 0; i < nbpt; i++) {
112 for (
unsigned int j = 0; j < nbpt - 1; j++) {
113 if (ptsx[j].get_j() > ptsx[j + 1].get_j()) {
114 double tmp = ptsx[j + 1].
get_j();
115 ptsx[j + 1].set_j(ptsx[j].get_j());
120 for (
unsigned int i = 0; i < nbpt; i++) {
121 for (
unsigned int j = 0; j < nbpt - 1; j++) {
122 if (ptsy[j].get_i() > ptsy[j + 1].get_i()) {
123 double tmp = ptsy[j + 1].get_i();
124 ptsy[j + 1].set_i(ptsy[j].get_i());
167 unsigned int _height,
unsigned int _width)
224 for (
unsigned int i = 0; i < refPts.size(); ++i) {
228 for (
unsigned int i = 0; i < curPts.size(); ++i) {
233 if (curPts.size() < 4) {
234 for (
unsigned int i = 0; i < 3; i += 1) {
235 for (
unsigned int j = 0; j < 3; j += 1) {
247 std::vector<unsigned char> mask;
248 H = cv::findHomography(cv::Mat(refPts), cv::Mat(curPts), mask, cv::RANSAC, 10);
251 const cv::Mat_<double> &H_tmp =
H;
253 for (
unsigned int i = 0; i < 4; i++) {
256 double w = 1. / (H_tmp(2, 0) * pt.x + H_tmp(2, 1) * pt.y + H_tmp(2, 2));
257 dst_corners[i] = cv::Point2f((
float)((H_tmp(0, 0) * pt.x + H_tmp(0, 1) * pt.y + H_tmp(0, 2)) * w),
258 (
float)((H_tmp(1, 0) * pt.x + H_tmp(1, 1) * pt.y + H_tmp(1, 2)) * w));
261 double *ptr = (
double *)H_tmp.data;
262 for (
unsigned int i = 0; i < 9; i++) {
263 this->
homography[(
unsigned int)(i / 3)][i % 3] = *(ptr++);
272 for (
unsigned int i = 0; i < mask.size(); i += 1) {
275 ip.
set_i(curPts[i].y);
276 ip.
set_j(curPts[i].x);
278 ip.
set_i(refPts[i].y);
279 ip.
set_j(refPts[i].x);
305 const unsigned int height,
const unsigned int width)
308 vpTRACE(
"Bad size for the subimage");
350 for (
unsigned int i = 0; i <
dst_corners.size(); i++) {
384 display(Icurrent, displayKpts);
430 std::vector<vpImagePoint> corners;
432 for (
unsigned int i = 0; i <
dst_corners.size(); i++) {
434 corners.push_back(ip);
449 ip.y = (float)_modelROI.y;
450 ip.x = (
float)_modelROI.x;
453 ip.y = (float)(_modelROI.y + _modelROI.height);
454 ip.x = (float)_modelROI.x;
457 ip.y = (float)(_modelROI.y + _modelROI.height);
458 ip.x = (float)(_modelROI.x + _modelROI.width);
461 ip.y = (float)_modelROI.y;
462 ip.x = (
float)(_modelROI.x + _modelROI.width);
486 #elif !defined(VISP_BUILD_SHARED_LIBS) 489 void dummy_vpPlanarObjectDetector(){};
void initialiseRefCorners(const cv::Rect &_modelROI)
virtual unsigned int matchPoint(const vpImage< unsigned char > &I)
void computeRoi(vpImagePoint *ip, const unsigned int nbpt)
void getMatchedPoints(const unsigned int _index, vpImagePoint &_referencePoint, vpImagePoint &_currentPoint)
std::vector< cv::Point2f > ref_corners
The corners in the reference image.
void load(const std::string &dataFilename, const std::string &objName)
Load the Fern classifier.
error that can be emited by ViSP classes.
void getReferencePoint(const unsigned int _i, vpImagePoint &_imPoint)
cv::Rect modelROI
The ROI for the reference image.
static const vpColor green
vpImagePoint getTopLeft() const
cv::Mat H
Computed homography in the OpenCV format.
bool matchPoint(const vpImage< unsigned char > &I)
virtual unsigned int buildReference(const vpImage< unsigned char > &I)
std::vector< cv::Point2f > dst_corners
void set_i(const double ii)
void display(vpImage< unsigned char > &I, bool displayKpts=false)
std::vector< vpImagePoint > currentImagePoints
vpHomography homography
Computed homography in the ViSP format.
const std::vector< cv::Point2f > & getRefPt() const
cv::Rect getModelROI() const
unsigned int buildReference(const vpImage< unsigned char > &I)
void record(const std::string &_objectName, const std::string &_dataFile)
record the Ferns classifier in the text file
bool isCorrect
Flag to indicate wether the last computed homography is correct or not.
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)
unsigned int getHeight() const
void recordDetector(const std::string &objectName, const std::string &dataFile)
Record the Ferns classifier in the text file.
unsigned int minNbMatching
void set_uv(const double u, const double v)
Defines a rectangle in the plane.
std::vector< vpImagePoint > getDetectedCorners() const
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)
const std::vector< cv::Point2f > & getCurPt() const
void load(const std::string &_dataFile, const std::string &)
load the Fern classifier
std::vector< vpImagePoint > refImagePoints
unsigned int getWidth() const
virtual ~vpPlanarObjectDetector()