由浅入深解释createTracbar opencv

滑条

学 习 策 略 : 前 两 个 是 一 个 整 体 , 看 第 一 个 不 太 明 白 就 看 第 二 个 。 再 不 明 白 , 就 看 附 录 中 对 函 数 的 介 绍 。 第 三 个 是 对 图 像 的 操 作 , 后 两 个 是 对 视 频 的 操 作 ! \textcolor{red}{学习策略:前两个是一个整体,看第一个不太明白就看第二个。\\再不明白,就看附录中对函数的介绍。\\第三个是对图像的操作,后两个是对视频的操作!}

1. 创建一个最简单的滑条

1.1 解释

此次创建的滑条并没有什么作用,我们仅仅是把滑条创建出来,并不会对其做任何改变。

1.2 代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
#define Win "faces"
#define Track "Trackbar"

void On_trackbar(int,void*)  //回调函数,欲知详情请往下看
{

}

int main(int argc, char**argv)
{
    int value_less = 0;
    Mat src = imread("../faces.png");
    namedWindow(Win);  // 创建滑动条之前必须创建框体
    createTrackbar(Track, Win, &value_less, 3, On_trackbar, &src);
    imshow(Win, src);   
    waitKey(0);
    return 0;
}

1.3 效果

2. 开始对滑条加入一些新功能

2.1 需求分析

我们想要加入一些新的东西,使得图像发生一些变化,但是creatTrackbar()只能够创建一个滑条。至于滑条做的事情,该函数管不着。为了解决这个问题,该函数使用了回调函数的机制。

2.2 回调函数

简而言之,就是在执行A函数时,内置回调函数。在运行过程中,回调函数辅助主函数,完成想要完成的事情!

(当然,我的解释很拉跨。因此还是建议百度之)

2.3 滑条中的回调函数

函数原型:

void (*TrackbarCallback)(int pos, void* userdata);

p a r a m \textcolor{green}{param} param

  • pos:表示滑块当前所在位置
  • userdata:createTrackbar中的value

f u n c t i o n : \textcolor{green}{function:} function:

完成图像的变化

2.4 代码

// 实现效果如同第一次实现内容
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
#define Win "faces"
#define Track "Trackbar"

void On_trackbar(int pos,void*img)
{
    Mat src = *(Mat*)img;	
    imshow(Win, src);    
}

int main(int argc, char**argv)
{
    int value_less = 0;
    Mat src = imread("../faces.png");
    // 创建滑动条之前必须创建框体
    namedWindow(Win);
    createTrackbar(Track, Win, &value_less, 3, On_trackbar, &src);
    waitKey(0);
    return 0;
}

2.5 效果

3. 使用滑条对图像进行操作

此 部 分 , 已 经 默 认 你 学 过 二 值 化 相 关 内 容 ( 这 部 分 比 较 简 单 , 一 看 就 懂 ) \textcolor{red}{此部分,已经默认你学过二值化相关内容(这部分比较简单,一看就懂)}

3.1 对图像的二值化操作

根据我们之前的叙述,改变图像的代码应该加入到回调函数里面。

### 3.2 代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
#define Win "faces"
#define Track "Trackbar"

void On_trackbar(int pos ,void*img)
{
    Mat src = *(Mat*)img;
    Mat dst;
    threshold(src, dst, pos, 255, 0); //注意:此为新加入的内容
    imshow(Win, dst);    
}

int main(int argc, char**argv)
{
    int value_less = 0;
    Mat src = imread("../faces.png");
    // 创建滑动条之前必须创建框体
    namedWindow(Win);
    createTrackbar(Track, Win, &value_less, 255, On_trackbar, &src);
    waitKey(0);
    return 0;
}

4. 制作一个简易的视频播放器

学 习 此 部 分 , 默 认 你 已 经 掌 握 视 频 相 关 的 内 容 以 及 部 分 函 数 \textcolor{red}{学习此部分,默认你已经掌握视频相关的内容以及部分函数}

4.1 需求

制作一个简易的视频播放器,满足以下要求:

  • 能够显示一个视频
  • 创建一个滑块,并且保证能够显示出目前所在帧
  • 拖动滑块,能够移动到所在的帧

4.2 代码

#include <iostream>
#include <opencv2/opencv.hpp>
#include <unistd.h>
using namespace std;
using namespace cv;
#define Win "faces"
#define Track1 "Track1bar"  // 设置当前帧
#define Path "../tree.avi"

VideoCapture cap;
void On_Track1bar(int pos ,void*img)
{
    cap.set(CAP_PROP_POS_FRAMES, pos);  // 将拖动帧数设为当前帧数
}

int main(int argc, char**argv)
{
    cap.open(Path);		//	打开所在路径的视频
    Mat frame;
    int value_less = 0;
    int value_more = cap.get(CAP_PROP_FRAME_COUNT); // 总帧数
    namedWindow(Win);    // 创建滑动条之前必须创建框体
    createTrackbar(Track1, Win, &value_less, value_more, On_Track1bar);
    while (true)
    {
        cap >> frame;
        int current_pos = (int)cap.get(CAP_PROP_POS_FRAMES);
        setTrackbarPos(Track1, Win, current_pos); // 设置滑动条位置:自动更新
        imshow(Win, frame);
        if(Waitkey(10)==27) break;
    }
    
    return 0;
}

5. 制作一个可暂停的视频播放器

5.1 需求

制作一个可暂停的视频播放器,满足以下条件:

  • 能够显示一个视频
  • 创建一个滑条,能够显示当前所在帧
  • 拖动滑条,能够把视频移动到需要移动到的帧
  • 再创建一个滑条,当滑条置于0的时候为暂停,1的时候为播放

### 5.2 简单解析

这个难度也不大,仅仅再加入一个滑条,并且改一下逻辑就好了。

(嘿嘿,过于简单)

5.3 代码

#include <iostream>
#include <opencv2/opencv.hpp>
#include <unistd.h>
using namespace std;
using namespace cv;
#define Win "faces"
#define Track1 "Track1bar"  //设置当前帧
#define Track2 "Track2Bar"  // 设置暂停、继续
#define Path "../tree.avi"

VideoCapture cap;
int situation = 1;

void On_Track1bar(int pos ,void*img)
{
    cap.set(CAP_PROP_POS_FRAMES, pos);  //将拖动帧数设为当前帧数
}

void On_Track2bar(int pos, void*img)
{
    if(pos==0) situation=0;
    if(pos==1) situation=1; 
}
int main(int argc, char**argv)
{
    cap.open(Path);
    Mat frame;
    int value_less = 0;
    int value_more = cap.get(CAP_PROP_FRAME_COUNT);
    int stop_pos = 0;
    int begin_pos = 1;
    namedWindow(Win);    // 创建滑动条之前必须创建框体
    createTrackbar(Track1, Win, &value_less, value_more, On_Track1bar);
    createTrackbar(Track2, Win, &stop_pos, begin_pos, On_Track2bar);
    while (true)
    {
        if (situation == 1)
        {
            cap >> frame;
            int current_pos = (int)cap.get(CAP_PROP_POS_FRAMES);
            setTrackbarPos(Track1, Win, current_pos); //设置滑动条位置:自动更新
            imshow(Win, frame);
        }
        if(waitKey(100)==27) break;
    }
    
    return 0;
}

6. 总结

滑 条 部 分 并 不 难 , 如 果 想 要 掌 握 , 只 需 要 看 前 两 个 例 子 就 可 以 了 。 关 键 是 怎 么 向 其 中 加 入 操 作 , 修 改 逻 辑 。 \textcolor{pink}{滑条部分并不难,如果想要掌握,只需要看前两个例子就可以了。\\关键是怎么向其中加入操作,修改逻辑。}

7. 附录

7.1 creatTrackbar()

官方代码

/*
@param trackbarname Name of the created trackbar.
@param winname Name of the window that will be used as a parent of the created trackbar.
@param value Optional pointer to an integer variable whose value reflects the position of the
slider. Upon creation, the slider position is defined by this variable.
@param count Maximal position of the slider. The minimal position is always 0.
@param onChange Pointer to the function to be called every time the slider changes position. This
function should be prototyped as void Foo(int,void\*); , where the first parameter is the trackbar
position and the second parameter is the user data (see the next parameter). If the callback is
the NULL pointer, no callbacks are called, but only value is updated.
@param userdata User data that is passed as is to the callback. It can be used to handle trackbar
events without using global variables.
    */
CV_EXPORTS int createTrackbar(const String& trackbarname, const String& winname,
                              int* value, int count,
                              TrackbarCallback onChange = 0,
                              void* userdata = 0);

函数作用

创建一个滑条

参数解释

  • 滑条名称
  • 窗口名称
  • 最小值(指针)
  • 最大值(int)
  • 回调函数
  • 其他数据

7.2 setTrackbarPos()

官方解释

/*
@param trackbarname Name of the trackbar.
@param winname Name of the window that is the parent of trackbar.
@param pos New position.
 */
CV_EXPORTS_W void setTrackbarPos(const String& trackbarname, const String& winname, int pos);

函数作用

设置滑条到某一位置

参数解释

  • 滑条名称
  • 窗口名称
  • 滑条移动到某一位置

如 果 你 读 过 《 学 习 O p e n C V 3 》 这 本 书 , 你 就 会 发 现 : 用 来 举 例 子 的 几 个 题 其 实 是 作 业 题 ! \textcolor{purple}{如果你读过《学习OpenCV3》这本书,你就会发现:\\用来举例子的几个题其实是作业题!} OpenCV3

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值