建轨迹条——createTrackbar函数

来源:互联网 发布:网络舆论与司法公正 编辑:程序博客网 时间:2024/05/22 16:46
#include<cv.h>
#include<highgui.h>
#include<iostream>
using namespace cv;
using namespace std;
Mat img;
int threshval = 160;//轨迹条滑块对应的初始值为160
//轨迹条的回调函数
static void on_trackbar(int , void *)
{
Mat bw = threshval <120?(img< threshval):(img>threshval);
//定义点和向量
vector<vector<Point>>contours;
vector<Vec4i>hierachy;
//查找轮廓
findContours(bw ,contours,hierachy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
//初始化det
Mat dst = Mat::zeros(img.size(),CV_8UC3);
//开始处理
if(!contours.empty() && ! hierachy.empty() )
{
//遍历所有顶层轮廓,随机生成颜色值绘制给各连接组成部分
int idx = 0;
for(; idx >=0 ; idx = hierachy[idx][0])
{
Scalar color (( rand()&255),(rand()&255),(rand()&255));
//绘制轮廓
drawContours( dst , contours,idx,color,CV_FILLED,8,hierachy);
}
}
//显示窗口
imshow("Connected Components",dst);


}
int main()
{
//system("color 5F");
img = imread ("cloud.jpg",0);//单通道
if(!img.data)
{
printf("it is wrong \n ");
return -1;
}
namedWindow("Image",WINDOW_NORMAL);
imshow ("Image",img);
//创建处理窗口
namedWindow("Connected Components",WINDOW_NORMAL);
//创建轨迹条
createTrackbar("Threshold","Connected Components",&threshval,255,on_trackbar);
on_trackbar(threshval,0);//轨迹回调函数
waitKey(0);
return 0 ;


}
1 0
原创粉丝点击