diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2018-01-12 19:40:22 +0100 | 
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2018-01-12 19:40:22 +0100 | 
| commit | 537a88e522314753149e1ea7921489e58043b40c (patch) | |
| tree | a62ac59a546c8b0ee8adeee3bd2aa255d7e4570e /cv | |
| parent | 11d8e48e4370c9201f8258b418aadc7a4290dba1 (diff) | |
tracker/pt: isolate point extractor and image type
Issue: #718
Diffstat (limited to 'cv')
| -rw-r--r-- | cv/video-widget.cpp | 54 | ||||
| -rw-r--r-- | cv/video-widget.hpp | 13 | 
2 files changed, 46 insertions, 21 deletions
| diff --git a/cv/video-widget.cpp b/cv/video-widget.cpp index bad81905..4c9a73e6 100644 --- a/cv/video-widget.cpp +++ b/cv/video-widget.cpp @@ -9,11 +9,13 @@  #include "video-widget.hpp"  #include "compat/check-visible.hpp" +#include "compat/util.hpp" + +#include <cstring> +  #include <opencv2/imgproc.hpp> -cv_video_widget::cv_video_widget(QWidget* parent) : QWidget(parent), -    mtx(QMutex::Recursive), -    freshp(false) +cv_video_widget::cv_video_widget(QWidget* parent) : QWidget(parent)  {      connect(&timer, SIGNAL(timeout()), this, SLOT(update_and_repaint()), Qt::DirectConnection);      timer.start(65); @@ -25,9 +27,7 @@ void cv_video_widget::update_image(const cv::Mat& frame)      if (!freshp)      { -        const int w = preview_size.width(), h = preview_size.height(); - -        if (w < 1 || h < 1) +        if (width < 1 || height < 1)              return;          if (_frame.cols != frame.cols || _frame.rows != frame.rows) @@ -38,28 +38,50 @@ 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_8UC4); -        if (_frame3.cols != w || _frame3.rows != h) -            _frame3 = cv::Mat(h, w, CV_8UC4); +        if (_frame3.cols != width || _frame3.rows != height) +            _frame3 = cv::Mat(height, width, CV_8UC4);          cv::cvtColor(_frame, _frame2, cv::COLOR_BGR2BGRA); -        const cv::Mat* img_; +        const cv::Mat* img; -        if (_frame.cols != w || _frame.rows != h) +        if (_frame.cols != width || _frame.rows != height)          { -            cv::resize(_frame2, _frame3, cv::Size(w, h), 0, 0, cv::INTER_NEAREST); +            cv::resize(_frame2, _frame3, cv::Size(width, height), 0, 0, cv::INTER_NEAREST); -            img_ = &_frame3; +            img = &_frame3;          }          else -            img_ = &_frame2; +            img = &_frame2; + +        const unsigned nbytes = 4 * img->rows * img->cols; + +        vec.resize(nbytes); -        const cv::Mat& img = *img_; +        std::memcpy(vec.data(), img->data, nbytes); -        texture = QImage((const unsigned char*) img.data, w, h, QImage::Format_ARGB32); +        texture = QImage((const unsigned char*) vec.data(), width, height, QImage::Format_ARGB32);      }  } +void cv_video_widget::update_image(const QImage& img) +{ +    QMutexLocker l(&mtx); + +    if (freshp) +        return; + +    const unsigned nbytes = img.sizeInBytes(); + +    vec.resize(nbytes); + +    std::memcpy(vec.data(), img.constBits(), nbytes); + +    texture = QImage((const unsigned char*) vec.data(), img.width(), img.height(), img.format()); + +    freshp = true; +} +  void cv_video_widget::paintEvent(QPaintEvent*)  {      QMutexLocker foo(&mtx); @@ -74,8 +96,6 @@ 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 5dd70815..7faddb49 100644 --- a/cv/video-widget.hpp +++ b/cv/video-widget.hpp @@ -9,6 +9,7 @@  #pragma once  #include <memory> +#include <vector>  #include <opencv2/core.hpp> @@ -25,17 +26,21 @@  class cv_video_widget final : public QWidget  {      Q_OBJECT +  public:      cv_video_widget(QWidget *parent); -    void update_image(const cv::Mat &frame); +    void update_image(const cv::Mat& frame); +    void update_image(const QImage& image); + +    static constexpr inline int width = 320, height = 240;  protected slots:      void paintEvent(QPaintEvent*) override;      void update_and_repaint();  private: -    QMutex mtx; +    QMutex mtx { QMutex::Recursive };      QImage texture; +    std::vector<unsigned char> vec;      QTimer timer; -    QSize preview_size;      cv::Mat _frame, _frame2, _frame3; -    bool freshp; +    bool freshp = false;  }; | 
