From 946252efabf79792aac655914276aab3fad36ef2 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 21 Feb 2017 20:39:43 +0100 Subject: cv/video-widget: fix logic error Also try not to poke size() outside UI thread. --- cv/video-widget.cpp | 25 ++++++++++++++++++++----- cv/video-widget.hpp | 2 ++ 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'cv') diff --git a/cv/video-widget.cpp b/cv/video-widget.cpp index f45830b8..ce3f3b47 100644 --- a/cv/video-widget.cpp +++ b/cv/video-widget.cpp @@ -31,14 +31,27 @@ void cv_video_widget::update_image(const cv::Mat& frame) if (_frame2.cols != _frame.cols || _frame2.rows != _frame.rows) _frame2 = cv::Mat(_frame.rows, _frame.cols, CV_8UC3); - if (_frame3.cols != width() || _frame3.rows != height()) - _frame3 = cv::Mat(height(), width(), CV_8UC3); + const int w = preview_size.width(), h = preview_size.height(); + + if (_frame3.cols != w || _frame3.rows != h) + _frame3 = cv::Mat(h, w, CV_8UC3); cv::cvtColor(_frame, _frame2, cv::COLOR_RGB2BGR); - if (_frame.cols != width() || _frame.rows || height()) - cv::resize(_frame2, _frame3, cv::Size(width(), height()), 0, 0, cv::INTER_NEAREST); - texture = QImage((const unsigned char*) _frame3.data, _frame3.cols, _frame3.rows, QImage::Format_RGB888); + const cv::Mat* img_; + + if (_frame.cols != w || _frame.rows != h) + { + cv::resize(_frame2, _frame3, cv::Size(w, h), 0, 0, cv::INTER_NEAREST); + + img_ = &_frame3; + } + else + img_ = &_frame2; + + const cv::Mat& img = *img_; + + texture = QImage((const unsigned char*) img.data, img.cols, img.rows, QImage::Format_RGB888); } } @@ -53,6 +66,8 @@ void cv_video_widget::update_and_repaint() { QMutexLocker l(&mtx); + preview_size = size(); + if (freshp) { freshp = false; diff --git a/cv/video-widget.hpp b/cv/video-widget.hpp index 3e328977..3b29ffc6 100644 --- a/cv/video-widget.hpp +++ b/cv/video-widget.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #include class cv_video_widget final : public QWidget @@ -32,6 +33,7 @@ private: QMutex mtx; QImage texture; QTimer timer; + QSize preview_size; cv::Mat _frame, _frame2, _frame3; bool freshp; }; -- cgit v1.2.3