forked from skaringa/emeocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageProcessor.cpp
274 lines (232 loc) · 7.65 KB
/
ImageProcessor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* ImageProcessor.cpp
*
*/
#include <vector>
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>
#include "ImageProcessor.h"
#include "Config.h"
/**
* Functor to help sorting rectangles by their x-position.
*/
class sortRectByX {
public:
bool operator()(cv::Rect const & a, cv::Rect const & b) const {
return a.x < b.x;
}
};
ImageProcessor::ImageProcessor(const Config & config) :
_config(config), _debugWindow(false), _debugSkew(false), _debugDigits(false), _debugEdges(false) {
}
/**
* Set the input image.
*/
void ImageProcessor::setInput(cv::Mat & img) {
_img = img;
}
/**
* Get the vector of output images.
* Each image contains the edges of one isolated digit.
*/
const std::vector<cv::Mat> & ImageProcessor::getOutput() {
return _digits;
}
void ImageProcessor::debugWindow(bool bval) {
_debugWindow = bval;
if (_debugWindow) {
cv::namedWindow("ImageProcessor");
}
}
void ImageProcessor::debugSkew(bool bval) {
_debugSkew = bval;
}
void ImageProcessor::debugEdges(bool bval) {
_debugEdges = bval;
}
void ImageProcessor::debugDigits(bool bval) {
_debugDigits = bval;
}
void ImageProcessor::showImage() {
cv::imshow("ImageProcessor", _img);
cv::waitKey(1);
}
/**
* Main processing function.
* Read input image and create vector of images for each digit.
*/
void ImageProcessor::process() {
_digits.clear();
// convert to gray
cvtColor(_img, _imgGray, CV_BGR2GRAY);
// initial rotation to get the digits up
rotate(_config.getRotationDegrees());
// detect and correct remaining skew (+- 30 deg)
float skew_deg = detectSkew();
rotate(skew_deg);
// find and isolate counter digits
findCounterDigits();
if (_debugWindow) {
showImage();
}
}
/**
* Rotate image.
*/
void ImageProcessor::rotate(double rotationDegrees) {
cv::Mat M = cv::getRotationMatrix2D(cv::Point(_imgGray.cols / 2, _imgGray.rows / 2), rotationDegrees, 1);
cv::Mat img_rotated;
cv::warpAffine(_imgGray, img_rotated, M, _imgGray.size());
_imgGray = img_rotated;
if (_debugWindow) {
cv::warpAffine(_img, img_rotated, M, _img.size());
_img = img_rotated;
}
}
/**
* Draw lines into image.
* For debugging purposes.
*/
void ImageProcessor::drawLines(std::vector<cv::Vec2f>& lines) {
// draw lines
for (size_t i = 0; i < lines.size(); i++) {
float rho = lines[i][0];
float theta = lines[i][1];
double a = cos(theta), b = sin(theta);
double x0 = a * rho, y0 = b * rho;
cv::Point pt1(cvRound(x0 + 1000 * (-b)), cvRound(y0 + 1000 * (a)));
cv::Point pt2(cvRound(x0 - 1000 * (-b)), cvRound(y0 - 1000 * (a)));
cv::line(_img, pt1, pt2, cv::Scalar(255, 0, 0), 1);
}
}
/**
* Draw lines into image.
* For debugging purposes.
*/
void ImageProcessor::drawLines(std::vector<cv::Vec4i>& lines, int xoff, int yoff) {
for (size_t i = 0; i < lines.size(); i++) {
cv::line(_img, cv::Point(lines[i][0] + xoff, lines[i][1] + yoff),
cv::Point(lines[i][2] + xoff, lines[i][3] + yoff), cv::Scalar(255, 0, 0), 1);
}
}
/**
* Detect the skew of the image by finding almost (+- 30 deg) horizontal lines.
*/
float ImageProcessor::detectSkew() {
log4cpp::Category& rlog = log4cpp::Category::getRoot();
cv::Mat edges = cannyEdges();
// find lines
std::vector<cv::Vec2f> lines;
cv::HoughLines(edges, lines, 1, CV_PI / 180.f, 140);
// filter lines by theta and compute average
std::vector<cv::Vec2f> filteredLines;
float theta_min = 60.f * CV_PI / 180.f;
float theta_max = 120.f * CV_PI / 180.0f;
float theta_avr = 0.f;
float theta_deg = 0.f;
for (size_t i = 0; i < lines.size(); i++) {
float theta = lines[i][1];
if (theta >= theta_min && theta <= theta_max) {
filteredLines.push_back(lines[i]);
theta_avr += theta;
}
}
if (filteredLines.size() > 0) {
theta_avr /= filteredLines.size();
theta_deg = (theta_avr / CV_PI * 180.f) - 90;
rlog.info("detectSkew: %.1f deg", theta_deg);
} else {
rlog.warn("failed to detect skew");
}
if (_debugSkew) {
drawLines(filteredLines);
}
return theta_deg;
}
/**
* Detect edges using Canny algorithm.
*/
cv::Mat ImageProcessor::cannyEdges() {
cv::Mat edges;
// detect edges
cv::Canny(_imgGray, edges, _config.getCannyThreshold1(), _config.getCannyThreshold2());
return edges;
}
/**
* Find bounding boxes that are aligned at y position.
*/
void ImageProcessor::findAlignedBoxes(std::vector<cv::Rect>::const_iterator begin,
std::vector<cv::Rect>::const_iterator end, std::vector<cv::Rect>& result) {
std::vector<cv::Rect>::const_iterator it = begin;
cv::Rect start = *it;
++it;
result.push_back(start);
for (; it != end; ++it) {
if (abs(start.y - it->y) < _config.getDigitYAlignment() && abs(start.height - it->height) < 5) {
result.push_back(*it);
}
}
}
/**
* Filter contours by size of bounding rectangle.
*/
void ImageProcessor::filterContours(std::vector<std::vector<cv::Point> >& contours,
std::vector<cv::Rect>& boundingBoxes, std::vector<std::vector<cv::Point> >& filteredContours) {
// filter contours by bounding rect size
for (size_t i = 0; i < contours.size(); i++) {
cv::Rect bounds = cv::boundingRect(contours[i]);
if (bounds.height > _config.getDigitMinHeight() && bounds.height < _config.getDigitMaxHeight()
&& bounds.width > 5 && bounds.width < bounds.height) {
boundingBoxes.push_back(bounds);
filteredContours.push_back(contours[i]);
}
}
}
/**
* Find and isolate the digits of the counter,
*/
void ImageProcessor::findCounterDigits() {
log4cpp::Category& rlog = log4cpp::Category::getRoot();
// edge image
cv::Mat edges = cannyEdges();
if (_debugEdges) {
cv::imshow("edges", edges);
}
cv::Mat img_ret = edges.clone();
// find contours in whole image
std::vector<std::vector<cv::Point> > contours, filteredContours;
std::vector<cv::Rect> boundingBoxes;
cv::findContours(edges, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
// filter contours by bounding rect size
filterContours(contours, boundingBoxes, filteredContours);
rlog << log4cpp::Priority::INFO << "number of filtered contours: " << filteredContours.size();
// find bounding boxes that are aligned at y position
std::vector<cv::Rect> alignedBoundingBoxes, tmpRes;
for (std::vector<cv::Rect>::const_iterator ib = boundingBoxes.begin(); ib != boundingBoxes.end(); ++ib) {
tmpRes.clear();
findAlignedBoxes(ib, boundingBoxes.end(), tmpRes);
if (tmpRes.size() > alignedBoundingBoxes.size()) {
alignedBoundingBoxes = tmpRes;
}
}
rlog << log4cpp::Priority::INFO << "max number of alignedBoxes: " << alignedBoundingBoxes.size();
// sort bounding boxes from left to right
std::sort(alignedBoundingBoxes.begin(), alignedBoundingBoxes.end(), sortRectByX());
if (_debugEdges) {
// draw contours
cv::Mat cont = cv::Mat::zeros(edges.rows, edges.cols, CV_8UC1);
cv::drawContours(cont, filteredContours, -1, cv::Scalar(255));
cv::imshow("contours", cont);
}
// cut out found rectangles from edged image
for (int i = 0; i < alignedBoundingBoxes.size(); ++i) {
cv::Rect roi = alignedBoundingBoxes[i];
_digits.push_back(img_ret(roi));
if (_debugDigits) {
cv::rectangle(_img, roi, cv::Scalar(0, 255, 0), 2);
}
}
}