-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharris_corners_detector.cpp
70 lines (64 loc) · 2.29 KB
/
harris_corners_detector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "harris_corners_detector.h"
harris_corners_detector::harris_corners_detector():neighbourhood(3), aperture(3),
k(0.01), maxStrength(0.0),
threshold(0.01), nonMaxSize(3)
{
}
void harris_corners_detector::detect(const cv::Mat& image) {
// Harris computation
cv::cornerHarris(image,cornerStrength,
neighbourhood,// neighborhood size
aperture, // aperture size
k); // Harris parameter
// internal threshold computation
double minStrength; // not used
cv::minMaxLoc(cornerStrength,
&minStrength,&maxStrength);
// local maxima detection
cv::Mat dilated; // temporary image
cv::dilate(cornerStrength,dilated,cv::Mat());
cv::compare(cornerStrength,dilated,
localMax,cv::CMP_EQ);
}
cv::Mat harris_corners_detector::getCornerMap(double qualityLevel) {
cv::Mat cornerMap;
// thresholding the corner strength
threshold= qualityLevel*maxStrength;
cv::threshold(cornerStrength,cornerTh,
threshold,255,cv::THRESH_BINARY);
// convert to 8-bit image
cornerTh.convertTo(cornerMap,CV_8U);
// non-maxima suppression
cv::bitwise_and(cornerMap,localMax,cornerMap);
return cornerMap;
}
void harris_corners_detector::getCorners(std::vector<cv::Point> &points,
double qualityLevel) {
// Get the corner map
cv::Mat cornerMap= getCornerMap(qualityLevel);
// Get the corners
getCorners(points, cornerMap);
}
void harris_corners_detector::getCorners(std::vector<cv::Point> &points,
const cv::Mat& cornerMap) {
// Iterate over the pixels to obtain all features
for( int y = 0; y < cornerMap.rows; y++ ) {
const uchar* rowPtr = cornerMap.ptr<uchar>(y);
for( int x = 0; x < cornerMap.cols; x++ ) {
// if it is a feature point
if (rowPtr[x]) {
points.push_back(cv::Point(x,y));
}
}
}
}
void harris_corners_detector::drawOnImage(cv::Mat &image,const std::vector<cv::Point> &points,cv::Scalar color,int radius, int thickness) {
std::vector<cv::Point>::const_iterator it=
points.begin();
// for all corners
while (it!=points.end()) {
// draw a circle at each corner location
cv::circle(image,*it,radius,color,thickness);
++it;
}
}