码迷,mamicode.com
首页 > 其他好文 > 详细

查找轮廓+寻找凸包

时间:2020-06-30 14:37:06      阅读:47      评论:0      收藏:0      [点我收藏+]

标签:imshow   int()   pre   nbsp   amp   create   raw   imp   turn   

 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 }

 

查找轮廓+寻找凸包

标签:imshow   int()   pre   nbsp   amp   create   raw   imp   turn   

原文地址:https://www.cnblogs.com/winslam/p/13157860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!