Read@CVL
Image.h
Go to the documentation of this file.
1 /*******************************************************************************************************
2  ReadFramework is the basis for modules developed at CVL/TU Wien for the EU project READ.
3 
4  Copyright (C) 2016 Markus Diem <diem@caa.tuwien.ac.at>
5  Copyright (C) 2016 Stefan Fiel <fiel@caa.tuwien.ac.at>
6  Copyright (C) 2016 Florian Kleber <kleber@caa.tuwien.ac.at>
7 
8  This file is part of ReadFramework.
9 
10  ReadFramework is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  ReadFramework is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23  The READ project has received funding from the European Union’s Horizon 2020
24  research and innovation programme under grant agreement No 674943
25 
26  related links:
27  [1] http://www.caa.tuwien.ac.at/cvl/
28  [2] https://transkribus.eu/Transkribus/
29  [3] https://github.com/TUWien/
30  [4] http://nomacs.org
31  *******************************************************************************************************/
32 
33 #pragma once
34 
35 #pragma warning(push, 0) // no warnings from includes
36 #include <QSharedPointer>
37 #include <QImage>
38 #include <QString>
39 #include <QColor>
40 #include <QPen>
41 #include <QDebug>
42 
43 #include <opencv2/core.hpp>
44 #pragma warning(pop)
45 
46 #pragma warning (disable: 4251) // inlined Qt functions in dll interface
47 
48 #ifndef DllCoreExport
49 #ifdef DLL_CORE_EXPORT
50 #define DllCoreExport Q_DECL_EXPORT
51 #else
52 #define DllCoreExport Q_DECL_IMPORT
53 #endif
54 #endif
55 
56 // Qt defines
57 
58 namespace rdf {
59 
60 class Rect;
61 
63 
64 public:
65  Histogram(const cv::Mat& values = cv::Mat());
66  Histogram(double minVal, double maxVal, int numBins = 256);
67 
68  cv::Mat draw(const QPen& pen = QPen(), const QColor& bgCol = QColor(255, 255, 255));
69  void draw(QPainter& p, const Rect& r) const;
70 
71  bool isEmpty() const;
72 
73  cv::Mat hist() const;
74  double maxBin() const;
75  int maxBinIdx() const;
76  double minBin() const;
77  int numBins() const;
78  int binIdx(double val) const;
79  double value(int binIdx) const;
80 
81  void add(double val, double weight = 1.0);
82 
83  template <typename Num>
84  static Histogram fromData(const cv::Mat& data, int numBins = 256) {
85 
86  double minV = 0, maxV = 0;
87  cv::minMaxLoc(data, &minV, &maxV);
88 
89  if (minV == maxV) {
90  qWarning() << "min == max that's not good for creating a histogram - aborting";
91  return Histogram();
92  }
93 
94  Histogram h(minV, maxV, numBins);
95  float* hmp = h.hist().ptr<float>();
96 
97  for (int rIdx = 0; rIdx < data.rows; rIdx++) {
98 
99  const Num* ptr = data.ptr<Num>(rIdx);
100 
101  for (int cIdx = 0; cIdx < data.cols; cIdx++) {
102 
103  // this is inherently dangerous & fast
104  // it relies on the fact that binIdx is always within the range
105  int bin = h.binIdx(ptr[cIdx]);
106  hmp[bin]++;
107  }
108  }
109 
110  return h;
111  }
112 
113 protected:
114  void draw(QPainter& p) const;
115  double transformX(double val, const Rect& r) const;
116  double transformY(double val, double minV, double maxV, const Rect& r) const;
117 
118  cv::Mat mHist;
119  double mMinVal = 0;
120  double mMaxVal = 0;
121 };
122 
123 // read defines
127 namespace Image {
128 
129  DllCoreExport cv::Mat qImage2Mat(const QImage& img);
130  DllCoreExport QImage mat2QImage(const cv::Mat& img, bool toRGB = false);
131  //DllCoreExport cv::Mat qPixmap2Mat(const QPixmap& img); // remember: do not use QPixmap (only supported with UIs)
132  //DllCoreExport QPixmap mat2QPixmap(const cv::Mat& img);
133  DllCoreExport cv::Mat qVector2Mat(const QVector<float>& data);
134 
135  DllCoreExport QImage load(const QString& path, bool* ok = 0);
136  DllCoreExport bool save(const QImage& img, const QString& savePath, int compression = -1);
137  DllCoreExport bool save(const cv::Mat& img, const QString& savePath, int compression = -1);
138  DllCoreExport bool alphaChannelUsed(const QImage& img);
139  DllCoreExport void imageInfo(const cv::Mat& img, const QString name);
140  DllCoreExport QString printImage(const cv::Mat& img, const QString name);
141  DllCoreExport QJsonObject matToJson(const cv::Mat& img, bool compress = true);
142  DllCoreExport cv::Mat jsonToMat(const QJsonObject& jo, const QString& filePath = "");
143 
144  DllCoreExport QJsonObject matToJsonExtern(const cv::Mat& img, const QString& fileName, bool compress = true);
145  DllCoreExport bool writeMat(const cv::Mat& img, const QString& filePath, bool compress = true);
146 
147 
154  template <typename numFmt>
155  QString printMat(const cv::Mat& src, const QString varName) {
156 
157  QString msg = varName;
158  msg.append(" = ["); // matlab...
159 
160  int cnt = 0;
161 
162  for (int rIdx = 0; rIdx < src.rows; rIdx++) {
163 
164  const numFmt* srcPtr = src.ptr<numFmt>(rIdx);
165 
166  for (int cIdx = 0; cIdx < src.cols; cIdx++, cnt++) {
167 
168 
169  msg.append(QString::number(srcPtr[cIdx]));
170  msg.append( (cIdx < src.cols - 1) ? " " : "; " ); // next row matlab?
171 
172  if (cnt % 7 == 0 && cnt > 0)
173  msg.append("...\n");
174  }
175 
176  }
177  msg.append("];");
178 
179  return msg;
180  }
181 }
182 
183 }
DllCoreExport cv::Mat qVector2Mat(const QVector< float > &data)
Definition: Image.cpp:141
int binIdx(double val) const
Definition: Image.cpp:575
cv::Mat hist() const
Definition: Image.cpp:546
DllCoreExport QImage mat2QImage(const cv::Mat &img, bool toRGB=false)
Converts a cv::Mat to QImage.
Definition: Image.cpp:106
static Histogram fromData(const cv::Mat &data, int numBins=256)
Definition: Image.h:84
#define DllCoreExport
Definition: Image.h:52
DllCoreExport bool save(const cv::Mat &img, const QString &savePath, int compression=-1)
Saves the specified cv::Mat img.
Definition: Image.cpp:218
DllCoreExport QString printImage(const cv::Mat &img, const QString name)
Prints the image as a string formatted according Matlab.
Definition: Image.cpp:313
DllCoreExport bool alphaChannelUsed(const QImage &img)
Checks if the alpha channel is used.
Definition: Image.cpp:234
DllCoreExport void imageInfo(const cv::Mat &img, const QString name)
Prints the basic image information.
Definition: Image.cpp:263
DllCoreExport bool writeMat(const cv::Mat &img, const QString &filePath, bool compress=true)
Definition: Image.cpp:366
QString printMat(const cv::Mat &src, const QString varName)
Prints the values of a cv::Mat to copy it to Matlab.
Definition: Image.h:155
DllCoreExport QImage load(const QString &path, bool *ok=0)
Definition: Image.cpp:152
DllCoreExport QJsonObject matToJson(const cv::Mat &img, bool compress=true)
Definition: Image.cpp:333
Definition: Algorithms.cpp:45
Definition: Shapes.h:344
DllCoreExport cv::Mat qImage2Mat(const QImage &img)
Converts a QImage to a cv::Mat.
Definition: Image.cpp:60
cv::Mat mHist
Definition: Image.h:118
DllCoreExport cv::Mat jsonToMat(const QJsonObject &jo, const QString &filePath="")
Definition: Image.cpp:399
Definition: Image.h:62
DllCoreExport QJsonObject matToJsonExtern(const cv::Mat &img, const QString &fileName, bool compress=true)
Definition: Image.cpp:353