Is it possible to also update other windows when a trackbar on a certain window is changed?

Only for sharing if you want to see the code in C++.

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
const string wndInput = "Input";
const string wndGrayed = "Grayed";
const string wndBlurred = "Blurred";
const string wndCannied = "Cannied";
const string tbKSize = "KSize";
const string tbThd1 = "Threshold1";
const string tbThd2 = "Threshold2";
const int KSIZE_MAX = 200;
const int THD1_MAX = 200;
const int THD2_MAX = 200;
const string path = "../resources/test.png";
Mat input = imread(path);
Mat grayed;
Mat blurred;
Mat cannied;
void Refresh()
{
    int kSize = getTrackbarPos(tbKSize, wndBlurred);
    int thd1 = getTrackbarPos(tbThd1, wndCannied);
    int thd2 = getTrackbarPos(tbThd2, wndCannied);
    if (kSize % 2 != 0)
        // size must be odd integers.
        GaussianBlur(input, blurred, cv::Size(kSize, kSize), 3, 0);
    Canny(blurred, cannied, thd1, thd2);
    imshow(wndBlurred, blurred);
    imshow(wndCannied, cannied);
}
void onThd1(int, void *)
{
    Refresh();
}
void onThd2(int, void *)
{
    Refresh();
}
void onKSize(int, void *)
{
    Refresh();
}
int main()
{
    namedWindow(wndInput);
    namedWindow(wndGrayed);
    namedWindow(wndBlurred);
    namedWindow(wndCannied);
    cvtColor(input, grayed, cv::COLOR_BGR2GRAY);
    int kSize = 71;
    int thd1 = 50;
    int thd2 = 100;
    createTrackbar(tbKSize, wndBlurred, &kSize, KSIZE_MAX, onKSize);
    createTrackbar(tbThd1, wndCannied, &thd1, THD1_MAX, onThd1);
    createTrackbar(tbThd2, wndCannied, &thd2, THD2_MAX, onThd2);
    Refresh();
    imshow(wndInput, input);
    imshow(wndGrayed, grayed);
    waitKey();
    cout << "===== END =====" << endl;
    return 0;
}
1 Like