OpenCV4入门系列教程87:查找和绘制图像轮廓矩

索引地址:系列索引

上一篇:OpenCV4入门系列教程86:图像距(几何矩、中心矩、hu矩)

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//--------------------------------------【程序说明】-------------------------------------------
// 程序说明:《OpenCV3编程入门》OpenCV3版书本配套示例程序76
// 程序描述:查找和绘制图片轮廓矩
// 2014年11月 Created by @浅墨_毛星云
// 2014年12月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------

//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;

//-----------------------------------【宏定义部分】--------------------------------------------
// 描述:定义一些辅助宏
//------------------------------------------------------------------------------------------------
#define WINDOW_NAME1 "Original" //为窗口标题定义的宏
#define WINDOW_NAME2 "Result" //为窗口标题定义的宏

//-----------------------------------【全局变量声明部分】--------------------------------------
// 描述:全局变量的声明
//-----------------------------------------------------------------------------------------------
Mat g_srcImage;
Mat g_grayImage;
int g_nThresh = 100;
int g_nMaxThresh = 255;
RNG g_rng(12345);
Mat g_cannyMat_output;
vector<vector<Point>> g_vContours;
vector<Vec4i> g_vHierarchy;

//-----------------------------------【全局变量声明部分】--------------------------------------
// 描述:全局变量的声明
//-----------------------------------------------------------------------------------------------
void on_ThreshChange(int, void *);

//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main(int argc, char **argv) {
// 读入原图像, 返回3通道图像数据
g_srcImage = imread("1.jpg", 1);

// 把原图像转化成灰度图像并进行平滑
cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY);
blur(g_grayImage, g_grayImage, Size(3, 3));

// 创建新窗口
namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE);
imshow(WINDOW_NAME1, g_srcImage);

//创建滚动条并进行初始化
createTrackbar(" 阈值", WINDOW_NAME1, &g_nThresh, g_nMaxThresh, on_ThreshChange);
on_ThreshChange(0, 0);

waitKey(0);
return (0);
}

//-----------------------------------【on_ThreshChange( )函数】-------------------------------
// 描述:回调函数
//-----------------------------------------------------------------------------------------------
void on_ThreshChange(int, void *) {
// 使用Canndy检测边缘
Canny(g_grayImage, g_cannyMat_output, g_nThresh, g_nThresh * 2, 3);

// 找到轮廓
findContours(g_cannyMat_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE,
Point(0, 0));

// 计算矩
vector<Moments> mu(g_vContours.size());
for (unsigned int i = 0; i < g_vContours.size(); i++) {
mu[ i ] = moments(g_vContours[ i ], false);
}

// 计算中心矩
vector<Point2f> mc(g_vContours.size());
for (unsigned int i = 0; i < g_vContours.size(); i++) {
mc[ i ] = Point2f(static_cast<float>(mu[ i ].m10 / mu[ i ].m00),
static_cast<float>(mu[ i ].m01 / mu[ i ].m00));
}

// 绘制轮廓
Mat drawing = Mat::zeros(g_cannyMat_output.size(), CV_8UC3);
for (unsigned int i = 0; i < g_vContours.size(); i++) {
Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255),
g_rng.uniform(0, 255)); //随机生成颜色值
drawContours(drawing, g_vContours, i, color, 2, 8, g_vHierarchy, 0,
Point()); //绘制外层和内层轮廓
circle(drawing, mc[ i ], 4, color, -1, 8, 0);
; //绘制圆
}

// 显示到窗口中
namedWindow(WINDOW_NAME2, WINDOW_AUTOSIZE);
imshow(WINDOW_NAME2, drawing);

// 通过m00计算轮廓面积并且和OpenCV函数比较
printf("\t 输出内容: 面积和轮廓长度\n");
for (unsigned int i = 0; i < g_vContours.size(); i++) {
printf(" >通过m00计算出轮廓[%d]的面积: (M_00) = %.2f \n OpenCV函数计算出的面积=%.2f , "
"长度: %.2f \n\n",
i, mu[ i ].m00, contourArea(g_vContours[ i ]), arcLength(g_vContours[ i ], true));
Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));
drawContours(drawing, g_vContours, i, color, 2, 8, g_vHierarchy, 0, Point());
circle(drawing, mc[ i ], 4, color, -1, 8, 0);
}
}

下一篇:OpenCV4入门系列教程88:用几何矩计算轮廓中心与横纵比过滤