42 #include <visp/vpPlanarObjectDetector.h>
44 #if (VISP_HAVE_OPENCV_VERSION >= 0x020000) // Require opencv >= 2.0.0
46 #include <visp/vpImageConvert.h>
47 #include <visp/vpException.h>
48 #include <visp/vpImagePoint.h>
49 #include <visp/vpDisplay.h>
50 #include <visp/vpDisplayX.h>
51 #include <visp/vpColor.h>
52 #include <visp/vpImageTools.h>
63 : fern(), homography(), H(), dst_corners(), isCorrect(false), ref_corners(), modelROI(), currentImagePoints(),
64 refImagePoints(), minNbMatching(10)
77 : fern(), homography(), H(), dst_corners(), isCorrect(false), ref_corners(), modelROI(), currentImagePoints(),
78 refImagePoints(), minNbMatching(10)
80 load(_dataFile, _objectName);
115 std::vector < vpImagePoint > ptsx(nbpt);
116 std::vector < vpImagePoint > ptsy(nbpt);
117 for(
unsigned int i=0; i<nbpt; i++){
118 ptsx[i] = ptsy[i] = ip[i];
121 for(
unsigned int i=0; i<nbpt; i++){
122 for(
unsigned int j=0; j<nbpt-1; j++){
123 if(ptsx[j].get_j() > ptsx[j+1].get_j()){
124 double tmp = ptsx[j+1].
get_j();
125 ptsx[j+1].set_j(ptsx[j].get_j());
130 for(
unsigned int i=0; i<nbpt; i++){
131 for(
unsigned int j=0; j<nbpt-1; j++){
132 if(ptsy[j].get_i() > ptsy[j+1].get_i()){
133 double tmp = ptsy[j+1].get_i();
134 ptsy[j+1].set_i(ptsy[j].get_i());
183 unsigned int _height,
unsigned int _width)
246 for(
unsigned int i=0; i<refPts.size(); ++i){
250 for(
unsigned int i=0; i<curPts.size(); ++i){
255 if(curPts.size() < 4){
256 for (
unsigned int i = 0; i < 3; i += 1){
257 for (
unsigned int j = 0; j < 3; j += 1){
270 std::vector<unsigned char> mask;
271 H = cv::findHomography(cv::Mat(refPts), cv::Mat(curPts), mask, cv::RANSAC, 10);
275 const cv::Mat_<double>& H_tmp =
H;
277 for(
unsigned int i = 0; i < 4; i++ )
281 double w = 1./(H_tmp(2,0)*pt.x + H_tmp(2,1)*pt.y + H_tmp(2,2));
282 dst_corners[i] = cv::Point2f((
float)((H_tmp(0,0)*pt.x + H_tmp(0,1)*pt.y + H_tmp(0,2))*w),
283 (
float)((H_tmp(1,0)*pt.x + H_tmp(1,1)*pt.y + H_tmp(1,2))*w));
286 double* ptr = (
double*)H_tmp.data;
287 for(
unsigned int i=0; i<9; i++){
288 this->
homography[(
unsigned int)(i/3)][i%3] = *(ptr++);
299 for (
unsigned int i = 0; i < mask.size(); i += 1){
302 ip.
set_i(curPts[i].y);
303 ip.
set_j(curPts[i].x);
305 ip.
set_i(refPts[i].y);
306 ip.
set_j(refPts[i].x);
335 const vpImagePoint &iP,
const unsigned int height,
const unsigned int width)
339 vpTRACE(
"Bad size for the subimage");
341 "Bad size for the subimage"));
347 (
unsigned int)iP.
get_i(),
348 (
unsigned int)iP.
get_j(),
349 height, width, subImage);
373 (
unsigned int)rectangle.
getWidth()));
430 display(Icurrent, displayKpts);
479 std::vector<vpImagePoint>
482 std::vector <vpImagePoint> corners;
486 corners.push_back(ip);
502 ip.y = (float)_modelROI.y;
503 ip.x = (
float)_modelROI.x;
506 ip.y = (float)(_modelROI.y+_modelROI.height);
507 ip.x = (float)_modelROI.x;
510 ip.y = (float)(_modelROI.y+_modelROI.height);
511 ip.x = (float)(_modelROI.x+_modelROI.width);
514 ip.y = (float)_modelROI.y;
515 ip.x = (
float)(_modelROI.x+_modelROI.width);
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.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
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)
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 ...
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
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()