Ask Your Question

Revision history [back]

You need to create a thread loop, initialise the windows in the thread (best just before the loop starts) and update your windows from within the loop.

Here is an example, this thread loop is run from a detached thread (use mutex's to lock data passed into the loop and unlock accordingly)

void ImageViewer::processImageQueue()
{
// Initialise
cv::Mat floatImage(1230, 1204, CV_32FC3);
cv::Mat temp;
cv::Mat image;

sliderSelection = 0;

cv::namedWindow(m_windowName, cv::WINDOW_AUTOSIZE);
cv::createTrackbar("Exposure Time", m_windowName, &sliderSelection, maxSelection, onTrackbar);

ourself = this;
onTrackbar(sliderSelection, 0);


// Main thread loop
while (true)
{
    int imagesQueued = 0;
    {
        std::lock_guard<std::mutex> guard(m_imageQueueMutex);
        imagesQueued = m_imageQueue.size();
    }

    if (imagesQueued < 1)
    {
        continue;
    }

    {
        std::lock_guard<std::mutex> guard(m_imageQueueMutex);
        image = m_imageQueue[0];
        m_imageQueue.pop_front();
    }

    image.convertTo(temp, CV_32FC1);

    double min, max;
    cv::minMaxLoc(temp, &min, &max);

    temp = (temp - min) / (max - min);

    cv::cvtColor(temp, floatImage, CV_GRAY2BGR);

    cv::putText(floatImage, std::to_string(framesPerSecond()) + " fps", cv::Point(32, 32), 0, 1.0, cv::Scalar(0.0, 0.0, 1.0));
    cv::putText(floatImage, std::to_string(imagesQueued - 1) + " queued image(s)", cv::Point(32, 64), 0, 1.0, cv::Scalar(1.0, 0.0, 0.0));

    cv::imshow(m_windowName, floatImage);
    cv::waitKey(1);
}
}