Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | PointedEar | 1 | /*------------------------------------------------------------------------------------------*\ |
2 | This file contains material supporting chapter 9 of the cookbook: |
||
3 | Computer Vision Programming using the OpenCV Library. |
||
4 | by Robert Laganiere, Packt Publishing, 2011. |
||
5 | |||
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, |
||
8 | subject to the restriction that the copyright notice may not be removed |
||
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. |
||
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, |
||
13 | and any consequent failure, is purely the responsibility of the user. |
||
14 | |||
15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name |
||
16 | \*------------------------------------------------------------------------------------------*/ |
||
17 | |||
18 | #include <iostream> |
||
19 | #include <vector> |
||
20 | #include <opencv2/core/core.hpp> |
||
21 | #include <opencv2/imgproc/imgproc.hpp> |
||
22 | #include <opencv2/highgui/highgui.hpp> |
||
23 | #include <opencv2/features2d/features2d.hpp> |
||
24 | #include <opencv2/calib3d/calib3d.hpp> |
||
25 | #include "matcher.h" |
||
26 | |||
27 | int main() |
||
28 | { |
||
29 | // Read input images |
||
30 | cv::Mat image1= cv::imread("../canal1.jpg",0); |
||
31 | cv::Mat image2= cv::imread("../canal2.jpg",0); |
||
32 | if (!image1.data || !image2.data) |
||
33 | return 0; |
||
34 | |||
35 | // Display the images |
||
36 | cv::namedWindow("Right Image"); |
||
37 | cv::imshow("Right Image",image1); |
||
38 | cv::namedWindow("Left Image"); |
||
39 | cv::imshow("Left Image",image2); |
||
40 | |||
41 | // Prepare the matcher |
||
42 | RobustMatcher rmatcher; |
||
43 | rmatcher.setConfidenceLevel(0.98); |
||
44 | rmatcher.setMinDistanceToEpipolar(1.0); |
||
45 | rmatcher.setRatio(0.65f); |
||
46 | cv::Ptr<cv::FeatureDetector> pfd= new cv::SurfFeatureDetector(10); |
||
47 | rmatcher.setFeatureDetector(pfd); |
||
48 | |||
49 | // Match the two images |
||
50 | std::vector<cv::DMatch> matches; |
||
51 | std::vector<cv::KeyPoint> keypoints1, keypoints2; |
||
52 | cv::Mat fundemental= rmatcher.match(image1,image2,matches, keypoints1, keypoints2); |
||
53 | |||
54 | // draw the matches |
||
55 | cv::Mat imageMatches; |
||
56 | cv::drawMatches(image1,keypoints1, // 1st image and its keypoints |
||
57 | image2,keypoints2, // 2nd image and its keypoints |
||
58 | matches, // the matches |
||
59 | imageMatches, // the image produced |
||
60 | cv::Scalar(255,255,255)); // color of the lines |
||
61 | cv::namedWindow("Matches"); |
||
62 | cv::imshow("Matches",imageMatches); |
||
63 | |||
64 | // Convert keypoints into Point2f |
||
65 | std::vector<cv::Point2f> points1, points2; |
||
66 | |||
67 | for (std::vector<cv::DMatch>::const_iterator it= matches.begin(); |
||
68 | it!= matches.end(); ++it) { |
||
69 | |||
70 | // Get the position of left keypoints |
||
71 | float x= keypoints1[it->queryIdx].pt.x; |
||
72 | float y= keypoints1[it->queryIdx].pt.y; |
||
73 | points1.push_back(cv::Point2f(x,y)); |
||
74 | cv::circle(image1,cv::Point(x,y),3,cv::Scalar(255,255,255),3); |
||
75 | // Get the position of right keypoints |
||
76 | x= keypoints2[it->trainIdx].pt.x; |
||
77 | y= keypoints2[it->trainIdx].pt.y; |
||
78 | cv::circle(image2,cv::Point(x,y),3,cv::Scalar(255,255,255),3); |
||
79 | points2.push_back(cv::Point2f(x,y)); |
||
80 | } |
||
81 | |||
82 | // Draw the epipolar lines |
||
83 | std::vector<cv::Vec3f> lines1; |
||
84 | cv::computeCorrespondEpilines(cv::Mat(points1),1,fundemental,lines1); |
||
85 | |||
86 | for (vector<cv::Vec3f>::const_iterator it= lines1.begin(); |
||
87 | it!=lines1.end(); ++it) { |
||
88 | |||
89 | cv::line(image2,cv::Point(0,-(*it)[2]/(*it)[1]), |
||
90 | cv::Point(image2.cols,-((*it)[2]+(*it)[0]*image2.cols)/(*it)[1]), |
||
91 | cv::Scalar(255,255,255)); |
||
92 | } |
||
93 | |||
94 | std::vector<cv::Vec3f> lines2; |
||
95 | cv::computeCorrespondEpilines(cv::Mat(points2),2,fundemental,lines2); |
||
96 | |||
97 | for (vector<cv::Vec3f>::const_iterator it= lines2.begin(); |
||
98 | it!=lines2.end(); ++it) { |
||
99 | |||
100 | cv::line(image1,cv::Point(0,-(*it)[2]/(*it)[1]), |
||
101 | cv::Point(image1.cols,-((*it)[2]+(*it)[0]*image1.cols)/(*it)[1]), |
||
102 | cv::Scalar(255,255,255)); |
||
103 | } |
||
104 | |||
105 | // Display the images with epipolar lines |
||
106 | cv::namedWindow("Right Image Epilines (RANSAC)"); |
||
107 | cv::imshow("Right Image Epilines (RANSAC)",image1); |
||
108 | cv::namedWindow("Left Image Epilines (RANSAC)"); |
||
109 | cv::imshow("Left Image Epilines (RANSAC)",image2); |
||
110 | |||
111 | cv::waitKey(); |
||
112 | return 0; |
||
113 | } |