ImGraph
 All Classes Functions Variables Enumerations Pages
ImagePreview.h
1 #ifndef _IMAGE_PREVIEW_HEADER_
2 #define _IMAGE_PREVIEW_HEADER_
3 
4 #ifdef _WIN32
5 #pragma warning(push)
6 #pragma warning(disable:4996 4251 4244 4275 4800)
7 #endif
8 #include <QGraphicsView>
9 #include <QResizeEvent>
10 #include <QDialog >
11 #include <QPainter>
12 #include <QRect>
13 #include <QString>
14 #include <QDialog>
15 #include <QLabel>
16 #include <QWidget>
17 #include <QLayout>
18 #include <QComboBox>
19 #include <QDial>
20 #include <QCheckBox>
21 #include <QPainterPath>
22 #include <QBasicTimer>
23 
24 #include <opencv2/core.hpp>
25 #include <opencv2/imgproc.hpp>
26 
27 #include <iostream>
28 #include "Convertor.h"
29 #ifdef _WIN32
30 #pragma warning(pop)
31 #endif
32 
33 namespace charliesoft
34 {
35  class Converter : public QObject {
36  Q_OBJECT;
37  QBasicTimer _timer;
38  cv::Mat _frame;
39  bool _processAll;
40  static void matDeleter(void* mat) { delete static_cast<cv::Mat*>(mat); }
41  void queue(const cv::Mat & frame) {
42  _frame = frame;
43  if (!_timer.isActive()) _timer.start(0, this);
44  }
45  void process(cv::Mat frame) {
46  //optimisation???
47  //if (frame.cols>9 && frame.rows>9)
48  // cv::resize(frame, frame, cv::Size(), 0.3, 0.3, cv::INTER_AREA);
49 
50  frame = MatrixConvertor::convert(frame, CV_8UC3);
51 
52  cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);
53  const QImage image(frame.data, frame.cols, frame.rows, frame.step,
54  QImage::Format_RGB888, &matDeleter, new cv::Mat(frame));
55  emit imageReady(image);
56 
57  frame = cv::Mat();
58  }
59  void timerEvent(QTimerEvent * ev) {
60  if (ev->timerId() == _timer.timerId())
61  {
62  process(_frame);
63  _frame.release();
64  _timer.stop();
65  }
66  }
67  public:
68  explicit Converter(QObject * parent = 0) : QObject(parent), _processAll(false) {}
69  void setProcessAll(bool all) { _processAll = all; }
70  Q_SIGNAL void imageReady(const QImage &);
71  void processFrame(const cv::Mat & frame) {
72  if (_processAll) process(frame); else queue(frame);
73  }
74  };
75 
76  class ImageViewer : public QWidget {
77  Q_OBJECT;
78  QImage _img;
79  Converter* _converter;
80  void paintEvent(QPaintEvent *) {
81  QPainter p(this);
82  p.drawImage(QRect(0, 0, width(), height()), _img, QRect(0, 0, _img.width(), _img.height()));
83  }
84  public:
85  ImageViewer(QWidget * parent = 0) : QWidget(parent) {
86  setAttribute(Qt::WA_OpaquePaintEvent);
87  _converter = new Converter();
88  connect(_converter, SIGNAL(imageReady(QImage)), SLOT(setImage(QImage)));
89  }
90 
91  void setImage(cv::Mat img)
92  {
93  _converter->processFrame(img);
94  }
95 
96  Q_SLOT void setImage(const QImage & img) {
97  _img = img;
98  update();
99  }
100  Q_SIGNAL void matReady(cv::Mat);
101 
102  protected:
103  virtual void mouseMoveEvent(QMouseEvent *event);
104 
105  virtual void enterEvent(QEvent *event);
106  virtual void leaveEvent(QEvent *event);
107  };
108 
109 }
110 
111 #endif
Definition: ImagePreview.h:76
Definition: ImagePreview.h:35