OpenCV Trackbar -显示双值而不是int。

时间:2022-04-13 16:01:29

I'm using createTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallback onChange=0, void* userdata=0) in openCV to get an input value, which needs to be a double in the range between 0 and 1. I can easily calculate the needed value from the trackbar position, but obviously the GUI only shows the int-value for the slider, which is a little bit irritating.

我使用createTrackbar(const string&trackbarname, const string&winname, int* value, int count, TrackbarCallback onChange=0, void* userdata=0)在openCV中获取一个输入值,该值需要在0到1之间的范围内增加一倍。我可以很容易地从trackbar位置计算所需的值,但是很明显,GUI只显示了slider的int值,这有点烦人。

Is there any way to change the value the trackbar shows? I can't find any information about this in the documentation or tutorials...

有什么方法可以改变trackbar显示的值吗?我在文档或教程中找不到关于这个的任何信息……

3 个解决方案

#1


2  

No, there is not. Unfortunately, the way the trackbar is implemented (with or without Qt) it does not support that.

不,没有。不幸的是,trackbar的实现方式(有或没有Qt)并不支持这一点。

#2


0  

AFAIK there is not possibility to pass double or other floating point value but still if you are okay let's say with 0.001 precision you can create a trackbar with max value 1000 and then simply treat its value as a double betwen 0 and 1 by dividing trackbar value by max value.

AFAIK没有可能通过双或其他浮点值但是如果你好的假设0.001精度可以创建一个trackbar马克斯价值1000,然后简单地把它的值作为双在0和1 trackbar值除以最大价值。

#3


0  

Like already stated, this is not possible. Here is a little wrapper which calculates double values for you conveniently:

如前所述,这是不可能的。这里有一个小包装器,可以方便地为您计算双值:

class DoubleTrack{
public:

    int int_value = 0;
    double precision;
    void(*user_callback)(double);

    void setup(const std::string& field_name, const std::string& window_name, void(*function)(double), double max_value, double default_value = 0, unsigned precision = 100){
            int_value = default_value * precision;
            createTrackbar(field_name, window_name, &int_value, max_value * precision, DoubleTrack::callback, this);
            user_callback = function;
            this->precision = precision;
    }

    static void callback(int, void* object){
        DoubleTrack* pObject = static_cast<DoubleTrack*>(object);
        pObject->user_callback(pObject->int_value / pObject->precision);
    }

};

static void test(double value){
    std::cout << value << std::endl;
}

DoubleTrack().setup("field_name", "window_name", test, 4.5);

#1


2  

No, there is not. Unfortunately, the way the trackbar is implemented (with or without Qt) it does not support that.

不,没有。不幸的是,trackbar的实现方式(有或没有Qt)并不支持这一点。

#2


0  

AFAIK there is not possibility to pass double or other floating point value but still if you are okay let's say with 0.001 precision you can create a trackbar with max value 1000 and then simply treat its value as a double betwen 0 and 1 by dividing trackbar value by max value.

AFAIK没有可能通过双或其他浮点值但是如果你好的假设0.001精度可以创建一个trackbar马克斯价值1000,然后简单地把它的值作为双在0和1 trackbar值除以最大价值。

#3


0  

Like already stated, this is not possible. Here is a little wrapper which calculates double values for you conveniently:

如前所述,这是不可能的。这里有一个小包装器,可以方便地为您计算双值:

class DoubleTrack{
public:

    int int_value = 0;
    double precision;
    void(*user_callback)(double);

    void setup(const std::string& field_name, const std::string& window_name, void(*function)(double), double max_value, double default_value = 0, unsigned precision = 100){
            int_value = default_value * precision;
            createTrackbar(field_name, window_name, &int_value, max_value * precision, DoubleTrack::callback, this);
            user_callback = function;
            this->precision = precision;
    }

    static void callback(int, void* object){
        DoubleTrack* pObject = static_cast<DoubleTrack*>(object);
        pObject->user_callback(pObject->int_value / pObject->precision);
    }

};

static void test(double value){
    std::cout << value << std::endl;
}

DoubleTrack().setup("field_name", "window_name", test, 4.5);