【opencv学习之十二】opencv滑条及实例

来源:互联网 发布:广东省造价软件 编辑:程序博客网 时间:2024/05/20 07:15

先看createTrackbar方法的源代码:

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

第一个参数,const string&类型的trackbarname,表示轨迹条的名字。

第二个参数,const string&类型的winname,对应的窗口名字,表示要显示在那个窗口上,即namedWindow()。

第三个参数,int* 类型的value,一个指向整型的指针,表示滑块初始值。
第四个参数,int类型的count,表示滑块可以达到的最大位置的值,另外滑块最小值始终为0。
第五个参数,TrackbarCallback类型的onChange,首先注意他有默认值0。这是一个指向回调函数的指针,每次滑块位置改变时,这个函数都会进行回调。并且这个函数的原型必须为void XXXX(int,void*);其中第一个参数是轨迹条的位置,第二个参数是用户数据(看下面的第六个参数)。如果回调是NULL指针,表示没有回调函数的调用,仅第三个参数value有变化。

第六个参数,void*类型的userdata,他也有默认值0。这个参数是用户传给回调函数的数据,用来处理轨迹条事件。如果使用的第三个参数value实参是全局变量的话,完全可以不去管这个userdata参数。

下面实例代码:

#include "mainwindow.h"#include <QApplication>#include <QMessageBox>#include <QFileDialog>#include <QDebug>#include <QTime>#include <QDir>#include <QFile>#include <math.h>#include <opencv/cv.h>#include <opencv/highgui.h>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/core/core.hpp>#include <iostream>#pragma execution_character_set("utf-8")using namespace cv;using namespace std;///////////////////////////////////////////////////////////// 第一个滑条实例//////////////////////////////////////////////////////////Mat img;int threshval = 160;            //轨迹条滑块对应的值,给初值160static void on_trackbar(int, void*)//  描述:轨迹条的回调函数{    Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);    vector<vector<Point> > contours;  //定义点和向量    vector<Vec4i> hierarchy;//定义点和向量    findContours( bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );  //查找轮廓    Mat dst = Mat::zeros(img.size(), CV_8UC3);//初始化dst    if( !contours.empty() && !hierarchy.empty() ) //开始处理    {        //遍历所有顶层轮廓,随机生成颜色值绘制给各连接组成部分        int idx = 0;        for( ; idx >= 0; idx = hierarchy[idx][0] )        {            Scalar color( (rand()&255), (rand()&255), (rand()&255) );            //绘制填充轮廓            drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );        }    }    imshow( "Connected Components", dst );//显示窗口}int main(int argc, char *argv[]){    QApplication a(argc, argv);    //    system("color 5F");   //控制台程序时候显示蓝色    img = imread("D:/12.jpg", 0); //载入图片    if( !img.data ) { printf("Oh,no,读取img图片文件错误~! \n"); return -1; }    namedWindow( "Image", 1 );//显示原图    imshow( "Image", img );  //原图    namedWindow( "Connected Components", 1 ); //创建处理窗口    createTrackbar(                "Threshold", //const char* trackbarName,滑条名字                "Connected Components",//const char* windowName,名字对应窗口名字,不然不显示                &threshval,//int* value,显示时候滑条初始值                255, //int count,最大值                on_trackbar//CvTrackbarCallback onChange,滑动时候触发函数                );//创建轨迹条    on_trackbar(threshval, 0);//轨迹条回调函数,必须有不然    waitKey(0);    MainWindow w;    w.show();    return a.exec();}////////////////////////////////////////////////////////////// 第二个滑条实例//////////////////////////////////////////////////////////////IplImage* src = NULL  ;//IplImage* dst = NULL ;//static const char* wnd_name = "canny" ;//static const char* file_name = "D:/12.jpg" ;//static const char* trackbar_name = "threshold" ;//void on_track( int pos )//{//    if( src->nChannels != 1 )//    {//        printf("source image is not gray/n");//    }//    if( pos == 0  )//    {//        cvShowImage(wnd_name,src);//    }//    else//    {//        cvCanny(src,dst,pos,pos * 3 ,3);//边缘检测//        cvShowImage(wnd_name,dst);//    }//}//int main(int argc, char *argv[])//{//    QApplication a(argc, argv);//    int value = 0 ;//    src = cvLoadImage( file_name,0 );//    dst = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);//    cvNamedWindow(wnd_name,CV_WINDOW_AUTOSIZE ) ;//    cvCreateTrackbar(//                trackbar_name,//const char* trackbarName,//                wnd_name,//const char* windowName,//                &value,//int* value,//                100,//int count,//                on_track//CvTrackbarCallback onChange//                );//    on_track(0);//    cvWaitKey(0);//    cvDestroyAllWindows();//    cvReleaseImage(&src);//    cvReleaseImage(&dst);//    MainWindow w;//    w.show();//    return a.exec();//}
显示效果如下:



原创粉丝点击