Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not set trackBarPos, if two trackbar in the same window #25537

Closed
RichardZeenChen opened this issue May 3, 2024 · 7 comments
Closed

Can not set trackBarPos, if two trackbar in the same window #25537

RichardZeenChen opened this issue May 3, 2024 · 7 comments

Comments

@RichardZeenChen
Copy link

System Information

OpenCV version: 4.8.0
Operating System / Platform: Win 10
Compiler & compiler version: vs2019 vc16

Detailed description

When you uncomment code setTrackbarPos("kernel", "dst", 3);, the program will report an error during runtime

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

void gaussianBlurCall(int pos, void* userdata) {
    Mat* img = static_cast<Mat*>(userdata);
    int k = getTrackbarPos("kernel", "dst");
    if (k % 2 == 0) {
        k = k - 1;
    }
    int sigma = getTrackbarPos("sigma", "dst");
    Mat dst;
    GaussianBlur(*img, dst, Size(k, k), sigma);
    imshow("dst", dst);
}

int main(int argc, char** argv)
{
    Mat src = imread("blur.tif");
    namedWindow("dst", WINDOW_NORMAL);
    
    createTrackbar("kernel", "dst", nullptr, 21, gaussianBlurCall, &src);
    setTrackbarMin("kernel", "dst", 1);
    //setTrackbarPos("kernel", "dst", 3);
    
    createTrackbar("sigma", "dst", nullptr, 10, gaussianBlurCall, &src);
    setTrackbarMin("sigma", "dst", 1);
    //setTrackbarPos("sigma", "dst", 7);
    waitKey();

    return 0;
}


### Steps to reproduce

```cpp
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

void gaussianBlurCall(int pos, void* userdata) {
    Mat* img = static_cast<Mat*>(userdata);
    int k = getTrackbarPos("kernel", "dst");
    if (k % 2 == 0) {
        k = k - 1;
    }
    int sigma = getTrackbarPos("sigma", "dst");
    Mat dst;
    GaussianBlur(*img, dst, Size(k, k), sigma);
    imshow("dst", dst);
}

int main(int argc, char** argv)
{
    Mat src = imread("blur.tif");
    namedWindow("dst", WINDOW_NORMAL);
    
    createTrackbar("kernel", "dst", nullptr, 21, gaussianBlurCall, &src);
    setTrackbarMin("kernel", "dst", 1);
    //setTrackbarPos("kernel", "dst", 3); //you can not uncomment this code
    
    createTrackbar("sigma", "dst", nullptr, 10, gaussianBlurCall, &src);
    setTrackbarMin("sigma", "dst", 1);
    //setTrackbarPos("sigma", "dst", 7); //you can not uncomment this code
    waitKey();

	return 0;
}

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [ ] I updated to the latest OpenCV version and the issue is still there
- [ ] There is reproducer code and related data files (videos, images, onnx, etc)
@LaurentBerger
Copy link
Contributor

LaurentBerger commented May 3, 2024

There is no exception if img is not empty.

Mat src = imread("blur.tif");
if (src.empty())
{
    cout << "Check image file path";
    return 0;
}

Did you checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution? NO

@RichardZeenChen
Copy link
Author

I am sure that src is not empty. When I comment code setTrackbarPos("kernel", "dst", 3); and setTrackbarPos("sigma", "dst", 7);, the program will run successfully. However, I can not set trackbar pos, when two or more trackbar in the same window.

@asmorkalov
Copy link
Contributor

@RichardZeenChen Does the sample work well for you?

@RichardZeenChen
Copy link
Author

The program work well, only if you do not set the trackbarPos. Can not set trackBarPos, if two trackbar in the same window. Hower, the code work well if only one trackbar in the window.

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

void gaussianBlurCall(int pos, void* userdata) {
    Mat* img = static_cast<Mat*>(userdata);
    int k = getTrackbarPos("kernel", "dst");
    if (k % 2 == 0) {
        k = k - 1;
    }
    int sigma = getTrackbarPos("sigma", "dst");
    Mat dst;
    GaussianBlur(*img, dst, Size(k, k), sigma);
    imshow("dst", dst);
}

int main(int argc, char** argv)
{
    Mat src = imread("blur.tif");
    namedWindow("dst", WINDOW_NORMAL);
    
    createTrackbar("kernel", "dst", nullptr, 21, gaussianBlurCall, &src);
    setTrackbarMin("kernel", "dst", 1);
    setTrackbarPos("kernel", "dst", 3); //can not set pos
    
    createTrackbar("sigma", "dst", nullptr, 10, gaussianBlurCall, &src);
    setTrackbarMin("sigma", "dst", 1);
    setTrackbarPos("sigma", "dst", 7); //can not set pos
    waitKey();

    return 0;
}

@anshulchou-123
Copy link

@RichardZeenChen when we call the first setTrackbarPos("kernel", "dst", 3); interanlly it call the gaussianBlurCall which tries to get the value of "sigma" trackbar even before we created it using createTrackbar("sigma", "dst", nullptr, 10, gaussianBlurCall, &src); hence causing it to crash.

@alexlyulkov
Copy link
Contributor

Agree with @anshulchou-123. The provided code runs getTrackbarPos("sigma"...) before createTrackbar("sigma"...) which causes the crash. The code works fine if you run setTrackbarPos after creating both the trackbars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants