opencv-074-二值图像分析(提取最大轮廓与编码关键点)

知识点

二值化方法选择:
全局阈值二值化
基于形态学梯度二值化
inRange二值化
基于Canny边缘二值化
自适应二值化

操作步骤

二值化方法,得到二值图像,然后进行轮廓分析,根据面积寻找最大轮廓,然后根据轮廓进行多边形逼近,获得轮廓关键点,最后可以绘制轮廓与关键点。

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

using namespace std;
using namespace cv;

/*
* 二值图像分析(提取最大轮廓与编码关键点)
*/
int main() {
Mat src = imread("../images/case6.jpg");
if (src.empty()) {
cout << "could not load image.." << endl;
}
imshow("input", src);

// 图像二值化
Mat gray, binary;
GaussianBlur(src, src, Size(5, 5), 0);
cvtColor(src, gray, COLOR_BGR2GRAY);
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);

// 闭操作
Mat se = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(binary, binary, MORPH_CLOSE, se);

// 发现最大轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binary, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
int height = src.rows;
int width = src.cols;
int index = -1;
int max = 0;
for (size_t t = 0; t < contours.size(); ++t) {
Rect rect = boundingRect(contours[t]);
if (rect.height >= height || rect.width >= width) {
continue;
}
double area = contourArea(contours[t]);
if (area > max) {
max = area;
index = t;
}
}

// 绘制关键点
Mat result = Mat::zeros(src.size(), src.type());
Mat pts;
drawContours(src, contours, index, Scalar(0, 0, 255));
approxPolyDP(contours[index], pts, 4, true);
for (int i = 0; i < pts.rows; ++i) {
Vec2i pt = pts.at<Vec2i>(i, 0);
circle(src, Point(pt[0], pt[1]), 2, Scalar(0, 255, 0), 2);
circle(result, Point(pt[0], pt[1]), 2, Scalar(0, 255, 0), 2);
}

imshow("result", src);
imshow("result_binary", result);
waitKey(0);
return 0;
}
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
import cv2 as cv
import numpy as np

src = cv.imread("D:/images/case6.jpg")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)

# 图像二值化
# src = cv.GaussianBlur(src, (5, 5), 0)
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)

se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1))
binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, se)
cv.imshow("binary", binary)

# 轮廓提取
out, contours, hierarchy = cv.findContours(binary, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
height, width = src.shape[:2]
index = 0
max = 0
for c in range(len(contours)):
x, y, w, h = cv.boundingRect(contours[c])
if h >=height or w >= width:
continue
area = cv.contourArea(contours[c])
if area > max:
max = area
index = c

# 绘制轮廓关键点与轮廓
result = np.zeros(src.shape, dtype=np.uint8)
keypts = cv.approxPolyDP(contours[index], 4, True)
cv.drawContours(src, contours, index, (0, 0, 255), 1, 8)
cv.drawContours(result, contours, index, (0, 0, 255), 1, 8)
print(keypts)
for pt in keypts:
cv.circle(src, (pt[0][0], pt[0][1]), 2, (0, 255, 0), 2, 8, 0)
cv.circle(result, (pt[0][0], pt[0][1]), 2, (0, 255, 0), 2, 8, 0)
cv.imshow("result", result)
cv.imshow("output", src)
cv.imwrite("D:/result.png", result)
cv.imwrite("D:/output.png", src)

cv.waitKey(0)
cv.destroyAllWindows()

结果

代码地址

github