目录

OpenCV之HighGUI图形用户界面初步

滑动条的创建使用

滑动条的创建使用

 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
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;


//全局函数声明
Mat img;
int threshval = 160;   //轨迹条滑块对应的值,给初值160

//
//描述:轨迹条的回调函数
//
static void on_trackbar(int, void*)
{
    Mat bw = threshval < 128 ? (img < threshval):(img > threshval);
	
    //定义点和向量
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;

    //查找轮廓
    findContours(bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);	
	//初始化dst
	Mat dst = Mat::zeros(img.size(), CV_8UC3);
	//开始处理
	if(!contours.empty() && !hierarchy.empty())
	{
	    //遍历所有顶层轮廓,随机生成颜色值绘制个连接组成部分
		int idx = 0;
		for( ; idx >= 0; idx = hierarchy[idx][0])
		{
		    Scalar color((rand()&255), (rand()&255), (rand()&255));
			//绘制填充轮廓
			drawContours(dst, contours, idx, color, CV_FILLED, 8, hierarchy);
		 
		}
	}
	
	//显示窗口
	imshow("Connected Components", dst);
}


int main()
{
   img = imread("img.jpg");
   if(!img.data)
   {
     printf("Oh, no, img失败了 \n");
	 return -1;
   }
   
   //显示原图
   namedWindow("Image", 1);
   imshow("Image", img);
   
   //创建处理窗口
   namedWindow("Connected Components", 1);
   //创建轨迹条
   createTrackbar("Threshold", "Connected Components", &threshval, 255, on_trackbar);
   on_trackbar(threshval, 0); //轨迹条回调函数
   
   waitKey(0);
    
   return 0;
}