OpenCV高级图形用户界面(5)获取指定滑动条(trackbar)的当前位置函数getTrackbarPos()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

返回滑动条的位置。

该函数返回指定滑动条的当前位置。

cv::getTrackbarPos() 函数用于获取指定滑动条(trackbar)的当前位置。

注释
[仅 Qt 后端] 如果滑动条附加到了控制面板,则 winname 可以为空。

函数原型

int cv::getTrackbarPos	
(
	const String & 	trackbarname,
	const String & 	winname 
)	

参数

  • 参数trackbarname 滑动条的名称。
  • 参数twinname 作为滑动条父级的窗口的名称。

返回值

返回一个整数值,表示滑动条的当前位置。

代码示例


#include <iostream>
#include <opencv2/opencv.hpp>

// 回调函数
void onTrackbarSlide( int pos, void* userData )
{
    cv::Mat img = *( cv::Mat* )userData;
    cv::Mat thresholdImg;
    cv::threshold( img, thresholdImg, pos, 255, cv::THRESH_BINARY );
    cv::imshow( "Threshold Image", thresholdImg );
}

int main()
{
    // 加载图像
    cv::Mat img = cv::imread("/media/dingxin/data/study/OpenCV/sources/images/hawk.jpg", cv::IMREAD_GRAYSCALE );
    if ( img.empty() )
    {
        std::cerr << "Error: Image not found!" << std::endl;
        return -1;
    }

    // 创建窗口
    cv::namedWindow( "Threshold Image" );

    // 创建滑动条
    int thresholdValue = 128;
    cv::createTrackbar( "Threshold Value", "Threshold Image", &thresholdValue, 255, onTrackbarSlide, &img );

    // 初始显示
    cv::Mat thresholdImg;
    cv::threshold( img, thresholdImg, thresholdValue, 255, cv::THRESH_BINARY );
    cv::imshow( "Threshold Image", thresholdImg );

    // 获取滑动条的当前位置
    int currentPosition = cv::getTrackbarPos( "Threshold Value", "Threshold Image" );
    std::cout << "Current trackbar position: " << currentPosition << std::endl;

    // 主循环
    while ( true )
    {
        int key = cv::waitKey( 1 );
        if ( key == 27 )
        {  // ESC 键
            break;
        }
    }

    // 释放资源
    cv::destroyAllWindows();

    return 0;
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值