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.