# Creating trackbar in c++

**URL:** <https://forum.opencv.org/t/creating-trackbar-in-c/5331>\
**Category:** C++\
**Tags:** highgui\
**Created:** [September 23, 2021, 5:06am UTC](https://forum.opencv.org/t/creating-trackbar-in-c/5331 "2021-09-23T05:06:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhargavah](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhargavah](https://forum.opencv.org/u/bhargavah)\
**Post date:** [September 23, 2021, 5:06am UTC](https://forum.opencv.org/t/creating-trackbar-in-c/5331/1 "2021-09-23T05:06:42Z")

</div>

Hi Guys,

I am trying to detect colors using opencv. To do that, I convert the image to HSV colorspace first and then do the operation. And to provide the range of hsv min and max, I wanted to use the trackbars but it seems like I can not create trackbars with normal functions.

```auto
#include<iostream>
#include<opencv2/imgcodecs.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>

int hmin = 0, smin = 110, vmin = 153, hmax = 19, smax = 240, vmax = 255;

int main() {
  std::string imagePath = "/Users/apple/Learning/youtubeCourse/Learn-OpenCV-cpp-in-4-Hours-main/Resources/lambo.png";
  cv::Mat imageMat = cv::imread(imagePath);
  cv::imshow("Original image", imageMat);

  cv::Mat hsvMat;
  cv::cvtColor(imageMat, hsvMat, cv::COLOR_BGR2HSV);
  cv::imshow("HSV image", hsvMat);

  cv::namedWindow("Trackbars", 0); 
  cv::createTrackbar("Hue min", "Trackbars", &hmin, 200, 0); 

  cv::Mat oneColorMatrix;
  while(true) {
    cv::Scalar lower(hmin, smin, vmin);
    cv::Scalar higher(hmax, smax, vmax);
    cv::inRange(hsvMat, lower, higher, oneColorMatrix);
    cv::imshow("One color matrix", oneColorMatrix);
  }

  cv::waitKey(0);
}

```

And the error message I get during execution is this:

`[WARN:0] global /tmp/opencv-20210728-15491-hx6tk7/opencv-4.5.3/modules/highgui/src/window.cpp (704) createTrackbar UI/Trackbar(Hue min@Trackbars): Using 'value' pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback.`

Does this ring a bell to anyone?

---

<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:** [September 23, 2021, 7:53am UTC](https://forum.opencv.org/t/creating-trackbar-in-c/5331/2 "2021-09-23T07:53:14Z")

</div>

> [@bhargavah](#):
>
> Does this ring a bell to anyone?

yea, the api is changing here, they are trying to get away from using “raw pointers” in the interface. so this:

> [@bhargavah](#):
>
> ```auto
> cv::createTrackbar("Hue min", "Trackbars", &hmin, 200, 0); 
> 
> ```

should be changed to:

```auto
cv::createTrackbar("Hue min", "Trackbars", nullptr, 200, 0);
cv::setTrackbarPos("Hue min", "Trackbars", hmin);

```

also:

```auto
  while(true) {
    hmin = cv::getTrackbarPos("Hue min", "Trackbars");
 
    cv::Scalar lower(hmin, smin, vmin);
    ...

```

---

<div class="post-metadata">

**Author:** ![bhargavah](https://avatars.discourse-cdn.com/v4/letter/b/bc79bd/32.png) [@bhargavah](https://forum.opencv.org/u/bhargavah)\
**Post date:** [September 23, 2021, 11:36am UTC](https://forum.opencv.org/t/creating-trackbar-in-c/5331/3 "2021-09-23T11:36:46Z")

</div>

I see. Can you please enlighten me the reason for that as well. Thanks a lot for your reply.

---

<div class="post-metadata">

**Author:** ![hazyparker](https://avatars.discourse-cdn.com/v4/letter/h/85e7bf/32.png) [@hazyparker](https://forum.opencv.org/u/hazyparker)\
**Post date:** [November 25, 2021, 7:35am UTC](https://forum.opencv.org/t/creating-trackbar-in-c/5331/5 "2021-11-25T07:35:32Z")

</div>

This issue doesn’t occur on the 4.5.2 version. It happened on 4.5.3. The third parameter in `createTrackbar` values, this should be a pointer to `that bar`; However, when I use the code above, that value is fixed no matter how you move the bar. This might be a BUG I guess.

---

<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:** [November 25, 2021, 7:39am UTC](https://forum.opencv.org/t/creating-trackbar-in-c/5331/6 "2021-11-25T07:39:34Z")

</div>

> [@hazyparker](#):
>
> However, when I use the code above, that value is fixed no matter how you move the bar

```auto
 hmin = cv::getTrackbarPos("Hue min", "Trackbars");

```
