-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
289 lines (232 loc) · 12.7 KB
/
Copy pathmain.cpp
File metadata and controls
289 lines (232 loc) · 12.7 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <iostream>
#include "main.h"
#include <ctime>
Scalar myRed(Scalar(0, 0, 255));
Scalar myGreen(Scalar(0, 255, 0));
Scalar myBlue(Scalar(255, 0, 0));
Scalar myPink(Scalar(255, 0, 255));
Scalar myWhite(Scalar(255, 255, 255));
Scalar myWhite2(Scalar(255, 255, 255, 128));
Scalar myGrey(Scalar(128, 128, 128, 0.5));
int main() {
Mat img = imread("../examples/cn_2500_1000_500_500/cn_2500_1000_500_500.jpg");
Mat markedImg = imread("../examples/cn_2500_1000_500_500/cnMarking_2500_1000_500_500.png");
vectorizeImgWithSeveralParameters(img, markedImg, {1}, {1, 2}, {2}, "../newDir/");
}
/*!
* векторизуем изображение с одинарным комплектом параметров
* @param img - исходное изображение
* @param img - изображение того же участка, но с ручной разметкой
* @param dpEps - параметр удаленности для алгоритма Дугласа-Пекера
* @param gridInterval - интервал между точками вспомогательной сетки алгоритма Грибова
* @param ppc - количество рассматриваемых предыдущих точек в алгоритме Грибова
* @param dirPath - путь к папке, в которой будут лежать результаты (на конце слэш) (папка уже должна быть создана)
* @return векторизованные контуры
*/
vector<vector<Point>> vectorizeImg(const Mat& img, const Mat& markedImg, int dpEps, int gridInterval, int ppc, const String& dirPath) {
String commonPathPart = "_dpEps_" + to_string(dpEps) + "_interval_" + to_string(gridInterval) + "_ppc_" + to_string(ppc) + ".jpg";
String resultImgPath = dirPath + commonPathPart;
String onlyContoursImgPath = dirPath + "only_contours" + commonPathPart;
String resultWithMarkingPath = dirPath + "with_marking" + commonPathPart;
String iouPath = dirPath + "iou.txt";
String timePath = dirPath + "time.txt";
ofstream iouOut;
iouOut.open(iouPath, ofstream::out);
iouOut << "Initial IoU " << getIou(img, markedImg) << endl;
ofstream timeOut;
timeOut.open(timePath, ofstream::out);
// они не сильно влияют, пусть будут дефолтными
int gridStartX = 0;
int gridStartY = 0;
// применяем дилатацию к исходному изображению
Mat dilatedContoursImg;
dilate(img, dilatedContoursImg, Mat());
// применяем эрозию к дилатированному изображению
Mat erodedContoursImg;
erode(dilatedContoursImg, erodedContoursImg, Mat());
// ищем контуры на дилатированно-эрозированном изображении
Mat contoursImg;
Canny(erodedContoursImg, contoursImg, 100, 255);
// применяем дилатацию к контурам
Mat dilatedContoursImg2;
dilate(contoursImg, dilatedContoursImg2, Mat());
// применяем эрозию к контурам
Mat erodedContoursImg2;
erode(dilatedContoursImg2, erodedContoursImg2, Mat());
// ищем на картинке контуры
vector<vector<Point>> contours;
findContours(erodedContoursImg2, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
timeOut << "Количество контуров " << contours.size() << endl;
Mat resultImg = Mat::zeros(img.size(), CV_8UC3);
Mat resultImgWithMarking = markedImg.clone();
vector<vector<Point>> vectorizedContours;
int startTime = clock();
for (vector<Point> contour: contours) {
drawLines(resultImgWithMarking, contour, myPink);
drawLines(resultImg, contour, myPink);
bool isClosed = isClosedContour(contour);
if (!isClosed) {
contour = doubleContourToSingle(contour);
}
// проверяем, близок ли контур по площади к площади описывающего его прямоугольника
if (canBeDescribedByRect(contour)) {
// находим контур-прямоугольник
vector<Point> resultContour = processingMinAreaRect(contour);
drawLines(resultImgWithMarking, resultContour, myGreen);
drawLines(resultImg, resultContour, myGreen);
vectorizedContours.push_back(resultContour);
// переходим к следующему контуру
continue;
}
// если нельзя описать, как прямоугольник, то используем алгоритм Грибова
vector<Point> gribovContour = processingGribovAlgorithm2(
resultImg,
contour,
dpEps,
gridStartX,
gridStartY,
gridInterval,
gridInterval,
ppc);
drawLines(resultImgWithMarking, gribovContour, myGreen);
drawLines(resultImg, gribovContour, myGreen);
vectorizedContours.push_back(gribovContour);
}
int endTime = clock();
// время работы
double duration = endTime - startTime;
timeOut << "eps " << dpEps << " interval " << gridInterval << " ppc " << ppc << " duration " << duration << endl;
timeOut.close();
Mat rightImg = Mat::zeros(img.size(), CV_8UC3);
fillPoly(rightImg, vectorizedContours, myWhite);
// вычисляем iou для ручной разметки и полученного алгоритмом изображения
double iou = getIou(markedImg, rightImg);
iouOut << "eps " << dpEps << " interval " << gridInterval << " ppc " << ppc << " iou " << iou << endl;
iouOut.close();
imwrite(resultImgPath, rightImg);
imwrite(onlyContoursImgPath, resultImg);
imwrite(resultWithMarkingPath, resultImgWithMarking);
return vectorizedContours;
}
/*!
* векторизуем изображение с разными комбинациями параметров
* @param img - исходное изображение
* @param img - изображение того же участка, но с ручной разметкой
* @param dpEpss - массив возможных значений параметра удаленности для алгоритма Дугласа-Пекера
* @param gridIntervals - массив возможных значений интервала между точками сетки в алгоритме Грибова
* @param ppcs - массив возможных значений количества рассматриваемых предыдущих точек
* @param dirPath - путь к папке, в которой будут лежать результаты (на конце слэш) (папка уже должна быть создана)
* @return векторизованные контуры, обладающие наилучшим iou
*/
vector<vector<Point>> vectorizeImgWithSeveralParameters(
const Mat& img,
const Mat& markedImg,
const vector<int>& dpEpss,
const vector<int>& gridIntervals,
const vector<int>& ppcs,
const String& dirPath
) {
String iouPath = dirPath + "iou.txt";
String timePath = dirPath + "time.txt";
ofstream iouOut;
iouOut.open(iouPath, ofstream::out);
iouOut << "Initial IoU " << getIou(img, markedImg) << endl;
ofstream timeOut;
timeOut.open(timePath, ofstream::out);
// они не сильно влияют, пусть будут дефолтными
int gridStartX = 0;
int gridStartY = 0;
// применяем дилатацию к исходному изображению
Mat dilatedContoursImg;
dilate(img, dilatedContoursImg, Mat());
// применяем эрозию к дилатированному изображению
Mat erodedContoursImg;
erode(dilatedContoursImg, erodedContoursImg, Mat());
// ищем контуры на дилатированно-эрозированном изображении
Mat contoursImg;
Canny(erodedContoursImg, contoursImg, 100, 255);
// применяем дилатацию к контурам
Mat dilatedContoursImg2;
dilate(contoursImg, dilatedContoursImg2, Mat());
// применяем эрозию к контурам
Mat erodedContoursImg2;
erode(dilatedContoursImg2, erodedContoursImg2, Mat());
// ищем на картинке контуры
vector<vector<Point>> contours;
findContours(erodedContoursImg2, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
timeOut << "Количество контуров " << contours.size() << endl;
double bestIou = 0;
vector<vector<Point>> bestVectorizedContours;
// итерируемся по разным значениям, чтобы найти лучшее
for (int dpEps: dpEpss) {
for (int gridInterval: gridIntervals) {
for (int ppc: ppcs) {
String commonPathPart = "_dpEps_"
+ to_string(dpEps)
+ "_interval_"
+ to_string(gridInterval)
+ "_ppc_"
+ to_string(ppc)
+ ".jpg";
String resultImgPath = dirPath + commonPathPart;
String onlyContoursImgPath = dirPath + "only_contours" + commonPathPart;
String resultWithMarkingPath = dirPath + "with_marking" + commonPathPart;
Mat resultImg = Mat::zeros(img.size(), CV_8UC3);
Mat resultImgWithMarking = markedImg.clone();
vector<vector<Point>> vectorizedContours;
int startTime = clock();
for (vector<Point> contour: contours) {
drawLines(resultImgWithMarking, contour, myPink);
drawLines(resultImg, contour, myPink);
bool isClosed = isClosedContour(contour);
if (!isClosed) {
contour = doubleContourToSingle(contour);
}
// проверяем, близок ли контур по площади к площади описывающего его прямоугольника
if (canBeDescribedByRect(contour)) {
// находим контур-прямоугольник
vector<Point> resultContour = processingMinAreaRect(contour);
drawLines(resultImgWithMarking, resultContour, myGreen);
drawLines(resultImg, resultContour, myGreen);
vectorizedContours.push_back(resultContour);
// переходим к следующему контуру
continue;
}
// если нельзя описать, как прямоугольник, то используем алгоритм Грибова
vector<Point> gribovContour = processingGribovAlgorithm2(
resultImg,
contour,
dpEps,
gridStartX,
gridStartY,
gridInterval,
gridInterval,
ppc);
drawLines(resultImgWithMarking, gribovContour, myGreen);
drawLines(resultImg, gribovContour, myGreen);
vectorizedContours.push_back(gribovContour);
}
int endTime = clock();
// время работы
double duration = endTime - startTime;
timeOut << "eps " << dpEps << " interval " << gridInterval << " ppc " << ppc << " duration " << duration
<< endl;
Mat rightImg = Mat::zeros(img.size(), CV_8UC3);
fillPoly(rightImg, vectorizedContours, myWhite);
// вычисляем iou для ручной разметки и полученного алгоритмом изображения
double iou = getIou(markedImg, rightImg);
iouOut << "eps " << dpEps << " interval " << gridInterval << " ppc " << ppc << " iou " << iou << endl;
if (iou > bestIou) {
bestIou = iou;
bestVectorizedContours = vectorizedContours;
}
imwrite(resultImgPath, rightImg);
imwrite(onlyContoursImgPath, resultImg);
imwrite(resultWithMarkingPath, resultImgWithMarking);
}
}
}
iouOut.close();
timeOut.close();
return bestVectorizedContours;
}