建轨迹条——createTrackbar函数

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值