38 #include <visp3/vision/vpPlanarObjectDetector.h>
40 #if (VISP_HAVE_OPENCV_VERSION >= 0x020000) && (VISP_HAVE_OPENCV_VERSION < 0x030000) // Require opencv >= 2.0.0 and < 3.0.0
42 #include <visp3/core/vpImageConvert.h>
43 #include <visp3/core/vpException.h>
44 #include <visp3/core/vpImagePoint.h>
45 #include <visp3/core/vpDisplay.h>
46 #include <visp3/core/vpColor.h>
47 #include <visp3/core/vpImageTools.h>
58 : fern(), homography(), H(), dst_corners(), isCorrect(false), ref_corners(), modelROI(), currentImagePoints(),
59 refImagePoints(), minNbMatching(10)
72 : fern(), homography(), H(), dst_corners(), isCorrect(false), ref_corners(), modelROI(), currentImagePoints(),
73 refImagePoints(), minNbMatching(10)
75 load(_dataFile, _objectName);
110 std::vector < vpImagePoint > ptsx(nbpt);
111 std::vector < vpImagePoint > ptsy(nbpt);
112 for(
unsigned int i=0; i<nbpt; i++){
113 ptsx[i] = ptsy[i] = ip[i];
116 for(
unsigned int i=0; i<nbpt; i++){
117 for(
unsigned int j=0; j<nbpt-1; j++){
118 if(ptsx[j].get_j() > ptsx[j+1].get_j()){
119 double tmp = ptsx[j+1].
get_j();
120 ptsx[j+1].set_j(ptsx[j].get_j());
125 for(
unsigned int i=0; i<nbpt; i++){
126 for(
unsigned int j=0; j<nbpt-1; j++){
127 if(ptsy[j].get_i() > ptsy[j+1].get_i()){
128 double tmp = ptsy[j+1].get_i();
129 ptsy[j+1].set_i(ptsy[j].get_i());
178 unsigned int _height,
unsigned int _width)
241 for(
unsigned int i=0; i<refPts.size(); ++i){
245 for(
unsigned int i=0; i<curPts.size(); ++i){
250 if(curPts.size() < 4){
251 for (
unsigned int i = 0; i < 3; i += 1){
252 for (
unsigned int j = 0; j < 3; j += 1){
265 std::vector<unsigned char> mask;
266 H = cv::findHomography(cv::Mat(refPts), cv::Mat(curPts), mask, cv::RANSAC, 10);
270 const cv::Mat_<double>& H_tmp =
H;
272 for(
unsigned int i = 0; i < 4; i++ )
276 double w = 1./(H_tmp(2,0)*pt.x + H_tmp(2,1)*pt.y + H_tmp(2,2));
277 dst_corners[i] = cv::Point2f((
float)((H_tmp(0,0)*pt.x + H_tmp(0,1)*pt.y + H_tmp(0,2))*w),
278 (
float)((H_tmp(1,0)*pt.x + H_tmp(1,1)*pt.y + H_tmp(1,2))*w));
281 double* ptr = (
double*)H_tmp.data;
282 for(
unsigned int i=0; i<9; i++){
283 this->
homography[(
unsigned int)(i/3)][i%3] = *(ptr++);
294 for (
unsigned int i = 0; i < mask.size(); i += 1){
297 ip.
set_i(curPts[i].y);
298 ip.
set_j(curPts[i].x);
300 ip.
set_i(refPts[i].y);
301 ip.
set_j(refPts[i].x);
330 const vpImagePoint &iP,
const unsigned int height,
const unsigned int width)
334 vpTRACE(
"Bad size for the subimage");
336 "Bad size for the subimage"));
342 (
unsigned int)iP.
get_i(),
343 (
unsigned int)iP.
get_j(),
344 height, width, subImage);
368 (
unsigned int)rectangle.
getWidth()));
425 display(Icurrent, displayKpts);
474 std::vector<vpImagePoint>
477 std::vector <vpImagePoint> corners;
481 corners.push_back(ip);
497 ip.y = (float)_modelROI.y;
498 ip.x = (
float)_modelROI.x;
501 ip.y = (float)(_modelROI.y+_modelROI.height);
502 ip.x = (float)_modelROI.x;
505 ip.y = (float)(_modelROI.y+_modelROI.height);
506 ip.x = (float)(_modelROI.x+_modelROI.width);
509 ip.y = (float)_modelROI.y;
510 ip.x = (
float)(_modelROI.x+_modelROI.width);
536 #elif !defined(VISP_BUILD_SHARED_LIBS)
538 void dummy_vpPlanarObjectDetector() {};
void initialiseRefCorners(const cv::Rect &_modelROI)
unsigned int getWidth() const
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.
const std::vector< cv::Point2f > & getRefPt() const
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
The estimated new coordinates of the corners (reprojected using the homography).
void set_i(const double ii)
void display(vpImage< unsigned char > &I, bool displayKpts=false)
vpFernClassifier fern
Fern Classifier used to match the points between a reference image and the current image...
std::vector< vpImagePoint > currentImagePoints
Vector of the image point in the current image that match after the deletion of the outliers with the...
vpHomography homography
Computed homography in the ViSP format.
unsigned int buildReference(const vpImage< unsigned char > &I)
const std::vector< cv::Point2f > & getCurPt() const
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)
cv::Rect getModelROI() const
void recordDetector(const std::string &objectName, const std::string &dataFile)
Record the Ferns classifier in the text file.
unsigned int minNbMatching
Minimal number of point to after the ransac needed to suppose that the homography has been correctly ...
unsigned int getHeight() const
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)
void load(const std::string &_dataFile, const std::string &)
load the Fern classifier
std::vector< vpImagePoint > refImagePoints
Vector of the image point in the reference image that match after the deletion of the outliers with t...
virtual ~vpPlanarObjectDetector()