Skip to content

Instantly share code, notes, and snippets.

@burak-yildizoz
Created December 19, 2020 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burak-yildizoz/aba8072ddc7c261a0b375738959616ef to your computer and use it in GitHub Desktop.
Save burak-yildizoz/aba8072ddc7c261a0b375738959616ef to your computer and use it in GitHub Desktop.
OpenCV createTrackbar lambda callback function
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp> // putText, CV_RGB
#include <tuple>
#include <string> // to_string
int main() {
cv::Mat src = cv::Mat(480, 640, CV_8UC3, CV_RGB(0, 0, 0));
cv::Mat dst;
int num = 5;
typedef std::tuple<const cv::Mat&, cv::Mat&, int&> tup_t;
tup_t tup(src, dst, num);
cv::TrackbarCallback cv_cb = [](int, void* userdata) {
tup_t& tup = *(tup_t*)userdata;
const cv::Mat& src = std::get<0>(tup);
cv::Mat& dst = std::get<1>(tup);
int& num = std::get<2>(tup);
dst = src.clone();
cv::putText(dst, std::to_string(num), cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 1.0, CV_RGB(255, 0, 0));
};
cv_cb(0, &tup); // initialize dst
const char* winname = "result";
cv::namedWindow(winname);
cv::createTrackbar("NUM", winname, &num, 10, cv_cb, &tup);
char ch = 0;
while (ch != 27) { // ESC is pressed
cv::imshow(winname, dst);
ch = cv::waitKey(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment