Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 8 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
 
25
int main()
26
{
27
        // Read input images
28
        cv::Mat image1= cv::imread("../church01.jpg",0);
29
        cv::Mat image2= cv::imread("../church02.jpg",0);
30
        if (!image1.data || !image2.data)
31
                return 0;
32
 
33
    // Display the images
34
        cv::namedWindow("Right Image");
35
        cv::imshow("Right Image",image1);
36
        cv::namedWindow("Left Image");
37
        cv::imshow("Left Image",image2);
38
 
39
        // vector of keypoints
40
        std::vector<cv::KeyPoint> keypoints1;
41
        std::vector<cv::KeyPoint> keypoints2;
42
 
43
        // Construction of the SURF feature detector 
44
        cv::SurfFeatureDetector surf(3000);
45
 
46
        // Detection of the SURF features
47
        surf.detect(image1,keypoints1);
48
        surf.detect(image2,keypoints2);
49
 
50
        std::cout << "Number of SURF points (1): " << keypoints1.size() << std::endl;
51
        std::cout << "Number of SURF points (2): " << keypoints2.size() << std::endl;
52
 
53
        // Draw the kepoints
54
        cv::Mat imageKP;
55
        cv::drawKeypoints(image1,keypoints1,imageKP,cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
56
        cv::namedWindow("Right SURF Features");
57
        cv::imshow("Right SURF Features",imageKP);
58
        cv::drawKeypoints(image2,keypoints2,imageKP,cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
59
        cv::namedWindow("Left SURF Features");
60
        cv::imshow("Left SURF Features",imageKP);
61
 
62
        // Construction of the SURF descriptor extractor 
63
        cv::SurfDescriptorExtractor surfDesc;
64
 
65
        // Extraction of the SURF descriptors
66
        cv::Mat descriptors1, descriptors2;
67
        surfDesc.compute(image1,keypoints1,descriptors1);
68
        surfDesc.compute(image2,keypoints2,descriptors2);
69
 
70
        std::cout << "descriptor matrix size: " << descriptors1.rows << " by " << descriptors1.cols << std::endl;
71
 
72
        // Construction of the matcher 
73
        cv::BruteForceMatcher<cv::L2<float>> matcher;
74
 
75
        // Match the two image descriptors
76
        std::vector<cv::DMatch> matches;
77
        matcher.match(descriptors1,descriptors2, matches);
78
 
79
        std::cout << "Number of matched points: " << matches.size() << std::endl;
80
 
81
        std::nth_element(matches.begin(),    // initial position
82
                             matches.begin()+24, // position of the sorted element
83
                                         matches.end());     // end position
84
        // remove all elements after the 25th
85
        matches.erase(matches.begin()+25, matches.end());
86
 
87
        cv::Mat imageMatches;
88
        cv::drawMatches(image1,keypoints1,  // 1st image and its keypoints
89
                            image2,keypoints2,  // 2nd image and its keypoints
90
                                        matches,                        // the matches
91
                                        imageMatches,           // the image produced
92
                                        cv::Scalar(255,255,255)); // color of the lines
93
        cv::namedWindow("Matches");
94
        cv::imshow("Matches",imageMatches);
95
 
96
        cv::waitKey();
97
        return 0;
98
 
99
        int size=7;
100
        cv::Mat imaf1;
101
        image1.convertTo(imaf1,CV_32F);
102
 
103
        cv::Mat imaf2;
104
        image2.convertTo(imaf2,CV_32F);
105
 
106
        cv::waitKey();
107
        return 0;
5 PointedEar 108
}