opencv-083-角点检测(亚像素级别角点检测)

知识点

OpenCV中角点检测的结果实际不够精准,角点检测最后的结果是整数值,因为真实的计算中有些位置可能是在浮点数的空间内才最大值,这样就需要我们通过给定的响应值,在像素邻域空间进行拟合,实现亚像素级别的角点检测。如:(100,5)实际上应该是(100.126,4.329) .

API

1
2
3
4
5
6
7
8
9
10
11
12
void cv::cornerSubPix(
InputArray image,
InputOutputArray corners,
Size winSize,
Size zeroZone,
TermCriteria criteria
)
image单通道输入图像,八位或者浮点数
corners是输入输出的关键点坐标集合
winSize表示插值计算时候窗口大小
zeroZone表示搜索区域中间的dead region边长的一半,有时用于避免自相关矩阵的奇异性。如果值设为(-1,-1)则表示没有这个区域。
criteria角点精准化迭代过程的终止条件

代码(c++,python)

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
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

void process_frame(Mat &image);

RNG rng(12345);

/*
* 角点检测(亚像素级别角点检测)
*/
int main() {
VideoCapture capture("../images/color_object.mp4");
if (!capture.isOpened()) {
cout << "could not open video..." << endl;
return -1;
}

Mat frame;
while (true) {
bool ret = capture.read(frame);
imshow("input", frame);
if (!ret) break;

process_frame(frame);
imshow("result", frame);

char c = waitKey(5);
if (c == 27) {
break;
}
}

waitKey(0);
return 0;
}

void process_frame(Mat &image) {
// Detector parameters
int maxCorners = 100;
double quality_level = 0.01;
double minDistance = 0.04;

// detecting corners
Mat gray, dst;
cvtColor(image, gray, COLOR_BGR2GRAY);
vector<Point2f> corners;
goodFeaturesToTrack(gray, corners, maxCorners, quality_level,
minDistance, Mat(), 3, false);

// detect sub-pixel 亚像素检测
Size winSize = Size(5,5);
Size zeroZone = Size(-1,-1);
TermCriteria criteria = TermCriteria(TermCriteria::EPS +
TermCriteria::COUNT, 40, 0.001);
cornerSubPix(gray, corners, winSize, zeroZone, criteria);

// drawing corner
for (int i = 0; i < corners.size(); ++i) {
int b = rng.uniform(0, 255);
int g = rng.uniform(0, 255);
int r = rng.uniform(0, 255);
circle(image, corners[i], 5, Scalar(b, g, r), 3);
}
}
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
import numpy as np
import cv2 as cv


def process(image, opt=1):
# Detecting corners
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
corners = cv.goodFeaturesToTrack(gray, 100, 0.05, 10)
print(len(corners))
for pt in corners:
print(pt)
b = np.random.random_integers(0, 256)
g = np.random.random_integers(0, 256)
r = np.random.random_integers(0, 256)
x = np.int32(pt[0][0])
y = np.int32(pt[0][1])
cv.circle(image, (x, y), 5, (int(b), int(g), int(r)), 2)

# detect sub-pixel
winSize = (5, 5)
zeroZone = (-1, -1)
criteria = (cv.TERM_CRITERIA_EPS + cv.TermCriteria_COUNT, 40, 0.001)
# Calculate the refined corner locations
corners = cv.cornerSubPix(gray, corners, winSize, zeroZone, criteria)
# display
for i in range(corners.shape[0]):
print(" -- Refined Corner [", i, "] (", corners[i, 0, 0], ",", corners[i, 0, 1], ")")
return image


cap = cv.VideoCapture("D:/images/video/vtest.avi")
while True:
ret, frame = cap.read()
frame = cv.flip(frame, 1)
cv.imwrite("D:/input.png", frame)
cv.imshow('input', frame)
result = process(frame)
cv.imshow('result', result)
k = cv.waitKey(5)&0xff
if k == 27:
break
cap.release()
cv.destroyAllWindows()

结果

代码地址

github