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.
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:
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?)
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.
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:
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
}