Rev 3 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3 | Rev 5 | ||
---|---|---|---|
1 | /*------------------------------------------------------------------------------------------*\
|
1 | /*------------------------------------------------------------------------------------------*\
|
2 | This file contains material supporting chapter 8 of the cookbook:
|
2 | This file contains material supporting chapter 8 of the cookbook:
|
3 | Computer Vision Programming using the OpenCV Library.
|
3 | Computer Vision Programming using the OpenCV Library.
|
4 | by Robert Laganiere, Packt Publishing, 2011.
|
4 | by Robert Laganiere, Packt Publishing, 2011.
|
5 | 5 | ||
6 | This program is free software; permission is hereby granted to use, copy, modify,
|
6 | This program is free software; permission is hereby granted to use, copy, modify,
|
7 | and distribute this source code, or portions thereof, for any purpose, without fee,
|
7 | and distribute this source code, or portions thereof, for any purpose, without fee,
|
8 | subject to the restriction that the copyright notice may not be removed
|
8 | subject to the restriction that the copyright notice may not be removed
|
9 | or altered from any source or altered source distribution.
|
9 | or altered from any source or altered source distribution.
|
10 | The software is released on an as-is basis and without any warranties of any kind.
|
10 | The software is released on an as-is basis and without any warranties of any kind.
|
11 | In particular, the software is not guaranteed to be fault-tolerant or free from failure.
|
11 | In particular, the software is not guaranteed to be fault-tolerant or free from failure.
|
12 | The author disclaims all warranties with regard to this software, any use,
|
12 | The author disclaims all warranties with regard to this software, any use,
|
13 | and any consequent failure, is purely the responsibility of the user.
|
13 | and any consequent failure, is purely the responsibility of the user.
|
14 |
|
14 |
|
15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
|
15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
|
16 | \*------------------------------------------------------------------------------------------*/
|
16 | \*------------------------------------------------------------------------------------------*/
|
17 | 17 | ||
18 | #include <iostream>
|
18 | #include <iostream>
|
19 | #include <vector>
|
19 | #include <vector>
|
20 | #include <opencv2/core/core.hpp>
|
20 | #include <opencv2/core/core.hpp>
|
21 | #include <opencv2/imgproc/imgproc.hpp>
|
21 | #include <opencv2/imgproc/imgproc.hpp>
|
22 | #include <opencv2/highgui/highgui.hpp>
|
22 | #include <opencv2/highgui/highgui.hpp>
|
23 | #include <opencv2/features2d/features2d.hpp>
|
23 | #include <opencv2/features2d/features2d.hpp>
|
24 | 24 | ||
25 | int main() |
25 | int main() |
26 | {
|
26 | {
|
27 | // Read input images
|
27 | // Read input images
|
28 | cv::Mat image1= cv::imread("../church01.jpg",0); |
28 | cv::Mat image1= cv::imread("../church01.jpg",0); |
29 | cv::Mat image2= cv::imread("../church02.jpg",0); |
29 | cv::Mat image2= cv::imread("../church02.jpg",0); |
30 | if (!image1.data || !image2.data) |
30 | if (!image1.data || !image2.data) |
31 | return 0; |
31 | return 0; |
32 | 32 | ||
33 | // Display the images
|
33 | // Display the images
|
34 | cv::namedWindow("Right Image"); |
34 | cv::namedWindow("Right Image"); |
35 | cv::imshow("Right Image",image1); |
35 | cv::imshow("Right Image",image1); |
36 | cv::namedWindow("Left Image"); |
36 | cv::namedWindow("Left Image"); |
37 | cv::imshow("Left Image",image2); |
37 | cv::imshow("Left Image",image2); |
38 | 38 | ||
39 | // vector of keypoints
|
39 | // vector of keypoints
|
40 | std::vector<cv::KeyPoint> keypoints1; |
40 | std::vector<cv::KeyPoint> keypoints1; |
41 | std::vector<cv::KeyPoint> keypoints2; |
41 | std::vector<cv::KeyPoint> keypoints2; |
42 | 42 | ||
43 | // Construction of the SURF feature detector
|
43 | // Construction of the SURF feature detector
|
44 | cv::SurfFeatureDetector surf(3000); |
44 | cv::SurfFeatureDetector surf(3000); |
45 | 45 | ||
46 | // Detection of the SURF features
|
46 | // Detection of the SURF features
|
47 | surf.detect(image1,keypoints1); |
47 | surf.detect(image1,keypoints1); |
48 | surf.detect(image2,keypoints2); |
48 | surf.detect(image2,keypoints2); |
49 | 49 | ||
50 | std::cout << "Number of SURF points (1): " << keypoints1.size() << std::endl; |
50 | std::cout << "Number of SURF points (1): " << keypoints1.size() << std::endl; |
51 | std::cout << "Number of SURF points (2): " << keypoints2.size() << std::endl; |
51 | std::cout << "Number of SURF points (2): " << keypoints2.size() << std::endl; |
52 | 52 | ||
53 | // Draw the kepoints
|
53 | // Draw the kepoints
|
54 | cv::Mat imageKP; |
54 | cv::Mat imageKP; |
55 | cv::drawKeypoints(image1,keypoints1,imageKP,cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS); |
55 | cv::drawKeypoints(image1,keypoints1,imageKP,cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS); |
56 | cv::namedWindow("Right SURF Features"); |
56 | cv::namedWindow("Right SURF Features"); |
57 | cv::imshow("Right SURF Features",imageKP); |
57 | cv::imshow("Right SURF Features",imageKP); |
58 | cv::drawKeypoints(image2,keypoints2,imageKP,cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS); |
58 | cv::drawKeypoints(image2,keypoints2,imageKP,cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS); |
59 | cv::namedWindow("Left SURF Features"); |
59 | cv::namedWindow("Left SURF Features"); |
60 | cv::imshow("Left SURF Features",imageKP); |
60 | cv::imshow("Left SURF Features",imageKP); |
61 | 61 | ||
62 | // Construction of the SURF descriptor extractor
|
62 | // Construction of the SURF descriptor extractor
|
63 | cv::SurfDescriptorExtractor surfDesc; |
63 | cv::SurfDescriptorExtractor surfDesc; |
64 | 64 | ||
65 | // Extraction of the SURF descriptors
|
65 | // Extraction of the SURF descriptors
|
66 | cv::Mat descriptors1, descriptors2; |
66 | cv::Mat descriptors1, descriptors2; |
67 | surfDesc.compute(image1,keypoints1,descriptors1); |
67 | surfDesc.compute(image1,keypoints1,descriptors1); |
68 | surfDesc.compute(image2,keypoints2,descriptors2); |
68 | surfDesc.compute(image2,keypoints2,descriptors2); |
69 | 69 | ||
70 | std::cout << "descriptor matrix size: " << descriptors1.rows << " by " << descriptors1.cols << std::endl; |
70 | std::cout << "descriptor matrix size: " << descriptors1.rows << " by " << descriptors1.cols << std::endl; |
71 | 71 | ||
72 | // Construction of the matcher
|
72 | // Construction of the matcher
|
73 | cv::BruteForceMatcher<cv::L2<float>> matcher; |
73 | cv::BruteForceMatcher<cv::L2<float>> matcher; |
74 | 74 | ||
75 | // Match the two image descriptors
|
75 | // Match the two image descriptors
|
76 | std::vector<cv::DMatch> matches; |
76 | std::vector<cv::DMatch> matches; |
77 | matcher.match(descriptors1,descriptors2, matches); |
77 | matcher.match(descriptors1,descriptors2, matches); |
78 | 78 | ||
79 | std::cout << "Number of matched points: " << matches.size() << std::endl; |
79 | std::cout << "Number of matched points: " << matches.size() << std::endl; |
80 | 80 | ||
81 | std::nth_element(matches.begin(), // initial position |
81 | std::nth_element(matches.begin(), // initial position |
82 | matches.begin()+24, // position of the sorted element |
82 | matches.begin()+24, // position of the sorted element |
83 | matches.end()); // end position |
83 | matches.end()); // end position |
84 | // remove all elements after the 25th
|
84 | // remove all elements after the 25th
|
85 | matches.erase(matches.begin()+25, matches.end()); |
85 | matches.erase(matches.begin()+25, matches.end()); |
86 | 86 | ||
87 | cv::Mat imageMatches; |
87 | cv::Mat imageMatches; |
88 | cv::drawMatches(image1,keypoints1, // 1st image and its keypoints |
88 | cv::drawMatches(image1,keypoints1, // 1st image and its keypoints |
89 | image2,keypoints2, // 2nd image and its keypoints
|
89 | image2,keypoints2, // 2nd image and its keypoints
|
90 | matches, // the matches
|
90 | matches, // the matches
|
91 | imageMatches, // the image produced
|
91 | imageMatches, // the image produced
|
92 | cv::Scalar(255,255,255)); // color of the lines |
92 | cv::Scalar(255,255,255)); // color of the lines |
93 | cv::namedWindow("Matches"); |
93 | cv::namedWindow("Matches"); |
94 | cv::imshow("Matches",imageMatches); |
94 | cv::imshow("Matches",imageMatches); |
95 | 95 | ||
96 | cv::waitKey(); |
96 | cv::waitKey(); |
97 | return 0; |
97 | return 0; |
98 | 98 | ||
99 | int size=7; |
99 | int size=7; |
100 | cv::Mat imaf1; |
100 | cv::Mat imaf1; |
101 | image1.convertTo(imaf1,CV_32F); |
101 | image1.convertTo(imaf1,CV_32F); |
102 | 102 | ||
103 | cv::Mat imaf2; |
103 | cv::Mat imaf2; |
104 | image2.convertTo(imaf2,CV_32F); |
104 | image2.convertTo(imaf2,CV_32F); |
105 | 105 | ||
106 | cv::waitKey(); |
106 | cv::waitKey(); |
107 | return 0; |
107 | return 0; |
108 | }
|
- | |
109 | 108 | }
|
|
- | 109 |