imagepro  2019 march
Biblioteca imagepro
imagepro.h
Ir a la documentación de este archivo.
1 
25 #ifndef IMAGEPRO_H
26 #define IMAGEPRO_H
27 
28 #include <QtWidgets>
29 #include "opencv2/opencv.hpp"
30 #include "opencv2/highgui/highgui.hpp"
31 
32 namespace
33 {
34 inline QImage mat_2_qimage_ref_policy(cv::Mat &mat, QImage::Format format)
35 {
36  return QImage(mat.data, mat.cols, mat.rows, mat.step, format);
37 }
38 
39 inline QImage mat_2_qimage_cpy_policy(cv::Mat const &mat, QImage::Format format)
40 {
41  return QImage(mat.data, mat.cols, mat.rows, mat.step, format).copy();
42 }
43 
47 struct qimage_2_mat_cpy_policy
48 {
49  static cv::Mat start(QImage const &img, int format)
50  {
51  return cv::Mat(img.height(), img.width(), format, const_cast<uchar*>(img.bits()), img.bytesPerLine()).clone();
52  }
53 };
54 
60 struct qimage_2_mat_ref_policy
61 {
62  static cv::Mat start(QImage &img, int format)
63  {
64  return cv::Mat(img.height(), img.width(), format, img.bits(), img.bytesPerLine());
65  }
66 };
67 
71 template<typename Policy>
72 struct qimage_2_mat
73 {
74  template<typename Image>
75  static cv::Mat run(Image &img, bool swap);
76 };
77 
83 template<typename Policy>
84 template<typename Image>
85 cv::Mat qimage_2_mat<Policy>::run(Image &img, bool swap)
86 {
87  if(img.isNull()){
88  return cv::Mat();
89  }
90 
91  switch (img.format()) {
92  case QImage::Format_RGB888:{
93  cv::Mat result = Policy::start(img, CV_8UC3);
94  if(swap){
95  cv::cvtColor(result, result, CV_RGB2BGR);
96  }
97  return result;
98  }
99  case QImage::Format_Indexed8:{
100  return Policy::start(img, CV_8U);
101  }
102  case QImage::Format_RGB32:
103  case QImage::Format_ARGB32:
104  case QImage::Format_ARGB32_Premultiplied:{
105  return Policy::start(img, CV_8UC4);
106  }
107  default:
108  break;
109  }
110 
111  return cv::Mat();
112 }
113 }
114 
115 using namespace cv;
116 
117 class ImagePro
118 {
119 
120 public:
121  ImagePro();
122 
123  QImage mat_2_qimage_cpy(Mat const &mat, bool swap);
124  Mat qimage_2_mat_cpy(QImage const &img, bool swap);
125 };
126 
127 #endif // IMAGEPRO_H
Definition: imagepro.h:117