Assertion failed (trackbar) in getTrackbarPos

How to fix the following error?

OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(4.5.5) Error: Assertion failed (trackbar) in getTrackbarPos,
file C:\opencv-4.5.5\sources\modules\highgui\src\window.cpp, line 862

#include <opencv2/opencv.hpp>
#include <cstdlib>
#include <vector>

using namespace cv;
using namespace std;

template <typename T>
T PosToActual(T min, T max, int pos, int TICKS)
{
    return min + (max - min) * pos / TICKS;
}

void Refresh(int, void *data)
{
    Mat *original = (Mat *)data;

    Mat modified;
    vector<Mat> channels;
    split(*original, channels);

    int b = cv::getTrackbarPos("Blue", "Modified");
    int g = cv::getTrackbarPos("Green", "Modified");
    int r = cv::getTrackbarPos("Red", "Modified");

    channels[0] *= PosToActual<float>(0, 1, b, 10);
    channels[1] *= PosToActual<float>(0, 1, g, 10);
    channels[2] *= PosToActual<float>(0, 1, r, 10);

    merge(channels, modified);

    imshow("Modified", modified);
}

int main()
{
    const string filename = "family.jpg";

    Mat original = imread(filename);

    // Mat modified = Mat::zeros(original.size(), original.type());

    cv::namedWindow("Modified", WINDOW_NORMAL);
    int bFactor = 5;
    int gFactor = 5;
    int rFactor = 5;

    createTrackbar("Blue", "Modified", &bFactor, 10, Refresh, &original);
    createTrackbar("Green", "Modified", &gFactor, 10, Refresh, &original);
    createTrackbar("Red", "Modified", &rFactor, 10, Refresh, &original);

    Refresh(0, &original);

    waitKey();
}

Edit:

This issue only occurs when using OpenCV for Windows (prebuilt) version

  • 4.5.3
  • 4.5.4
  • 4.5.5

It works just fine for version 4.5.2. I have not checked for version lower than this yet.

1 Like

pretty weird bug, you found there…

however, you must have had those

[ WARN:0@0.029] global C:\p\opencv\modules\highgui\src\window.cpp (698) createTrackbar UI/Trackbar(Blue@Modified): Using 'value' pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback.

warning messages, and if you follow those, and create your trackbars like

createTrackbar("Blue", "Modified", nullptr, 10, Refresh, &original);
setTrackbarPos("Blue", "Modified", bFactor);

 -- the problem goes away !

(i've NO explanation, for now)

Thank you very much. @berak Please kindly see my last edit in the question. It is about version.

1 Like

already the 1st createTrackbar() calls Refresh() (since there is a value pointer), and the other two bars are not yet created – kaboom.

in other words : since they all share the same Refresh() callback, you may not set / get their position, until all of them were created successfully !

solution for now:

// 1st create ALL bars, without value pointer
createTrackbar("Blue", "Modified", 0, 10, Refresh, &original);
createTrackbar("Green", "Modified", 0, 10, Refresh, &original);
createTrackbar("Red", "Modified", 0, 10, Refresh, &original);

// then update their resp. position
setTrackbarPos("Blue", "Modified", bFactor);
setTrackbarPos("Green", "Modified", gFactor);
setTrackbarPos("Red", "Modified", rFactor);

i somehow doubt, that this is os or platform specific
(problems are all in the generic window.cpp)
but nice, that you found out, with which version this regression started !

1 Like

Thank you very much. Understood.