# Assertion failed (trackbar) in getTrackbarPos

**URL:** <https://forum.opencv.org/t/assertion-failed-trackbar-in-gettrackbarpos/8008>\
**Category:** C++\
**Tags:** highgui\
**Created:** [March 19, 2022, 8:15am UTC](https://forum.opencv.org/t/assertion-failed-trackbar-in-gettrackbarpos/8008 "2022-03-19T08:15:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PSTricks\_Fans](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/pstricks_fans/32/2197_2.png) [@PSTricks\_Fans](https://forum.opencv.org/u/PSTricks_Fans)\
**Post date:** [March 19, 2022, 8:15am UTC](https://forum.opencv.org/t/assertion-failed-trackbar-in-gettrackbarpos/8008/1 "2022-03-19T08:15:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 19, 2022, 10:53am UTC](https://forum.opencv.org/t/assertion-failed-trackbar-in-gettrackbarpos/8008/2 "2022-03-19T10:53:22Z")

</div>

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

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

 -- the problem goes away !

(i've NO explanation, for now)

```

---

<div class="post-metadata">

**Author:** ![PSTricks\_Fans](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/pstricks_fans/32/2197_2.png) [@PSTricks\_Fans](https://forum.opencv.org/u/PSTricks_Fans)\
**Post date:** [March 19, 2022, 12:06pm UTC](https://forum.opencv.org/t/assertion-failed-trackbar-in-gettrackbarpos/8008/3 "2022-03-19T12:06:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [March 20, 2022, 10:59am UTC](https://forum.opencv.org/t/assertion-failed-trackbar-in-gettrackbarpos/8008/4 "2022-03-20T10:59:25Z")

</div>

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);

```

> [@PSTricks\_Fans](#):
>
> This issue only occurs when using OpenCV for Windows

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 !

---

<div class="post-metadata">

**Author:** ![PSTricks\_Fans](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/pstricks_fans/32/2197_2.png) [@PSTricks\_Fans](https://forum.opencv.org/u/PSTricks_Fans)\
**Post date:** [March 21, 2022, 9:02am UTC](https://forum.opencv.org/t/assertion-failed-trackbar-in-gettrackbarpos/8008/5 "2022-03-21T09:02:25Z")

</div>

Thank you very much. Understood.
