26 #include <mrpt/examples_config.h> 28 MRPT_EXAMPLES_BASE_DIRECTORY +
29 string(
"vision_keypoint_matching_example/imgs/"));
47 cerr <<
"Cannot load " << imgL << endl;
50 cout <<
"Loaded test image: " << imgL << endl;
54 cerr <<
"Cannot load " << imgR << endl;
57 cout <<
"Loaded test image: " << imgR << endl;
59 cout <<
"***************************************************" << endl;
60 cout <<
"***************************************************" << endl;
64 cout <<
"Detecting HARRIS features in LEFT image" << endl;
67 cout <<
"Detected " << featsHarris_L.
size() << endl;
69 cout <<
"Detecting HARRIS features in RIGHT image" << endl;
71 cout <<
"Detected " << featsHarris_R.
size() << endl;
73 cout <<
"***************************************************" << endl;
74 cout <<
"***************************************************" << endl;
79 cout <<
"Matching HARRIS features by CORRELATION" << endl;
82 cout <<
"Matches found: " << mHarris.size() << endl;
84 cout <<
"***************************************************" << endl;
89 cout <<
"Projecting matched features" << endl;
97 mrpt::make_aligned_shared<CGridPlaneXY>(-10, 10, -10, 10, 0, 1);
99 scene3D->insert(gridXY);
100 scene3D->insert(map3D);
102 win3D.unlockAccess3DScene();
115 CFeatureList featsHarris_L, featsHarris_R, featsSIFT_L, featsSIFT_R,
116 featsSURF_L, featsSURF_R, featsFAST_L, featsFAST_R;
126 cerr <<
"Cannot load " << imgL << endl;
129 cout <<
"Loaded LEFT image: " << endl << imgL << endl;
133 cerr <<
"Cannot load " << imgR << endl;
136 cout <<
"Loaded RIGHT image: " << endl << imgR << endl;
138 cout <<
"***************************************************" << endl;
139 cout <<
"***************************************************" << endl;
143 cout <<
"Detecting HARRIS features in LEFT image" << endl;
146 cout <<
"Detected " << featsHarris_L.
size() << endl;
148 cout <<
"Detecting HARRIS features in RIGHT image" << endl;
150 cout <<
"Detected " << featsHarris_R.
size() << endl;
151 cout <<
"***************************************************" << endl;
154 cout <<
"Detecting SIFT features in LEFT image" << endl;
159 cout <<
"Detected " << featsSIFT_L.
size() << endl;
161 cout <<
"Detecting SIFT features in RIGHT image" << endl;
166 cout <<
"Detected " << featsSIFT_R.
size() << endl;
167 cout <<
"***************************************************" << endl;
170 cout <<
"Detecting SURF features in LEFT image" << endl;
173 cout <<
"Detected " << featsSURF_L.
size() << endl;
175 cout <<
"Detecting SURF features in RIGHT image" << endl;
177 cout <<
"Detected " << featsSURF_R.
size() << endl;
178 cout <<
"***************************************************" << endl;
181 cout <<
"Detecting FAST features in LEFT image" << endl;
184 cout <<
"Detected " << featsFAST_L.
size() << endl;
186 cout <<
"Detecting FAST features in RIGHT image" << endl;
188 cout <<
"Detected " << featsFAST_R.
size() << endl;
189 cout <<
"***************************************************" << endl;
190 cout <<
"***************************************************" << endl;
199 cout <<
"Matching HARRIS features" << endl;
201 nMatches =
matchFeatures(featsHarris_L, featsHarris_R, mHarris);
203 cout <<
"[NCC] Matches found: " << mHarris.size() <<
" in " << T * 1000.0f
208 nMatches =
matchFeatures(featsHarris_L, featsHarris_R, mHarris_SAD, opt);
210 cout <<
"[SAD] Matches found: " << mHarris_SAD.size() <<
" in " 211 << T * 1000.0f <<
" ms " << endl;
212 cout <<
"***************************************************" << endl;
215 cout <<
"Matching SIFT features by DESCRIPTOR" << endl;
218 nMatches =
matchFeatures(featsSIFT_L, featsSIFT_R, mSIFT, opt);
220 cout <<
"Matches found: " << mSIFT.size() <<
" in " << T * 1000.0f <<
" ms " 222 cout <<
"***************************************************" << endl;
225 cout <<
"Matching SURF features by DESCRIPTOR" << endl;
228 nMatches =
matchFeatures(featsSURF_L, featsSURF_R, mSURF, opt);
230 cout <<
"Matches found: " << mSURF.size() <<
" in " << T * 1000.0f <<
" ms " 232 cout <<
"***************************************************" << endl;
235 cout <<
"Matching FAST features" << endl;
237 nMatches =
matchFeatures(featsFAST_L, featsFAST_R, mFAST_CC);
239 cout <<
"[NCC] Matches found: " << mFAST_CC.size() <<
" in " << T * 1000.0f
244 nMatches =
matchFeatures(featsFAST_L, featsFAST_R, mFAST_SAD, opt);
246 cout <<
"[SAD] Matches found: " << mFAST_SAD.size() <<
" in " << T * 1000.0f
248 cout <<
"***************************************************" << endl;
252 CDisplayWindow winHarrisSAD, winHarrisNCC, winFASTSAD, winFASTNCC,
263 imL, imR, mHarris,
TColor(0, 0, 255));
265 imL, imR, mHarris_SAD,
TColor(0, 0, 255));
269 imL, imR, mFAST_SAD,
TColor(0, 255, 0));
271 imL, imR, mFAST_CC,
TColor(0, 255, 0));
312 for (it1 = list1.
begin(); it1 != list1.
end(); ++it1)
314 for (it2 = list2.
begin(); it2 != list2.
end(); ++it2)
319 win2.
setPos(0, imH * 1.5);
320 CImage joinimage, copyjoinimage, copyInfoImage;
327 infoimage.filledRectangle(0, 0, imW2, imH2,
TColor(150, 150, 150));
328 infoimage.textOut(20, imH2 - 53,
"SAD",
TColor::blue());
329 infoimage.textOut(20, imH2 - 41,
"NCC",
TColor::blue());
330 infoimage.textOut(20, imH2 - 29,
"SIFT",
TColor::blue());
331 infoimage.textOut(20, imH2 - 17,
"SURF",
TColor::blue());
332 for (it1 = list1.
begin(); it1 != list1.
end(); ++it1)
334 copyInfoImage = infoimage;
335 copyjoinimage = joinimage;
339 (*it1)->x + imW, 0, (*it1)->x + imW, imH,
347 bool firstMatch =
true;
350 double minsad = 1.0, maxncc = 0.0;
351 float minsiftd = 1.0f, minsurfd = 1.0f;
352 int idxsad = 0, idxncc = 0, idxsiftd = 0, idxsurfd = 0;
354 for (it2 = list2.
begin(); it2 != list2.
end(); ++it2)
356 if (fabs((*it1)->y - (*it2)->y) <= 1.0 && (*it1)->x > (*it2)->x)
372 (*it1)->patch, (*it2)->patch, u,
v, ncc);
380 float siftd = (*it1)->descriptorSIFTDistanceTo(*(*it2));
381 if (siftd < minsiftd)
388 float surfd = (*it1)->descriptorSURFDistanceTo(*(*it2));
389 if (surfd < minsurfd)
399 (*it1)->x + imW, 0, (*it1)->x + imW, imH,
407 double rx0, rx1, ry0, ry1, tx,
ty;
408 rx0 = (*it2)->x + imW - 15;
409 rx1 = (*it2)->x + imW;
410 tx = (*it2)->x + imW - 13;
413 ry0 = (*it2)->y - 20;
414 ry1 = (*it2)->y - 10;
419 ry0 = (*it2)->y + 10;
420 ry1 = (*it2)->y + 20;
424 rx0, ry0, rx1, ry1,
TColor(150, 150, 150));
448 80 + idxsad * 50, imH2 - 53,
format(
"%.2f", minsad),
451 80 + idxncc * 50, imH2 - 41,
format(
"%.2f", maxncc),
454 80 + idxsiftd * 50, imH2 - 29,
format(
"%.2f", minsiftd),
457 80 + idxsurfd * 50, imH2 - 17,
format(
"%.2f", minsurfd),
460 win.showImage(copyjoinimage);
470 int main(
int argc,
char** argv)
476 cerr <<
"Usage: " << argv[0] << endl;
477 cerr <<
"Options:" << endl;
478 cerr <<
" -match [-s]: TestMatchFeatures (if -s is set, final " 479 "matches in images will be shown)." 481 cerr <<
" -comp: TestMatchingComparative." << endl;
482 cerr <<
" -proj: TestExtractMatchProjectAndPaint." << endl;
486 if (!strcmp(argv[1],
"-match"))
488 if (argc == 3 && !strcmp(argv[2],
"-s"))
493 else if (!strcmp(argv[1],
"-proj"))
495 else if (!strcmp(argv[1],
"-comp"))
497 cout <<
"Press ^C to finish program." << endl;
502 cerr <<
"Usage: " << argv[0] << endl;
503 cerr <<
"Options:" << endl;
504 cerr <<
" -match [-s]: TestMatchFeatures (if -s is set, final " 505 "matches in images will be shown)." 507 cerr <<
" -comp: TestMatchingComparative." << endl;
508 cerr <<
" -proj: TestExtractMatchProjectAndPaint." << endl;
513 catch (std::exception& e)
515 std::cout <<
"MRPT exception caught: " << e.what() << std::endl;
520 printf(
"Another exception!!");
void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &outObj) const override
Returns a 3D object representing the map.
void update_patch(const CImage &patch, const unsigned int col, const unsigned int row)
Update a part of this image with the "patch" given as argument.
double Tac() noexcept
Stops the stopwatch.
void drawCircle(int x, int y, int radius, const mrpt::img::TColor &color=mrpt::img::TColor(255, 255, 255), unsigned int width=1) override
Draws a circle of a given radius.
void line(int x0, int y0, int x1, int y1, const mrpt::img::TColor color, unsigned int width=1, TPenStyle penStyle=psSolid) override
Draws a line.
void TestMatchingComparative()
void cross(int x0, int y0, const mrpt::img::TColor color, char type, unsigned int size=5, unsigned int width=1)
Draw a cross.
TInternalFeatList::iterator iterator
void showImagesAndMatchedPoints(const mrpt::img::CImage &img1, const mrpt::img::CImage &img2, const MATCHEDLIST &mList, const mrpt::img::TColor &color=mrpt::img::TColor::red(), bool showNumbers=false)
Show a pair of given color or grayscale images (put together) on the window and print a set of matche...
A high-performance stopwatch, with typical resolution of nanoseconds.
void TestExtractMatchProjectAndPaint()
size_t getHeight() const override
Returns the height of the image in pixels.
void joinImagesHorz(const CImage &im1, const CImage &im2)
Joins two images side-by-side horizontally.
void setWindowTitle(const std::string &str) override
Changes the window title text.
FAST feature detector, OpenCV's implementation ("Faster and better: A machine learning approach to...
bool loadFromFile(const std::string &fileName, int isColor=-1)
Load image from a file, whose format is determined from the extension (internally uses OpenCV)...
size_t getWidth() const override
Returns the width of the image in pixels.
A class for storing a map of 3D probabilistic landmarks.
Parameters associated to a stereo system.
virtual void filledRectangle(int x0, int y0, int x1, int y1, const mrpt::img::TColor color)
Draws a filled rectangle.
Harris border and corner detector [HARRIS].
Scale Invariant Feature Transform [LOWE'04].
This class creates a window as a graphical user interface (GUI) for displaying images to the user...
Classes for computer vision, detectors, features, etc.
mrpt::gui::CDisplayWindow3D::Ptr win
GLsizei const GLchar ** string
A list of visual features, to be used as output by detectors, as input/output by trackers, etc.
void setPos(int x, int y) override
Changes the position of the window on the screen.
void pause(const std::string &msg=std::string("Press any key to continue...")) noexcept
Shows the message "Press any key to continue" (or other custom message) to the current standard outpu...
Speeded Up Robust Feature [BAY'06].
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
size_t matchFeatures(const CFeatureList &list1, const CFeatureList &list2, CMatchedFeatureList &matches, const TMatchingOptions &options=TMatchingOptions(), const TStereoSystemParams ¶ms=TStereoSystemParams())
Find the matches between two lists of features which must be of the same type.
A structure containing options for the matching.
The namespace for 3D scene representation and rendering.
double computeSAD(const mrpt::img::CImage &patch1, const mrpt::img::CImage &patch2)
Calculates the Sum of Absolutes Differences (range [0,1]) between two patches.
void showImage(const mrpt::img::CImage &img)
Show a given color or grayscale image on the window.
virtual void textOut(int x0, int y0, const std::string &str, const mrpt::img::TColor color)
Renders 2D text using bitmap fonts.
Classes for creating GUI windows for 2D and 3D visualization.
void Tic() noexcept
Starts the stopwatch.
void openCV_cross_correlation(const mrpt::img::CImage &img, const mrpt::img::CImage &patch_img, size_t &x_max, size_t &y_max, double &max_val, int x_search_ini=-1, int y_search_ini=-1, int x_search_size=-1, int y_search_size=-1)
Computes the correlation between this image and another one, encapsulating the openCV function cvMatc...
void projectMatchedFeatures(const CMatchedFeatureList &matches, const mrpt::img::TStereoCamera &stereo_camera, std::vector< mrpt::math::TPoint3D > &out_points)
TMatchingMethod matching_method
Matching method.
Kanade-Lucas-Tomasi feature [SHI'94].
A class for storing images as grayscale or RGB bitmaps.
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.