opencv基本图像处理——添加滑动条trackbar

int cvCreateTrackbar(

const char* trackbar_name, //滑动条的名称

const char* window_name, //窗口的名称,滑动条不会遮挡图像

int* value, //当滑动条被拖到时,OpenCV会自动将当前位置所代表的值传给指针指向的整数

int count, //滑动条所能达到的最大值

CvTrackbarCallback on_change//可选的回调函数,回调函数可参见http://wapedia.mobi/zhtrad/回调函数

);


 

其中openCV的highgui库提供了两个函数来读取和设置滚动条的value值

//读取value值

int cvGetTrackbarPos(

const char* trackbar_name,

cosnt char* window_name

);

//设置value值

void cvSetTrackbarPos(

const char* trackbar_name,

const char* window_name,

int pos

);



2、cv::createtrackbar

int createTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallback onChange=0, void* userdata=0)

参数代表结果与cvCreateTrackbar的相同

//简单的阈值分割和trackbar实验

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
using namespace cv;

/// Global variables
int threshold_value = 0;
int threshold_type = 3;;
int const max_value = 255;
int const max_type = 4;
int const max_BINARY_value = 255;

Mat src, src_gray, dst;
const char* window_name = "Threshold Demo";

const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
const char* trackbar_value = "Value";

/// Function headers
void Threshold_Demo( int, void* );

int main( int, char** argv )
{
  /// Load an image
 // src = imread( argv[1], 1 );
  src = imread("../images/WindowsLogo.jpg",1);

  /// Convert the image to Gray
  cvtColor( src, src_gray, CV_RGB2GRAY );

  /// Create a window to display results
  namedWindow( window_name, CV_WINDOW_AUTOSIZE );

  /// Create Trackbar to choose type of Threshold
  createTrackbar( trackbar_type,
          window_name, &threshold_type,
          max_type, Threshold_Demo );

  createTrackbar( trackbar_value,
          window_name, &threshold_value,
          max_value, Threshold_Demo );

  /// Call the function to initialize
  Threshold_Demo( 0, 0 );

  /// Wait until user finishes program
  for(;;)
    {
      int c;
      c = waitKey( 20 );
      if( (char)c == 27 )
    { break; }
    }

}


/**
 * @function Threshold_Demo
 */
void Threshold_Demo( int, void* )
{
  /* 0: Binary
     1: Binary Inverted
     2: Threshold Truncated
     3: Threshold to Zero
     4: Threshold to Zero Inverted
   */

  threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );

  imshow( window_name, dst );
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值