-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_card_num.cpp
More file actions
301 lines (225 loc) · 7.84 KB
/
detect_card_num.cpp
File metadata and controls
301 lines (225 loc) · 7.84 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
290
291
292
293
294
295
296
297
298
299
300
301
#include <iostream>
#include <opencv2/opencv.hpp>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace cv;
using namespace std;
#define garo 1280
#define sero 720
#define restrict_w ((garo) * (0.4))
#define restrict_h ((sero) * (0.4))
#define restrict_nan ((garo) * (0.9))
//#define K_size ((garo) * (0.022))
int kernel_size = 28;
int main()
{
// 원본 사진 경로
const char* file_path = "../card_img/*.jpg";
vector<String> filename;
glob(file_path, filename, true);
vector<Mat> roi_vector;
for (int i = 0; i < filename.size(); i++)
{
String fname = filename[i];
Mat frame = imread(fname);
Mat last_result, last_result2, copy, copy_frame, matInput, matResult, k_3, k_15;
//resize(frame, frame, Size(garo, sero));
//imshow("현재 원본영상", frame);
//waitKey(0);
//destroyAllWindows();
last_result = frame.clone();
last_result2 = frame.clone();
matInput = frame.clone();
copy = frame.clone();
copy_frame = frame.clone();
k_3 = frame.clone();
k_15 = frame.clone();
Mat roi;
// resize 후 원본영상을 복사
cvtColor(matInput, matInput, COLOR_RGB2GRAY);
bilateralFilter(matInput, matResult, 5, 75, 75);
//GaussianBlur(matInput, matResult, Size(11,11), 0);
Canny(matResult, matInput, 0, 50);
//imshow("canny", matInput);
//waitKey(0);
//destroyAllWindows();
//Mat Kernel_15 = getStructuringElement(MORPH_RECT, Size((int)K_size, 1));
//Mat Kernel_15 = getStructuringElement(MORPH_RECT, Size(10, 1));
Mat Erode_Kernel = getStructuringElement(MORPH_RECT, Size(1, 3));
//Mat Dilate_Kernel = getStructuringElement(MORPH_RECT, Size(25, 5));
Mat Dilate_Kernel = getStructuringElement(MORPH_RECT, Size(kernel_size, 5));
// 모폴로지에 사용할 커널 생성
Mat dst1, dst2, dst3;
//morphologyEx(matInput, dst1, MORPH_DILATE, Kernel_15, Point(-1, -1), 2);
morphologyEx(matInput, dst2, MORPH_ERODE, Erode_Kernel, Point(-1, -1), 2);
morphologyEx(dst2, dst3, MORPH_DILATE, Dilate_Kernel, Point(-1, -1), 2);
//imshow("canny -> 팽창연산", dst1);
//waitKey(0);
//destroyAllWindows();
//mshow("canny -> 침식연산", dst2);
//aitKey(0);
//estroyAllWindows();
//
//mshow("canny -> 침식연산 후 팽창", dst3);
//aitKey(0);
//estroyAllWindows();
vector< vector<Point> > contour, contour1;
findContours(dst3, contour, RETR_LIST, CHAIN_APPROX_NONE);
vector<Rect> boundRect(contour.size());
vector<RotatedRect> minRect(contour.size());
// minAreaRect 생성
for (int i = 0; i < contour.size(); i++)
{
minRect[i] = minAreaRect(Mat(contour[i]));
}
// 후보 3개 저장용 구조체
typedef struct set
{
float area = 0;
int idx = 0;
}Set;
vector <Set> temp_set(3); // 후보 3개
// minAreaRect 그리는 부분
for (int i = 0; i < contour.size(); i++)
{
// center에 사각형의 중심(x,y)좌표, size에 width, height, 기울어진 각도를 가지고있음
Point2f rect_points[4];
minRect[i].points(rect_points);
float w, h;
w = minRect[i].size.width;
h = minRect[i].size.height;
float ratio = w > h ? w / h : h / w;
ratio = abs(ratio);
//전체 다 boxing
for (int j = 0; j < 4; j++)
{
line(last_result2, rect_points[j], rect_points[(j + 1) % 4], Scalar(255, 255, 0), 4, 8);
}
//가로, 세로 길이 , 종횡비 등으로 거르기
if (((w <= restrict_w && ratio >= 4) || (h <= restrict_h && ratio >= 4)) || ((isnan(ratio) && w < restrict_nan) || (isnan(ratio) && h < restrict_nan)))
{
int k = 0;
//넓이 순으로 상위 3등까지만 저장
if (temp_set[2].area < minRect[i].size.area())
{
if (temp_set[1].area < minRect[i].size.area())
{
if (temp_set[0].area < minRect[i].size.area())
{
temp_set[2] = temp_set[1];
temp_set[1] = temp_set[0];
temp_set[0].area = minRect[i].size.area();
temp_set[0].idx = i;
}
else
{
temp_set[2] = temp_set[1];
temp_set[1].area = minRect[i].size.area();
temp_set[1].idx = i;
}
}
else
{
temp_set[2].area = minRect[i].size.area();
temp_set[2].idx = i;
}
}
/*조건 박스
for (int j = 0; j < 4; j++)
{
line(k_15, rect_points[j], rect_points[(j + 1) % 4], Scalar(0, 0, 255), 4, 8);
}
*/
}
//전체 중에서 맨 마지막 컨투어 일때
if (i == contour.size() - 1)
{
for (int k = 0; k < 2; k++) // 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222를 3으로 바꿔야 후보 3개 지금은 2로함
{
//cout << "인덱스 : " << temp_set[k].idx;
//cout << " 의 넓이 : " << temp_set[k].area << endl;
minRect[temp_set[k].idx].points(rect_points);
int left_idx1 = 0;
int left_idx2 = 0;
int right_idx1 = 0;
int right_idx2 = 0;
for (int j = 0; j < 4; j++)
{
line(k_15, rect_points[j], rect_points[(j + 1) % 4], Scalar(0, 0, 255), 4, 8);
//cout << "현재의 j : " << j << " rect_points[j] : " << rect_points[j] << endl;
if (rect_points[left_idx2].x >= rect_points[j].x)
{
if (rect_points[left_idx1].x >= rect_points[j].x)
{
left_idx2 = left_idx1;
left_idx1 = j;
continue;
}
left_idx2 = j;
}
if (rect_points[right_idx2].x <= rect_points[j].x)
{
if (rect_points[right_idx1].x <= rect_points[j].x)
{
right_idx2 = right_idx1;
right_idx1 = j;
continue;
}
right_idx2 = j;
}
}
int leftTop = rect_points[left_idx2].y > rect_points[left_idx1].y ? left_idx1 : left_idx2;
int leftBottom = left_idx1 == leftTop ? left_idx2 : left_idx1;
int rightTop = rect_points[right_idx2].y > rect_points[right_idx1].y ? right_idx1 : right_idx2;
int rightBottom = right_idx1 == rightTop ? right_idx2 : right_idx1;
float width, height;
width = sqrtf(powf(rect_points[rightTop].x - rect_points[leftTop].x, 2) + powf(abs(rect_points[rightTop].y - rect_points[leftTop].y), 2));
height = sqrtf(powf(abs(rect_points[leftTop].x - rect_points[leftBottom].x), 2) + powf(rect_points[leftBottom].y - rect_points[leftTop].y, 2));
//cout << "left top : " << leftTop << endl;
//cout << "left bottom : " << leftBottom << endl;
//cout << "right top : " << rightTop << endl;
//cout << "right bottom : " << rightBottom << endl;
Point2f src[4], dst[4];
src[0] = Point2f(rect_points[leftTop].x, rect_points[leftTop].y - 10);
src[1] = Point2f(rect_points[rightTop].x, rect_points[rightTop].y - 10);
src[2] = Point2f(rect_points[leftBottom].x, rect_points[leftBottom].y + 10);
src[3] = Point2f(rect_points[rightBottom].x, rect_points[rightBottom].y + 10);
dst[0] = Point2f(0, 0);
dst[1] = Point2f(width - 1, 0);
dst[2] = Point2f(0, height - 1);
dst[3] = Point2f(width - 1, height - 1);
Mat transformMatrix = getPerspectiveTransform(src, dst);
warpPerspective(last_result, roi, transformMatrix, Size(width, height));
if (roi.empty())
{
cout << "roi is empty!" << endl;
continue;
}
imwrite("./roi.jpg", roi);
Mat read_roi = imread("./roi.jpg");
Mat filter, canny, thre, rest, med;
// 화질 개선된 grayscale 이미지가 filter에 저장됨
cvtColor(read_roi, read_roi, COLOR_RGB2GRAY);
//imshow("저장된 roi", read_roi);
//waitKey();
GaussianBlur(read_roi, filter, Size(0, 0), 3);
addWeighted(read_roi, 5.0, filter, -4.0, 0, filter);
//imshow("filter", filter);
//waitKey();
//destroyAllWindows();
roi_vector.push_back(filter.clone());
}
}
}
// roi 이미지 저장
for (int i = 0; i < roi_vector.size(); i++)
{
char roi_name[100];
sprintf_s(roi_name, "../roi_img/%d.jpg", i);
Mat roi = roi_vector[i];
imwrite(roi_name, roi);
}
cout << i << "번째 이미지 저장 끝" << endl;
}
}