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 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
#include "harrisDetector.h"
26
 
27
int main()
28
{
29
        // Read input image
30
        cv::Mat image= cv::imread("../church01.jpg",0);
31
        if (!image.data)
32
                return 0;
33
 
34
    // Display the image
35
        cv::namedWindow("Original Image");
36
        cv::imshow("Original Image",image);
37
 
38
        // Detect Harris Corners
39
        cv::Mat cornerStrength;
40
        cv::cornerHarris(image,cornerStrength,
41
                          3,     // neighborhood size
42
                                         3,     // aperture size
43
                                         0.01); // Harris parameter
44
 
45
   // threshold the corner strengths
46
        cv::Mat harrisCorners;
47
        double threshold= 0.0001;
48
        cv::threshold(cornerStrength,harrisCorners,
49
                 threshold,255,cv::THRESH_BINARY_INV);
50
 
51
    // Display the corners
52
        cv::namedWindow("Harris Corner Map");
53
        cv::imshow("Harris Corner Map",harrisCorners);
54
 
55
        // Create Harris detector instance
56
        HarrisDetector harris;
57
    // Compute Harris values
58
        harris.detect(image);
59
    // Detect Harris corners
60
        std::vector<cv::Point> pts;
61
        harris.getCorners(pts,0.01);
62
        // Draw Harris corners
63
        harris.drawOnImage(image,pts);
64
 
65
    // Display the corners
66
        cv::namedWindow("Harris Corners");
67
        cv::imshow("Harris Corners",image);
68
 
69
        // Read input image
70
        image= cv::imread("../church01.jpg",0);
71
 
72
        // Compute good features to track
73
        std::vector<cv::Point2f> corners;
74
        cv::goodFeaturesToTrack(image,corners,
75
                500,    // maximum number of corners to be returned
76
                0.01,   // quality level
77
                10);    // minimum allowed distance between points
78
 
79
        // for all corners
80
        std::vector<cv::Point2f>::const_iterator it= corners.begin();
81
        while (it!=corners.end()) {
82
 
83
                // draw a circle at each corner location
84
                cv::circle(image,*it,3,cv::Scalar(255,255,255),2);
85
                ++it;
86
        }
87
 
88
    // Display the corners
89
        cv::namedWindow("Good Features to Track");
90
        cv::imshow("Good Features to Track",image);
91
 
92
        // Read input image
93
        image= cv::imread("../church01.jpg",0);
94
 
95
        // vector of keypoints
96
        std::vector<cv::KeyPoint> keypoints;
97
        // Construction of the Good Feature to Track detector 
98
        cv::GoodFeaturesToTrackDetector gftt(
99
                500,    // maximum number of corners to be returned
100
                0.01,   // quality level
101
                10);    // minimum allowed distance between points
102
        // point detection using FeatureDetector method
103
        gftt.detect(image,keypoints);
104
 
105
        cv::drawKeypoints(image,                // original image
106
                keypoints,                                      // vector of keypoints
107
                image,                                          // the resulting image
108
                cv::Scalar(255,255,255),        // color of the points
109
                cv::DrawMatchesFlags::DRAW_OVER_OUTIMG); //drawing flag
110
 
111
    // Display the corners
112
        cv::namedWindow("Good Features to Track Detector");
113
        cv::imshow("Good Features to Track Detector",image);
114
 
115
        // Read input image
116
        image= cv::imread("../church01.jpg",0);
117
 
118
        keypoints.clear();
119
        cv::FastFeatureDetector fast(40);
120
        fast.detect(image,keypoints);
121
 
122
        cv::drawKeypoints(image,keypoints,image,cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_OVER_OUTIMG);
123
 
124
    // Display the corners
125
        cv::namedWindow("FAST Features");
126
        cv::imshow("FAST Features",image);
127
 
128
        // Read input image
129
        image= cv::imread("../church03.jpg",0);
130
 
131
        keypoints.clear();
132
        // Construct the SURF feature detector object
133
        cv::SurfFeatureDetector surf(2500);
134
        // Detect the SURF features
135
        surf.detect(image,keypoints);
136
 
137
        cv::Mat featureImage;
138
        cv::drawKeypoints(image,keypoints,featureImage,cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
139
 
140
    // Display the corners
141
        cv::namedWindow("SURF Features");
142
        cv::imshow("SURF Features",featureImage);
143
 
144
        // Read input image
145
        image= cv::imread("../church01.jpg",0);
146
 
147
        keypoints.clear();
148
        // Construct the SURF feature detector object
149
        cv::SiftFeatureDetector sift(
150
                0.03,  // feature threshold
151
                10.);  // threshold to reduce
152
                   // sensitivity to lines
153
 
154
        // Detect the SURF features
155
        sift.detect(image,keypoints);
156
 
157
        cv::drawKeypoints(image,keypoints,featureImage,cv::Scalar(255,255,255),cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
158
 
159
    // Display the corners
160
        cv::namedWindow("SIFT Features");
161
        cv::imshow("SIFT Features",featureImage);
162
 
163
        // Read input image
164
        image= cv::imread("../church01.jpg",0);
165
 
166
        keypoints.clear();
167
 
168
        cv::MserFeatureDetector mser;
169
        mser.detect(image,keypoints);
170
 
171
        // Draw the keypoints with scale and orientation information
172
        cv::drawKeypoints(image,                // original image
173
                keypoints,                                      // vector of keypoints
174
                featureImage,                           // the resulting image
175
                cv::Scalar(255,255,255),        // color of the points
176
                cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS); //drawing flag
177
 
178
    // Display the corners
179
        cv::namedWindow("MSER Features");
180
        cv::imshow("MSER Features",featureImage);
181
 
182
        cv::waitKey();
183
        return 0;
184
}