OpenCV 滑动条Trackbar C/C++/Python

OpenCV 滑动条Trackbar C/C++/Python

参考:

opencv 滑动条 cvCreateTrackbar():http://blog.csdn.net/u012005313/article/details/46788445
createTrackbar:http://docs.opencv.org/2.4.11/modules/highgui/doc/user_interface.html#createtrackbar
Adding a Trackbar to our applications!:http://docs.opencv.org/2.4.11/doc/tutorials/highgui/trackbar/trackbar.html#adding-a-trackbar-to-our-applications


使用滑动条控制参数的改变,调用回调函数修改显示的图像,更直观的比对不同参数的变化

主要内容

  1. C/C++/Python 函数格式
  2. C++ 下调用滑动条
  3. C 下调用滑动条
  4. Python 下调用滑动条
  5. getTrackbarPos

C/C++/Python 函数格式

C++ 函数:

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

参数:

  • trackbarname:滑动条名,滑动条的标识符,不同滑动条拥有不同的滑动条名
  • winname:窗口名,滑动条想要绑定的窗口的名字
  • value:可选,用于反映滑块位置。创建滑动条时可用此值指定滑块位置
  • count:滑块最大值。最小值通常是 0
  • onChange:回调函数,函数原型为 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`
  • userdata:User data that is passed as is to the callback.
    • It can be used to handle trackbar events without using global variables.

    C 函数:

    int cvCreateTrackbar(const char* trackbar_name, const char* window_name,
         int* value, int count, CvTrackbarCallback on_change=NULL )
    

    参数:

    • trackbar_name:同上
    • window_name:同上
    • value:同上
    • count:同上
    • on_change:函数原型为 void Foo(int)

    Python 函数:

    def createTrackbar(trackbarName, windowName, value, count, onChange)
    

    参数:同上,其中 onChange 回调函数有一个参数


    C++ 下调用滑动条

    程序 1:进行形态学膨胀操作,改变迭代次数

    #include 
    #include 
    #include 
    using namespace std;
    using namespace cv;
    
    // 滑动条名
    const string trackbarname = "expand";
    // 窗口名
    const string winname = "TrackbarDemo";
    // 最大值
    const int maxNum = 10;
    
    // 预设值
    int value = 3;
    
    Mat img;
    Mat res;
    
    int dilation_type = MORPH_RECT;
    int dilation_size = 1;
    Mat element = getStructuringElement(dilation_type, 
        Size(2 * dilation_size + 1, 2 * dilation_size + 1),
        Point(dilation_size, dilation_size));
    
    void onExpand(int, void*) {
        printf("value: %d\n", value);
        dilate(img, res, element, Point(-1, -1),value);
    
        imshow(winname, res);
    }
    
    int main(int argc, char* argv[]) {
        img = imread("c:\\lena.jpg", -1);
        if (img.empty()) {
            exit(0);
        }
        res = img.clone();
    
        namedWindow(winname);
        createTrackbar(trackbarname, winname, &value, maxNum, onExpand, NULL);
    
        onExpand(0, NULL);
    
        waitKey(0);
    
        return 0;
    }
    

    结果:

    OpenCV 滑动条Trackbar C/C++/Python_第1张图片

    如果需要改变多个参数,可以使用多个滑动条

    程序 2:改变迭代次数以及核大小

    #include 
    #include 
    #include 
    using namespace std;
    using namespace cv;
    
    // 滑动条名
    const string trackbarname1 = "interation";
    const string trackbarname2 = "dilationSize";
    // 窗口名
    const string winname = "TrackbarDemo";
    // 最大值
    const int maxNum1 = 10;
    const int maxNum2 = 5;
    
    // 预设值
    int interationValue = 3;
    int dilationSize = 2;
    
    Mat img;
    Mat res;
    
    int dilation_type = MORPH_RECT;
    
    void onDilate(int, void*) {
        printf("dilationSize: %d\n", dilationSize);
        printf("interationValue: %d\n", interationValue);
    
        Mat element = getStructuringElement(dilation_type,
            Size(2 * dilationSize + 1, 2 * dilationSize + 1),
            Point(dilationSize, dilationSize));
    
        dilate(img, res, element, Point(-1, -1), interationValue);
    
        imshow(winname, res);
    }
    
    int main(int argc, char* argv[]) {
        img = imread("c:\\lena.jpg", -1);
        if (img.empty()) {
            exit(0);
        }
        res = img.clone();
    
        namedWindow(winname);
        createTrackbar(trackbarname1, winname, &interationValue, maxNum1, onDilate, NULL);
        createTrackbar(trackbarname2, winname, &dilationSize, maxNum2, onDilate, NULL);
    
        onDilate(0, NULL);
    
        waitKey(0);
    
        return 0;
    }
    

    结果:

    OpenCV 滑动条Trackbar C/C++/Python_第2张图片


    C 下调用滑动条

    程序 1:进行形态学膨胀操作,改变迭代次数

    #include 
    #include 
    #include 
    #include 
    
    // 滑动条名
    const char* trackbarname = "interation";
    // 窗口名
    const char* winanme = "TrackbarDemo";
    // 最大值
    const int maxNum = 10;
    
    int interationValue = 3;
    
    IplImage* img;
    IplImage* res;
    
    IplConvKernel* element;
    
    void onDilate(int arg) {
    
        printf("interationValue: %d\n", interationValue);
    
        cvDilate(img, res, element, interationValue);
        cvShowImage(winanme, res);
    }
    
    
    int main(int argc, char* argv[]) {
        img = cvLoadImage("c:\\lena.jpg", CV_LOAD_IMAGE_UNCHANGED);
        if (img->imageData == NULL) {
            exit(0);
        }
        res = cvCreateImage(cvSize(img->width, img->height), img->depth, img->nChannels);
    
        int dilationSize = 1;
        int row = dilationSize * 2 + 1;
        int col = dilationSize * 2 + 1;
        element = cvCreateStructuringElementEx(2 * dilationSize + 1,
             2 * dilationSize + 1, dilationSize, dilationSize, CV_SHAPE_RECT, NULL);
    
    
        cvNamedWindow(winanme, CV_WINDOW_AUTOSIZE);
        cvCreateTrackbar(trackbarname, winanme, &interationValue, maxNum, onDilate);
    
        onDilate(0);
    
        cvWaitKey(0);
    
        cvReleaseStructuringElement(&element);
        cvReleaseImage(&img);
        cvReleaseImage(&res);
        cvDestroyWindow(winanme);
    
        return 0;
    }
    

    程序 2:改变迭代次数以及核大小

    #include 
    #include 
    #include 
    #include 
    
    // 滑动条名
    const char* trackbarname1 = "interation";
    const char* trackbarname2 = "dilationSize";
    // 窗口名
    const char* winanme = "TrackbarDemo";
    // 最大值
    const int maxNum1 = 10;
    const int maxNum2 = 5;
    
    int interationValue = 3;
    int dilationSize = 1;
    
    IplImage* img;
    IplImage* res;
    
    IplConvKernel* element;
    
    void onDilate(int arg) {
    
        printf("interationValue: %d\n", interationValue);
        printf("dilationSize: %d\n", dilationSize);
    
        int row = dilationSize * 2 + 1;
        int col = dilationSize * 2 + 1;
        element = cvCreateStructuringElementEx(2 * dilationSize + 1,
             2 * dilationSize + 1, dilationSize, dilationSize, CV_SHAPE_RECT, NULL);
    
        cvDilate(img, res, element, interationValue);
        cvShowImage(winanme, res);
    
        cvReleaseStructuringElement(&element);
    }
    
    
    int main(int argc, char* argv[]) {
        img = cvLoadImage("c:\\lena.jpg", CV_LOAD_IMAGE_UNCHANGED);
        if (img->imageData == NULL) {
            exit(0);
        }
        res = cvCreateImage(cvSize(img->width, img->height), img->depth, img->nChannels);
    
    
        cvNamedWindow(winanme, CV_WINDOW_AUTOSIZE);
        cvCreateTrackbar(trackbarname1, winanme, &interationValue, maxNum1, onDilate);
        cvCreateTrackbar(trackbarname2, winanme, &dilationSize, maxNum2, onDilate);
    
        onDilate(0);
    
        cvWaitKey(0);
    
        cvReleaseImage(&img);
        cvReleaseImage(&res);
        cvDestroyWindow(winanme);
    
        return 0;
    }
    

    Python 下调用滑动条

    程序 1:进行形态学膨胀操作,改变迭代次数

    # -*- coding: utf-8 -*-
    
    import cv2
    import os
    
    trackbar_name = "interation"
    win_name = "TrackbarDemo"
    
    max_num = 10
    value = 3
    
    kernerl = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    
    
    def on_dialte(arg):
        """
        膨胀操作
        :param arg:
        :return:
        """
        value = cv2.getTrackbarPos(trackbar_name, win_name)
        print "value:", value
    
        res = cv2.dilate(img, kernerl, None, None, value)
        cv2.imshow(win_name, res)
    
    
    if __name__ == '__main__':
        img = cv2.imread("c:\\lena.jpg", cv2.CV_LOAD_IMAGE_UNCHANGED)
        if img is None:
            os._exit(0)
    
        cv2.namedWindow(win_name)
        cv2.createTrackbar(trackbar_name, win_name, value, max_num, on_dialte)
    
        on_dialte(0)
    
        cv2.waitKey(0)
    

    程序 2:改变迭代次数以及核大小

    # -*- coding: utf-8 -*-
    
    import cv2
    import os
    
    trackbar_name1 = "interation"
    trackbar_name2 = "dilationSize"
    
    win_name = "TrackbarDemo"
    
    max_num1 = 10
    max_num2 = 5
    
    interation_value = 3
    dilation_size = 1
    
    
    def on_dilate(arg):
        """
        形态学膨胀操作
        :param arg:
        :return:
        """
        value = cv2.getTrackbarPos(trackbar_name1, win_name)
        print "value:", value
        size = cv2.getTrackbarPos(trackbar_name2, win_name)
        print "size:", size
    
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2 * size + 1, 2 * size + 1))
        res = cv2.dilate(img, kernel, None, None, value)
        cv2.imshow(win_name, res)
    
    
    if __name__ == '__main__':
        img = cv2.imread("c:\\lena.jpg", cv2.CV_LOAD_IMAGE_UNCHANGED)
        if img is None:
            os._exit(0)
    
        cv2.namedWindow(win_name)
        cv2.createTrackbar(trackbar_name1, win_name, interation_value, max_num1, on_dilate)
        cv2.createTrackbar(trackbar_name2, win_name, dilation_size, max_num2, on_dilate)
    
        on_dilate(0)
    
        cv2.waitKey(0)
    

    getTrackbarPos

    参考:getTrackbarPos

    C++ 函数:

    int getTrackbarPos(const string& trackbarname, const string& winname)
    

    C 函数:

    int cvGetTrackbarPos(const char* trackbar_name, const char* window_name)
    

    Python 函数:

    cv2.getTrackbarPos(trackbarname, winname) → retval
    

    参数:

    第一个:滑动条名
    第二个:窗口名

    返回值:指定窗口上指定滑动条的当前位置

你可能感兴趣的:(opencv,c,c++,python)