查找轮廓+寻找凸包

时间:2020-06-30
本文章向大家介绍 查找轮廓+寻找凸包 ,主要包括 查找轮廓+寻找凸包 使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 #include "opencv2/highgui/highgui.hpp"
 2 #include "opencv2/imgproc/imgproc.hpp"
 3 #include <iostream>
 4 
 5 using namespace cv;
 6 using namespace std;
 7 
 8 #define WINDOW_NAME1 "【原始图窗口】"                    
 9 #define WINDOW_NAME2 "【效果图窗口】"                
10 
11 Mat g_srcImage; Mat g_grayImage;
12 int g_nThresh = 50;
13 int g_maxThresh = 255;
14 RNG g_rng(12345);
15 Mat srcImage_copy = g_srcImage.clone();
16 Mat g_thresholdImage_output;
17 vector<vector<Point> > g_vContours;
18 vector<Vec4i> g_vHierarchy;
19 
20 void on_ThreshChange(int, void*);
21 void ShowHelpText();
22 
23 int main()
24 {
25     g_srcImage = imread("0.PNG", 1);
26 
27     cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY);
28     blur(g_grayImage, g_grayImage, Size(3, 3));
29 
30     imshow(WINDOW_NAME1, g_srcImage);
31 
32     createTrackbar(" 阈值:", WINDOW_NAME1, &g_nThresh, g_maxThresh, on_ThreshChange);
33     on_ThreshChange(0, 0);
34 
35     waitKey(0);
36     return(0);
37 }
38 
39 void on_ThreshChange(int, void*)
40 {
41     threshold(g_grayImage, g_thresholdImage_output, g_nThresh, 255, THRESH_BINARY);
42 
43     findContours(g_thresholdImage_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
44 
45     vector<vector<Point> >hull(g_vContours.size());
46     for (unsigned int i = 0; i < g_vContours.size(); i++)
47     {
48         convexHull(Mat(g_vContours[i]), hull[i], false);
49     }
50 
51     Mat drawing = Mat::zeros(g_thresholdImage_output.size(), CV_8UC3);
52     for (unsigned int i = 0; i < g_vContours.size(); i++)
53     {
54         Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));
55         //drawContours(drawing, g_vContours, i, color, 1, 8, vector<Vec4i>(), 0, Point());
56         drawContours(drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point());
57     }
58 
59     imshow(WINDOW_NAME2, drawing);
60 }

 

$flag 上一页 下一页