Subversion Repositories OpenCV2-Cookbook

Rev

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("../parliament1.bmp",0);
31
        cv::Mat image2= cv::imread("../parliament2.bmp",0);
32
        if (!image1.data || !image2.data)
33
                return 0;
34
 
35
    // Display the images
36
        cv::namedWindow("Image 1");
37
        cv::imshow("Image 1",image1);
38
        cv::namedWindow("Image 2");
39
        cv::imshow("Image 2",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
        for (std::vector<cv::DMatch>::const_iterator it= matches.begin();
67
                 it!= matches.end(); ++it) {
68
 
69
                         // Get the position of left keypoints
70
                         float x= keypoints1[it->queryIdx].pt.x;
71
                         float y= keypoints1[it->queryIdx].pt.y;
72
                         points1.push_back(cv::Point2f(x,y));
73
                         // Get the position of right keypoints
74
                         x= keypoints2[it->trainIdx].pt.x;
75
                         y= keypoints2[it->trainIdx].pt.y;
76
                         points2.push_back(cv::Point2f(x,y));
77
        }
78
 
79
        std::cout << points1.size() << " " << points2.size() << std::endl;
80
 
81
        // Find the homography between image 1 and image 2
82
        std::vector<uchar> inliers(points1.size(),0);
83
        cv::Mat homography= cv::findHomography(
84
                cv::Mat(points1),cv::Mat(points2), // corresponding points
85
                inliers,        // outputed inliers matches 
86
                CV_RANSAC,      // RANSAC method
87
                1.);        // max distance to reprojection point
88
 
89
        // Draw the inlier points
90
        std::vector<cv::Point2f>::const_iterator itPts= points1.begin();
91
        std::vector<uchar>::const_iterator itIn= inliers.begin();
92
        while (itPts!=points1.end()) {
93
 
94
                // draw a circle at each inlier location
95
                if (*itIn)
96
                        cv::circle(image1,*itPts,3,cv::Scalar(255,255,255),2);
97
 
98
                ++itPts;
99
                ++itIn;
100
        }
101
 
102
        itPts= points2.begin();
103
        itIn= inliers.begin();
104
        while (itPts!=points2.end()) {
105
 
106
                // draw a circle at each inlier location
107
                if (*itIn)
108
                        cv::circle(image2,*itPts,3,cv::Scalar(255,255,255),2);
109
 
110
                ++itPts;
111
                ++itIn;
112
        }
113
 
114
    // Display the images with points
115
        cv::namedWindow("Image 1 Homography Points");
116
        cv::imshow("Image 1 Homography Points",image1);
117
        cv::namedWindow("Image 2 Homography Points");
118
        cv::imshow("Image 2 Homography Points",image2);
119
 
120
        // Warp image 1 to image 2
121
        cv::Mat result;
122
        cv::warpPerspective(image1, // input image
123
                result,                 // output image
124
                homography,             // homography
125
                cv::Size(2*image1.cols,image1.rows)); // size of output image
126
 
127
        // Copy image 1 on the first half of full image
128
        cv::Mat half(result,cv::Rect(0,0,image2.cols,image2.rows));
129
        image2.copyTo(half);
130
 
131
    // Display the warp image
132
        cv::namedWindow("After warping");
133
        cv::imshow("After warping",result);
134
 
135
        cv::waitKey();
136
        return 0;
137
}