├── .github
└── workflows
│ └── manual.yml
├── CMakeLists.txt
├── CODEOWNERS
├── LICENSE
├── README.md
├── images
├── KITTI
│ └── 2011_09_26
│ │ └── image_00
│ │ └── data
│ │ ├── 0000000000.png
│ │ ├── 0000000001.png
│ │ ├── 0000000002.png
│ │ ├── 0000000003.png
│ │ ├── 0000000004.png
│ │ ├── 0000000005.png
│ │ ├── 0000000006.png
│ │ ├── 0000000007.png
│ │ ├── 0000000008.png
│ │ └── 0000000009.png
└── keypoints.png
└── src
├── MidTermProject_Camera_Student.cpp
├── dataStructures.h
├── matching2D.hpp
└── matching2D_Student.cpp
/.github/workflows/manual.yml:
--------------------------------------------------------------------------------
1 | # Workflow to ensure whenever a Github PR is submitted,
2 | # a JIRA ticket gets created automatically.
3 | name: Manual Workflow
4 |
5 | # Controls when the action will run.
6 | on:
7 | # Triggers the workflow on pull request events but only for the master branch
8 | pull_request_target:
9 | types: [opened, reopened]
10 |
11 | # Allows you to run this workflow manually from the Actions tab
12 | workflow_dispatch:
13 |
14 | jobs:
15 | test-transition-issue:
16 | name: Convert Github Issue to Jira Issue
17 | runs-on: ubuntu-latest
18 | steps:
19 | - name: Checkout
20 | uses: actions/checkout@master
21 |
22 | - name: Login
23 | uses: atlassian/gajira-login@master
24 | env:
25 | JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }}
26 | JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }}
27 | JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
28 |
29 | - name: Create NEW JIRA ticket
30 | id: create
31 | uses: atlassian/gajira-create@master
32 | with:
33 | project: CONUPDATE
34 | issuetype: Task
35 | summary: |
36 | Github PR [Assign the ND component] | Repo: ${{ github.repository }} | PR# ${{github.event.number}}
37 | description: |
38 | Repo link: https://github.com/${{ github.repository }}
39 | PR no. ${{ github.event.pull_request.number }}
40 | PR title: ${{ github.event.pull_request.title }}
41 | PR description: ${{ github.event.pull_request.description }}
42 | In addition, please resolve other issues, if any.
43 | fields: '{"components": [{"name":"Github PR"}], "customfield_16449":"https://classroom.udacity.com/", "customfield_16450":"Resolve the PR", "labels": ["github"], "priority":{"id": "4"}}'
44 |
45 | - name: Log created issue
46 | run: echo "Issue ${{ steps.create.outputs.issue }} was created"
47 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
2 |
3 | add_definitions(-std=c++11)
4 |
5 | set(CXX_FLAGS "-Wall")
6 | set(CMAKE_CXX_FLAGS, "${CXX_FLAGS}")
7 |
8 | project(camera_fusion)
9 |
10 | find_package(OpenCV 4.1 REQUIRED)
11 |
12 | include_directories(${OpenCV_INCLUDE_DIRS})
13 | link_directories(${OpenCV_LIBRARY_DIRS})
14 | add_definitions(${OpenCV_DEFINITIONS})
15 |
16 | # Executable for create matrix exercise
17 | add_executable (2D_feature_tracking src/matching2D_Student.cpp src/MidTermProject_Camera_Student.cpp)
18 | target_link_libraries (2D_feature_tracking ${OpenCV_LIBRARIES})
--------------------------------------------------------------------------------
/CODEOWNERS:
--------------------------------------------------------------------------------
1 | * @udacity/active-public-content
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Udacity
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SFND 2D Feature Tracking
2 |
3 |
4 |
5 | The idea of the camera course is to build a collision detection system - that's the overall goal for the Final Project. As a preparation for this, you will now build the feature tracking part and test various detector / descriptor combinations to see which ones perform best. This mid-term project consists of four parts:
6 |
7 | * First, you will focus on loading images, setting up data structures and putting everything into a ring buffer to optimize memory load.
8 | * Then, you will integrate several keypoint detectors such as HARRIS, FAST, BRISK and SIFT and compare them with regard to number of keypoints and speed.
9 | * In the next part, you will then focus on descriptor extraction and matching using brute force and also the FLANN approach we discussed in the previous lesson.
10 | * In the last part, once the code framework is complete, you will test the various algorithms in different combinations and compare them with regard to some performance measures.
11 |
12 | See the classroom instruction and code comments for more details on each of these parts. Once you are finished with this project, the keypoint matching part will be set up and you can proceed to the next lesson, where the focus is on integrating Lidar points and on object detection using deep-learning.
13 |
14 | ## Dependencies for Running Locally
15 | 1. cmake >= 2.8
16 | * All OSes: [click here for installation instructions](https://cmake.org/install/)
17 |
18 | 2. make >= 4.1 (Linux, Mac), 3.81 (Windows)
19 | * Linux: make is installed by default on most Linux distros
20 | * Mac: [install Xcode command line tools to get make](https://developer.apple.com/xcode/features/)
21 | * Windows: [Click here for installation instructions](http://gnuwin32.sourceforge.net/packages/make.htm)
22 |
23 | 3. OpenCV >= 4.1
24 | * All OSes: refer to the [official instructions](https://docs.opencv.org/master/df/d65/tutorial_table_of_content_introduction.html)
25 | * This must be compiled from source using the `-D OPENCV_ENABLE_NONFREE=ON` cmake flag for testing the SIFT and SURF detectors. If using [homebrew](https://brew.sh/): `$> brew install --build-from-source opencv` will install required dependencies and compile opencv with the `opencv_contrib` module by default (no need to set `-DOPENCV_ENABLE_NONFREE=ON` manually).
26 | * The OpenCV 4.1.0 source code can be found [here](https://github.com/opencv/opencv/tree/4.1.0)
27 |
28 | 4. gcc/g++ >= 5.4
29 | * Linux: gcc / g++ is installed by default on most Linux distros
30 | * Mac: same deal as make - [install Xcode command line tools](https://developer.apple.com/xcode/features/)
31 | * Windows: recommend using either [MinGW-w64](http://mingw-w64.org/doku.php/start) or [Microsoft's VCPKG, a C++ package manager](https://docs.microsoft.com/en-us/cpp/build/install-vcpkg?view=msvc-160&tabs=windows). VCPKG maintains its own binary distributions of OpenCV and many other packages. To see what packages are available, type `vcpkg search` at the command prompt. For example, once you've _VCPKG_ installed, you can install _OpenCV 4.1_ with the command:
32 | ```bash
33 | c:\vcpkg> vcpkg install opencv4[nonfree,contrib]:x64-windows
34 | ```
35 | Then, add *C:\vcpkg\installed\x64-windows\bin* and *C:\vcpkg\installed\x64-windows\debug\bin* to your user's _PATH_ variable. Also, set the _CMake Toolchain File_ to *c:\vcpkg\scripts\buildsystems\vcpkg.cmake*.
36 |
37 |
38 | ## Basic Build Instructions
39 |
40 | 1. Clone this repo.
41 | 2. Make a build directory in the top level directory: `mkdir build && cd build`
42 | 3. Compile: `cmake .. && make`
43 | 4. Run it: `./2D_feature_tracking`.
44 |
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000000.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000000.png
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000001.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000001.png
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000002.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000002.png
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000003.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000003.png
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000004.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000004.png
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000005.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000005.png
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000006.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000006.png
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000007.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000007.png
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000008.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000008.png
--------------------------------------------------------------------------------
/images/KITTI/2011_09_26/image_00/data/0000000009.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/KITTI/2011_09_26/image_00/data/0000000009.png
--------------------------------------------------------------------------------
/images/keypoints.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/udacity/SFND_2D_Feature_Tracking/5843e2bda52c463b00d37e9ce0fdf99bc5bea3a5/images/keypoints.png
--------------------------------------------------------------------------------
/src/MidTermProject_Camera_Student.cpp:
--------------------------------------------------------------------------------
1 | /* INCLUDES FOR THIS PROJECT */
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 |
16 | #include "dataStructures.h"
17 | #include "matching2D.hpp"
18 |
19 | using namespace std;
20 |
21 | /* MAIN PROGRAM */
22 | int main(int argc, const char *argv[])
23 | {
24 |
25 | /* INIT VARIABLES AND DATA STRUCTURES */
26 |
27 | // data location
28 | string dataPath = "../";
29 |
30 | // camera
31 | string imgBasePath = dataPath + "images/";
32 | string imgPrefix = "KITTI/2011_09_26/image_00/data/000000"; // left camera, color
33 | string imgFileType = ".png";
34 | int imgStartIndex = 0; // first file index to load (assumes Lidar and camera names have identical naming convention)
35 | int imgEndIndex = 9; // last file index to load
36 | int imgFillWidth = 4; // no. of digits which make up the file index (e.g. img-0001.png)
37 |
38 | // misc
39 | int dataBufferSize = 2; // no. of images which are held in memory (ring buffer) at the same time
40 | vector dataBuffer; // list of data frames which are held in memory at the same time
41 | bool bVis = false; // visualize results
42 |
43 | /* MAIN LOOP OVER ALL IMAGES */
44 |
45 | for (size_t imgIndex = 0; imgIndex <= imgEndIndex - imgStartIndex; imgIndex++)
46 | {
47 | /* LOAD IMAGE INTO BUFFER */
48 |
49 | // assemble filenames for current index
50 | ostringstream imgNumber;
51 | imgNumber << setfill('0') << setw(imgFillWidth) << imgStartIndex + imgIndex;
52 | string imgFullFilename = imgBasePath + imgPrefix + imgNumber.str() + imgFileType;
53 |
54 | // load image from file and convert to grayscale
55 | cv::Mat img, imgGray;
56 | img = cv::imread(imgFullFilename);
57 | cv::cvtColor(img, imgGray, cv::COLOR_BGR2GRAY);
58 |
59 | //// STUDENT ASSIGNMENT
60 | //// TASK MP.1 -> replace the following code with ring buffer of size dataBufferSize
61 |
62 | // push image into data frame buffer
63 | DataFrame frame;
64 | frame.cameraImg = imgGray;
65 | dataBuffer.push_back(frame);
66 |
67 | //// EOF STUDENT ASSIGNMENT
68 | cout << "#1 : LOAD IMAGE INTO BUFFER done" << endl;
69 |
70 | /* DETECT IMAGE KEYPOINTS */
71 |
72 | // extract 2D keypoints from current image
73 | vector keypoints; // create empty feature list for current image
74 | string detectorType = "SHITOMASI";
75 |
76 | //// STUDENT ASSIGNMENT
77 | //// TASK MP.2 -> add the following keypoint detectors in file matching2D.cpp and enable string-based selection based on detectorType
78 | //// -> HARRIS, FAST, BRISK, ORB, AKAZE, SIFT
79 |
80 | if (detectorType.compare("SHITOMASI") == 0)
81 | {
82 | detKeypointsShiTomasi(keypoints, imgGray, false);
83 | }
84 | else
85 | {
86 | //...
87 | }
88 | //// EOF STUDENT ASSIGNMENT
89 |
90 | //// STUDENT ASSIGNMENT
91 | //// TASK MP.3 -> only keep keypoints on the preceding vehicle
92 |
93 | // only keep keypoints on the preceding vehicle
94 | bool bFocusOnVehicle = true;
95 | cv::Rect vehicleRect(535, 180, 180, 150);
96 | if (bFocusOnVehicle)
97 | {
98 | // ...
99 | }
100 |
101 | //// EOF STUDENT ASSIGNMENT
102 |
103 | // optional : limit number of keypoints (helpful for debugging and learning)
104 | bool bLimitKpts = false;
105 | if (bLimitKpts)
106 | {
107 | int maxKeypoints = 50;
108 |
109 | if (detectorType.compare("SHITOMASI") == 0)
110 | { // there is no response info, so keep the first 50 as they are sorted in descending quality order
111 | keypoints.erase(keypoints.begin() + maxKeypoints, keypoints.end());
112 | }
113 | cv::KeyPointsFilter::retainBest(keypoints, maxKeypoints);
114 | cout << " NOTE: Keypoints have been limited!" << endl;
115 | }
116 |
117 | // push keypoints and descriptor for current frame to end of data buffer
118 | (dataBuffer.end() - 1)->keypoints = keypoints;
119 | cout << "#2 : DETECT KEYPOINTS done" << endl;
120 |
121 | /* EXTRACT KEYPOINT DESCRIPTORS */
122 |
123 | //// STUDENT ASSIGNMENT
124 | //// TASK MP.4 -> add the following descriptors in file matching2D.cpp and enable string-based selection based on descriptorType
125 | //// -> BRIEF, ORB, FREAK, AKAZE, SIFT
126 |
127 | cv::Mat descriptors;
128 | string descriptorType = "BRISK"; // BRIEF, ORB, FREAK, AKAZE, SIFT
129 | descKeypoints((dataBuffer.end() - 1)->keypoints, (dataBuffer.end() - 1)->cameraImg, descriptors, descriptorType);
130 | //// EOF STUDENT ASSIGNMENT
131 |
132 | // push descriptors for current frame to end of data buffer
133 | (dataBuffer.end() - 1)->descriptors = descriptors;
134 |
135 | cout << "#3 : EXTRACT DESCRIPTORS done" << endl;
136 |
137 | if (dataBuffer.size() > 1) // wait until at least two images have been processed
138 | {
139 |
140 | /* MATCH KEYPOINT DESCRIPTORS */
141 |
142 | vector matches;
143 | string matcherType = "MAT_BF"; // MAT_BF, MAT_FLANN
144 | string descriptorType = "DES_BINARY"; // DES_BINARY, DES_HOG
145 | string selectorType = "SEL_NN"; // SEL_NN, SEL_KNN
146 |
147 | //// STUDENT ASSIGNMENT
148 | //// TASK MP.5 -> add FLANN matching in file matching2D.cpp
149 | //// TASK MP.6 -> add KNN match selection and perform descriptor distance ratio filtering with t=0.8 in file matching2D.cpp
150 |
151 | matchDescriptors((dataBuffer.end() - 2)->keypoints, (dataBuffer.end() - 1)->keypoints,
152 | (dataBuffer.end() - 2)->descriptors, (dataBuffer.end() - 1)->descriptors,
153 | matches, descriptorType, matcherType, selectorType);
154 |
155 | //// EOF STUDENT ASSIGNMENT
156 |
157 | // store matches in current data frame
158 | (dataBuffer.end() - 1)->kptMatches = matches;
159 |
160 | cout << "#4 : MATCH KEYPOINT DESCRIPTORS done" << endl;
161 |
162 | // visualize matches between current and previous image
163 | bVis = true;
164 | if (bVis)
165 | {
166 | cv::Mat matchImg = ((dataBuffer.end() - 1)->cameraImg).clone();
167 | cv::drawMatches((dataBuffer.end() - 2)->cameraImg, (dataBuffer.end() - 2)->keypoints,
168 | (dataBuffer.end() - 1)->cameraImg, (dataBuffer.end() - 1)->keypoints,
169 | matches, matchImg,
170 | cv::Scalar::all(-1), cv::Scalar::all(-1),
171 | vector(), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
172 |
173 | string windowName = "Matching keypoints between two camera images";
174 | cv::namedWindow(windowName, 7);
175 | cv::imshow(windowName, matchImg);
176 | cout << "Press key to continue to next image" << endl;
177 | cv::waitKey(0); // wait for key to be pressed
178 | }
179 | bVis = false;
180 | }
181 |
182 | } // eof loop over all images
183 |
184 | return 0;
185 | }
--------------------------------------------------------------------------------
/src/dataStructures.h:
--------------------------------------------------------------------------------
1 | #ifndef dataStructures_h
2 | #define dataStructures_h
3 |
4 | #include
5 | #include
6 |
7 |
8 | struct DataFrame { // represents the available sensor information at the same time instance
9 |
10 | cv::Mat cameraImg; // camera image
11 |
12 | std::vector keypoints; // 2D keypoints within camera image
13 | cv::Mat descriptors; // keypoint descriptors
14 | std::vector kptMatches; // keypoint matches between previous and current frame
15 | };
16 |
17 |
18 | #endif /* dataStructures_h */
19 |
--------------------------------------------------------------------------------
/src/matching2D.hpp:
--------------------------------------------------------------------------------
1 | #ifndef matching2D_hpp
2 | #define matching2D_hpp
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 |
19 | #include "dataStructures.h"
20 |
21 |
22 | void detKeypointsHarris(std::vector &keypoints, cv::Mat &img, bool bVis=false);
23 | void detKeypointsShiTomasi(std::vector &keypoints, cv::Mat &img, bool bVis=false);
24 | void detKeypointsModern(std::vector &keypoints, cv::Mat &img, std::string detectorType, bool bVis=false);
25 | void descKeypoints(std::vector &keypoints, cv::Mat &img, cv::Mat &descriptors, std::string descriptorType);
26 | void matchDescriptors(std::vector &kPtsSource, std::vector &kPtsRef, cv::Mat &descSource, cv::Mat &descRef,
27 | std::vector &matches, std::string descriptorType, std::string matcherType, std::string selectorType);
28 |
29 | #endif /* matching2D_hpp */
30 |
--------------------------------------------------------------------------------
/src/matching2D_Student.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "matching2D.hpp"
3 |
4 | using namespace std;
5 |
6 | // Find best matches for keypoints in two camera images based on several matching methods
7 | void matchDescriptors(std::vector &kPtsSource, std::vector &kPtsRef, cv::Mat &descSource, cv::Mat &descRef,
8 | std::vector &matches, std::string descriptorType, std::string matcherType, std::string selectorType)
9 | {
10 | // configure matcher
11 | bool crossCheck = false;
12 | cv::Ptr matcher;
13 |
14 | if (matcherType.compare("MAT_BF") == 0)
15 | {
16 | int normType = cv::NORM_HAMMING;
17 | matcher = cv::BFMatcher::create(normType, crossCheck);
18 | }
19 | else if (matcherType.compare("MAT_FLANN") == 0)
20 | {
21 | // ...
22 | }
23 |
24 | // perform matching task
25 | if (selectorType.compare("SEL_NN") == 0)
26 | { // nearest neighbor (best match)
27 |
28 | matcher->match(descSource, descRef, matches); // Finds the best match for each descriptor in desc1
29 | }
30 | else if (selectorType.compare("SEL_KNN") == 0)
31 | { // k nearest neighbors (k=2)
32 |
33 | // ...
34 | }
35 | }
36 |
37 | // Use one of several types of state-of-art descriptors to uniquely identify keypoints
38 | void descKeypoints(vector &keypoints, cv::Mat &img, cv::Mat &descriptors, string descriptorType)
39 | {
40 | // select appropriate descriptor
41 | cv::Ptr extractor;
42 | if (descriptorType.compare("BRISK") == 0)
43 | {
44 |
45 | int threshold = 30; // FAST/AGAST detection threshold score.
46 | int octaves = 3; // detection octaves (use 0 to do single scale)
47 | float patternScale = 1.0f; // apply this scale to the pattern used for sampling the neighbourhood of a keypoint.
48 |
49 | extractor = cv::BRISK::create(threshold, octaves, patternScale);
50 | }
51 | else
52 | {
53 |
54 | //...
55 | }
56 |
57 | // perform feature description
58 | double t = (double)cv::getTickCount();
59 | extractor->compute(img, keypoints, descriptors);
60 | t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
61 | cout << descriptorType << " descriptor extraction in " << 1000 * t / 1.0 << " ms" << endl;
62 | }
63 |
64 | // Detect keypoints in image using the traditional Shi-Thomasi detector
65 | void detKeypointsShiTomasi(vector &keypoints, cv::Mat &img, bool bVis)
66 | {
67 | // compute detector parameters based on image size
68 | int blockSize = 4; // size of an average block for computing a derivative covariation matrix over each pixel neighborhood
69 | double maxOverlap = 0.0; // max. permissible overlap between two features in %
70 | double minDistance = (1.0 - maxOverlap) * blockSize;
71 | int maxCorners = img.rows * img.cols / max(1.0, minDistance); // max. num. of keypoints
72 |
73 | double qualityLevel = 0.01; // minimal accepted quality of image corners
74 | double k = 0.04;
75 |
76 | // Apply corner detection
77 | double t = (double)cv::getTickCount();
78 | vector corners;
79 | cv::goodFeaturesToTrack(img, corners, maxCorners, qualityLevel, minDistance, cv::Mat(), blockSize, false, k);
80 |
81 | // add corners to result vector
82 | for (auto it = corners.begin(); it != corners.end(); ++it)
83 | {
84 |
85 | cv::KeyPoint newKeyPoint;
86 | newKeyPoint.pt = cv::Point2f((*it).x, (*it).y);
87 | newKeyPoint.size = blockSize;
88 | keypoints.push_back(newKeyPoint);
89 | }
90 | t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
91 | cout << "Shi-Tomasi detection with n=" << keypoints.size() << " keypoints in " << 1000 * t / 1.0 << " ms" << endl;
92 |
93 | // visualize results
94 | if (bVis)
95 | {
96 | cv::Mat visImage = img.clone();
97 | cv::drawKeypoints(img, keypoints, visImage, cv::Scalar::all(-1), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
98 | string windowName = "Shi-Tomasi Corner Detector Results";
99 | cv::namedWindow(windowName, 6);
100 | imshow(windowName, visImage);
101 | cv::waitKey(0);
102 | }
103 | }
--------------------------------------------------------------------------------