'OpenCV createTrackbar

I am attempting to create a Track bar on my OpenCV application using createTrackbar function provided by OpenCV.

I want to add three trackbars actually one that will shuffle through range of colours, 2nd one that shuffles through Canny filter values and 3rd that shuffles through medianBlur values.

I am struggling with adding pointer to an integer variable, whose value will reflect the position of the slider.

So here is my inRange & Canny filter & medianNlur functions:

    Mat range_out;
        inRange(blur_out, Scalar(100, 100, 100), Scalar(120, 255, 255), range_out); 

    Mat mBlur;
        medianBlur(erode,mBlur,7);

        Mat canny_out;
        Canny(mBlur, canny_out, 125,350);


        createTrackbar("Colour values: ", window_Output, &range_out, 255);
createTrackbar("Colour values: ", window_Output, &mBlur, 7);
createTrackbar("Colour values: ", window_Output, &canny_out, 350);

From what i read the the variables have to be integer(i.e. range_out is Matrix Mat not int)

Could someone direct me to a decent tutorial where i could find my answer that I am looking for or explain how to do it.



Solution 1:[1]

I usually use the callback function in following fashion, which makes the code less cumbersome.

const int median_blur_max = 5;
int median_blur;
Mat img;
Mat src, dst;

void callBackFunction( int, void* ) {
    medianBlur(src,dst,median_blur);
    imshow("Controls", img );
}

int main( int argc, char** argv ) {
     namedWindow("Controls", 1);
     createTrackbar("Median blur", "Controls", &median_blur, median_blur_max, callBackFunction );

     callBackFunction(median_blur, 3);

     waitKey(0);
     return 0;
}

The example is not complete, but gives a good overview of the elements and useage of the callBackFunction.

It seems unclear to me if you wish to change Mat in your functions or not, but if thats the case you can simply add a switch-caseor if-sentence in the callBackFunction that changes the Mat for a given function call.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1