createTrackbar warns that value pointer is deprecated. But is there any other way to set trackbar's initial position?

When I create trackbar by this code

createTrackbar( "Canny thresh:", source_window, &thresh, thresh_max, thresh_callback );

Below warning is reported:
[ WARN:0@0.030] global D:\projects\opencv-src\modules\highgui\src\window.cpp (697) createTrackbar UI/Trackbar(Canny thresh:@Source): Using ‘value’ pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback.

Seems it suggest this way

createTrackbar( "Canny thresh:", source_window, nullptr, thresh_max, thresh_callback );

But I can’t set trackbar’s initial position by this.
Is there other ways to set trackbar position?

1 Like

use a pattern like:

createTrackbar( "Canny thresh:", source_window, nullptr, thresh_max, thresh_callback );
setTrackbarPos( "Canny thresh:", source_window, thresh);

also, docs:
https://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d

2 Likes

As your message is first answer in google I give history

and

The above solution using “nullptr” or “NULL” in combination with setTrackbarPos() does suppress the warning messages for me, but then my slider no longer controls the variable I’m trying to use to do stuff in my code.

How can I either:

  1. suppress the warnings (without changing the trackbar related code) so that users running my application don’t see ugly output in the console (not elegant, but the preferred solution, assuming that some sort of opencv compile directive can suppress?)
  2. change the code so that it does what I originally intended yet does not produce the warnings–using createTrackbar() with a callback function that acts on the variable that the slider is controlling? Please provide a working example.

it’s got a callback, doesn’t it? that’s the recommended way. have it call your callback, set your variable in there.

there is an outdated tutorial for this.

I’ve created an issue for someone to update both the docs and the tutorial.

Thanks for the reply. Running the opencv example code exactly as given works but produces the console warnings I’m trying to avoid in the first place:

[ WARN:0@0.001] global samples.cpp:61 findFile cv::samples::findFile(‘LinuxLogo.jpg’) => ‘/usr/local/lib/…/share/opencv4/samples/data/LinuxLogo.jpg’
[ WARN:0@0.001] global samples.cpp:61 findFile cv::samples::findFile(‘WindowsLogo.jpg’) => ‘/usr/local/lib/…/share/opencv4/samples/data/WindowsLogo.jpg’
[ WARN:0@0.027] global window.cpp:703 createTrackbar UI/Trackbar(Alpha x 100@Linear Blend): Using ‘value’ pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback.

How can I suppress or fix the trackbar warning? (not important, but why do the jpg load operations also give warnings?)

BTW, the way I implement my trackbars is more like this, which also works and gives same warning:

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
 
using namespace cv;
using namespace std;
 
const int alpha_slider_max = 100;
int alpha_slider;
double alpha;
double beta;
 
Mat src1;
Mat src2;
Mat dst;

static void on_trackbar( int, void* )
{
   alpha = (double) alpha_slider/alpha_slider_max;
   beta = ( 1.0 - alpha );
   cout << alpha_slider << endl;
}
 
int main( void )
{
   src1 = imread( samples::findFile("LinuxLogo.jpg") );
   src2 = imread( samples::findFile("WindowsLogo.jpg") );
 
   if( src1.empty() ) { cout << "Error loading src1 \n"; return -1; }
   if( src2.empty() ) { cout << "Error loading src2 \n"; return -1; }
 
   alpha_slider = 0;
   alpha = (double) alpha_slider/alpha_slider_max;
   beta = ( 1.0 - alpha );
   int keycode = 0;
 
   namedWindow("Linear Blend", WINDOW_AUTOSIZE); // Create Window
 
   char TrackbarName[50];
   snprintf( TrackbarName, sizeof(TrackbarName), "Alpha x %d", alpha_slider_max );
   createTrackbar( TrackbarName, "Linear Blend", &alpha_slider, alpha_slider_max, on_trackbar );
 
   while (keycode != 27) {
     addWeighted( src1, alpha, src2, beta, 0.0, dst);
     imshow( "Linear Blend", dst );
     //on_trackbar( alpha_slider, 0 );

     keycode = waitKey(1);
   }

   return 0;
}

those should be infos, not warnings. IDK why anyone would make those be warnings when the lookup succeeded.

feel free to submit an issue for those to be demoted to info level.

well, do as it says, pass NULL/nullptr instead, and use the callback, get the value from its first argument.

that is copied directly from the (bad) example.

wow the repo is full of instances of that bad piece of code. terrible.

use this:

// if the compiler cries about unused userdata, shut it up by using this signature instead:
// static void on_trackbar(int pos, void *)
static void on_trackbar(int pos, void *userdata)
{
    alpha = (double) pos / alpha_slider_max;
    beta = 1 - alpha;
}

...
const int alpha_slider_max = 100;
const int alpha_slider_initial = something;
double alpha;
double beta;
...
createTrackbar(TrackbarName, WindowName, nullptr, alpha_slider_max, on_trackbar);
setTrackbarPos(TrackbarName, WindowName, alpha_slider_initial);
...

ok using your advice I have it working with no warnings for all my trackbars (my actual code is 1000s of lines), but I had to give in and use globals like the example, assigning to the global the value of pos in each callback.

Seems odd that the trackbar function has a variable pointer that wants to be set to null, because if you ignore the warning it still works fine doing it that way–the pointer gets updated upon callback execution, then the value of that local variable can be used elsewhere within main(). I was trying to avoid globals whenever possible because I thought that was lazy (sloppy?) programming practice. I’m not a professional programmer so that’s just my impression.

Also odd that this warning never appeared until now, after I recently built a new OpenCV version (4.10) in Ubuntu for another system. Up until now I had been using early 4.X versions that I had built years ago on my laptop (both Linux and Windows), never having a need to update it since then, so I wonder if this warning is due to something changing with the later OpenCV versions? Or maybe because I used cmake in this case whereas before I was using eclipse and visual studio?

I’ve never gotten the userdata part of trackbar callbacks to work, maybe that’s the way to avoid globals (like the OpenCV function description says). Would like to see an example using that.

Bottom line, I really appreciate you taking the time to help me. I’ve gotten my immediate issue solved. Thanks!

you don’t have to use globals. you can use the userdata pointer to pass anything into the callback. the pointer just has to stay valid, so if you pass the address of a local variable around, make sure it’s still alive whenever the callback is called.

static void on_trackbar(int pos, void *userdata)
{
    *(double*)userdata = pos / alpha_slider_max;
    // beta is always 1 - alpha, so storing that is pointless
}

main
{
    ...
    const int alpha_slider_max = 100;
    const int alpha_slider_initial = something;
    double alpha; // set from the callback, pointer given to createTrackbar
    ...
    createTrackbar(TrackbarName, WindowName, nullptr, alpha_slider_max, on_trackbar, &alpha);
    setTrackbarPos(TrackbarName, WindowName, alpha_slider_initial);
    ...
    // the event loop using one of the waitKey functions
}