Read@CVL
ImageProcessor.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 <QObject>
37 #include <opencv2/core.hpp>
38 #include <opencv2/imgproc.hpp>
39 #pragma warning(pop)
40 
41 #ifndef DllCoreExport
42 #ifdef DLL_CORE_EXPORT
43 #define DllCoreExport Q_DECL_EXPORT
44 #else
45 #define DllCoreExport Q_DECL_IMPORT
46 #endif
47 #endif
48 
49 // Qt defines
50 
51 namespace cv {
52  // compare operator for keypoints
53  DllCoreExport bool operator==(const cv::KeyPoint& kpl, const cv::KeyPoint& kpr);
54 }
55 
56 namespace rdf {
57 
58 // read defines
59 class DllCoreExport IP { // basically a namespace for now
60 
61 public:
62  enum MorphShape { morph_square = 0, morph_disk };
63  enum MorphBorder { border_zero = 0, border_flip };
64 
65  // image processing
66  static cv::Mat createStructuringElement(int seSize, int shape);
67  static cv::Mat dilateImage(const cv::Mat& bwImg, int seSize, MorphShape shape = IP::morph_square, int borderValue = 0);
68  static cv::Mat erodeImage(const cv::Mat& bwImg, int seSize, MorphShape shape = IP::morph_square, int borderValue = 255);
69 
70  //static cv::Mat convolveSymmetric(const cv::Mat& hist, const cv::Mat& kernel);
71  static cv::Mat convolveIntegralImage(const cv::Mat& src, const int kernelSizeX, const int kernelSizeY = 0, MorphBorder norm = IP::border_zero);
72  static cv::Mat get1DGauss(double sigma, int kernelsize = -1);
73 
74  static cv::Mat threshOtsu(const cv::Mat& srcImg, int thType = CV_THRESH_BINARY_INV);
75  static double getThreshOtsu(const cv::Mat& hist, const double otsuThresh = 0);
76 
77  static void setBorderConst(cv::Mat &src, float val = 0.0f);
78  static void invertImg(cv::Mat& srcImg, cv::Mat mask = cv::Mat());
79  static cv::Mat preFilterArea(const cv::Mat& img, int minArea, int maxArea = -1);
80  static cv::Mat computeHist(const cv::Mat img, const cv::Mat mask = cv::Mat());
81 
82  static cv::Mat estimateMask(const cv::Mat& src, bool preFilter = true);
83  static void mulMask(cv::Mat& src, cv::Mat mask = cv::Mat());
84  static QPointF calcRotationSize(double angleRad, const QPointF& srcSize);
85  static cv::Mat rotateImage(const cv::Mat& src, double angleRad, int interpolation = cv::INTER_CUBIC, cv::Scalar borderValue = cv::Scalar(0));
86 
87  static cv::Mat invert(const cv::Mat& src);
88  static cv::Mat grayscale(const cv::Mat& src);
89 
90  static cv::Mat computeHist(const cv::Mat& data, int width, int numElements = -1, double* maxBin = 0);
91  static void draw(const std::vector<cv::Point>& pts, cv::Mat& img, unsigned char val = 255);
92 
93  static double statMomentMat(const cv::Mat& src, const cv::Mat& mask = cv::Mat(), double momentValue = 0.5, int maxSamples = 10000, int area = -1);
94  static QColor statMomentColor(const cv::Mat& src, const cv::Mat& mask = cv::Mat(), double momentValue = 0.5);
95 
96  static void normalize(cv::Mat& src);
97 
98 private:
99  template<typename sFmt, typename mFmt>
100  static void mulMaskIntern(cv::Mat src, const cv::Mat mask) {
101 
102  sFmt* srcPtr = (sFmt*)src.data;
103  const mFmt* mPtr = (mFmt*)mask.data;
104 
105  int srcStep = (int)src.step / sizeof(sFmt);
106  int mStep = (int)mask.step / sizeof(mFmt);
107 
108  for (int rIdx = 0; rIdx < src.rows; rIdx++, srcPtr += srcStep, mPtr += mStep) {
109 
110  for (int cIdx = 0; cIdx < src.cols; cIdx++) {
111 
112  if (mPtr[cIdx] == 0) srcPtr[cIdx] = 0;
113  }
114  }
115  }
116 
117  template<typename sFmt>
118  static void setBorderConstIntern(cv::Mat src, sFmt val) {
119 
120  sFmt* srcPtr = (sFmt*)src.data;
121  sFmt* srcPtr2 = (sFmt*)src.ptr<sFmt*>(src.rows - 1);
122  int srcStep = (int)src.step / sizeof(sFmt);
123 
124  for (int cIdx = 0; cIdx < src.cols; cIdx++) {
125  srcPtr[cIdx] = val;
126  srcPtr2[cIdx] = val;
127  }
128 
129  srcPtr = (sFmt*)src.data;
130  for (int rIdx = 0; rIdx < src.rows; rIdx++, srcPtr += srcStep) {
131  srcPtr[0] = val;
132  srcPtr[src.cols - 1] = val;
133  }
134  }
135 };
136 
137 }
#define DllCoreExport
Definition: ImageProcessor.h:45
Definition: Drawer.h:53
bool operator==(const cv::KeyPoint &kpl, const cv::KeyPoint &kpr)
Definition: ImageProcessor.cpp:45
MorphShape
Definition: ImageProcessor.h:62
Definition: ImageProcessor.h:59
Definition: Algorithms.cpp:45
MorphBorder
Definition: ImageProcessor.h:63