-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardDetector.cpp
322 lines (257 loc) · 9.44 KB
/
cardDetector.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void sharpenImage(Mat& grayScaleImage)
{
Mat gaussianFilteredImage;
Mat unsharpMask;
Mat outputImage;
GaussianBlur(grayScaleImage, unsharpMask, Size(25, 25), 30, 30);
addWeighted(grayScaleImage, 2.4, unsharpMask, -1, -15, outputImage);
grayScaleImage = outputImage;
imshow("Unblurred", outputImage);
}
void removeGradient(Mat& grayScaleImage, Mat& outputImage)
{
//checking all the possible errors
if (grayScaleImage.empty() || grayScaleImage.channels() != 1 || grayScaleImage.type() != CV_8UC1)
{
cout << "Wrong input!";
return;
}
outputImage = Mat(grayScaleImage.size(), CV_8UC1);
//initializing minimum and maximum values
int min = grayScaleImage.at<uchar>(0, 0);
int max = grayScaleImage.at<uchar>(0, 0);
//finding minimum and maximum value by checking every single pixel in grayScaleImage
for (int i = 0; i < grayScaleImage.rows; i++)
{
for (int j = 0; j < grayScaleImage.cols; j++)
{
if (grayScaleImage.at<uchar>(i, j) < min)
{
min = grayScaleImage.at<uchar>(i, j);
}
if (grayScaleImage.at<uchar>(i, j) > max)
{
max = grayScaleImage.at<uchar>(i, j);
}
}
}
//stretching histogram
for (int i = 0; i < outputImage.rows; i++)
{
for (int j = 0; j < outputImage.cols; j++)
{
outputImage.at<uchar>(i, j) = 255 * (grayScaleImage.at<uchar>(i, j) - min) / (max - min);
//cout << out.at<uchar>(i, j);
}
}
//resize(outputImage.clone(), outputImage, Size(), 0.7, 0.7);
imshow("Stretched", outputImage);
}
void contoursFinder(Mat& grayScaleImage, Mat& cannyImage)
{
//creating a vector in which we can store contours
vector<vector<Point>> contours;
//vector for hierarchy of the specific contours
vector<Vec4i> hierarchy;
RNG rng(1);
//Canny function returns the image with thin contours but we need to eliminate all the interior contours as well
Canny(grayScaleImage, cannyImage, 0, 255, 3);
//resize(cannyImage.clone(), cannyImage, Size(), 0.7, 0.7);
imshow("cannyImage", cannyImage);
//morphological operation needed to be done to make findContours able to find any contours
dilate(cannyImage, grayScaleImage, Mat());
imshow("dilatedImage", grayScaleImage);
//black image made from the grayScaleImage where the specific contours will be sent
cannyImage = Mat::zeros(grayScaleImage.size(), CV_8UC1);
Mat colouredImage = Mat::zeros(cannyImage.size(), CV_8UC3);
//stackOverflow
findContours(grayScaleImage, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_NONE, Point(0, 0));
//hierarchy will be changing after every iteration because every card has the next contour at the same level until it reaches the final card
for (int i = 0; i >= 0; i = hierarchy[i][0])
{
//cout << hierarchy[i][0] << endl;
//calculating the size of the blob so we can ignore the small symbols and numbers on a card to focus on a number of main symbols
if (contourArea(contours[i], false) / arcLength(contours[i], true) > 6.9)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
//segmentation of contours with the specfic size
drawContours(colouredImage, contours, i, color, 1, LINE_4, hierarchy, 0);
drawContours(cannyImage, contours, i, Scalar(255, 255, 255), 1, LINE_4, hierarchy, 0);
}
}
imshow("Test", cannyImage);
imshow("Segmented Image", colouredImage);
}
int* countSymbols(Mat& cannyImage, int sumTab[4])
{
int cardID = 0;
//creating a vector in which we can store contours
vector<vector<Point>> contours;
//vector for hierarchy of the specific contours
vector<Vec4i> hierarchy;
int contourID[4];
//stackOverflow
findContours(cannyImage, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE, Point(0, 0));
resize(cannyImage.clone(), cannyImage, Size(), 0.7, 0.7);
imshow("Final image", cannyImage);
for (int i = 0; i < hierarchy.size(); i++)
{
//if we find a contour that has no parent means we are inside each card
if (hierarchy[i][3] == -1)
{
//finding all the children of each card - hierarchy[i][2] is an internal contour of a card
contourID[cardID] = hierarchy[i][2];
cardID++;
}
}
for (int cardID = 0; cardID < 4; cardID++)
{
sumTab[cardID] = 0;
for (int i = 0; i < hierarchy.size(); i++)
{
//looking for all the elements which parent is the internal contour of a card
if (hierarchy[i][3] == contourID[cardID])
{
sumTab[cardID]++;
}
}
}
for (int cardID = 0; cardID < 4; cardID++)
{
cout << "Card nr " << (cardID + 1) << ": " << sumTab[cardID]<< endl;;
}
return sumTab;
}
void countVariation(Mat& cannyImage, int sumTab[4])
{
int sum = 0;
for (int i = 0; i < 4; i++)
{
sum += sumTab[i];
}
double tempTab[4];
double tempTabSum = 0;
cout << "Sum of the cards: " << sum << endl;
double mean = double(sum) / 4;
cout << "Mean value: " << mean << endl;
for (int i = 0; i < 4; i++)
{
tempTab[i] = (sumTab[i] - mean) * (sumTab[i] - mean);
tempTabSum += tempTab[i];
}
cout << "Temporary sum of the squares of the value and mean difference: " << tempTabSum << endl;
cout << "Variance value: " << tempTabSum / 4;
}
void userDecision(Mat &srcImage, Mat& grayScaleImage, Mat& laplacianImage, Mat& cannyImage)
{
cout << "Welcome to the cardDetector programme! Choose the type of the image which you would like to process: " << endl;
cout << "1. Original image" << endl;
cout << "2. Blurred image" << endl;
cout << "3. Gradient image" << endl;
cout << "4. Salt & Pepper image" << endl;
Mat outputImage;
int sumTab[4];
int userD;
cin >> userD;
switch (userD)
{
case 1:
cout << "You decided to pick the original image. " << endl;
srcImage = imread("12390186-2019-11-21-144455.tif", 1);
if (srcImage.empty())
return;
namedWindow("Original image", WINDOW_AUTOSIZE);
imshow("Original image", srcImage);
cvtColor(srcImage, grayScaleImage, COLOR_BGR2GRAY);
//resize(grayScaleImage.clone(), grayScaleImage, Size(), 0.7, 0.7);
namedWindow("Grayscaled image", WINDOW_AUTOSIZE);
imshow("Grayscaled image", grayScaleImage);
medianBlur(grayScaleImage, grayScaleImage, 5);
//resize(grayScaleImage.clone(), grayScaleImage, Size(), 0.7, 0.7);
namedWindow("Filtered Image", WINDOW_AUTOSIZE);
imshow("Filtered Image", grayScaleImage);
threshold(grayScaleImage.clone(), grayScaleImage, 0, 255, THRESH_BINARY + THRESH_OTSU);
namedWindow("Binarized Image", WINDOW_AUTOSIZE);
imshow("Binarized Image", grayScaleImage);
contoursFinder(grayScaleImage, cannyImage);
countSymbols(cannyImage, sumTab);
countVariation(cannyImage, sumTab);
waitKey(0);
break;
case 2:
cout << "You decided to pick the blurred image. " << endl;
srcImage = imread("12390186-2019-11-21-144455_blur.tif", 1);
if (srcImage.empty())
return;
namedWindow("Blurred image", WINDOW_AUTOSIZE);
imshow("Blurred image", srcImage);
cvtColor(srcImage, grayScaleImage, COLOR_BGR2GRAY);
namedWindow("Grayscaled image", WINDOW_AUTOSIZE);
imshow("Grayscaled image", grayScaleImage);
for (int i = 0; i < 2; i ++)
{
sharpenImage(grayScaleImage);
}
namedWindow("Fixed image", WINDOW_AUTOSIZE);
imshow("Fixed image", grayScaleImage);
medianBlur(grayScaleImage.clone(), grayScaleImage, 5);
threshold(grayScaleImage.clone(), grayScaleImage, 0, 255, THRESH_BINARY + THRESH_OTSU);
namedWindow("Binarized Image", WINDOW_AUTOSIZE);
imshow("Binarized Image", grayScaleImage);
contoursFinder(grayScaleImage, cannyImage);
countSymbols(cannyImage, sumTab);
countVariation(cannyImage, sumTab);
waitKey(0);
break;
case 3:
cout << "You decided to pick the gradient image. " << endl;
srcImage = imread("12390186-2019-11-21-144455_gradient.tif", 1);
if (srcImage.empty())
return;
namedWindow("Gradient image", WINDOW_AUTOSIZE);
imshow("Gradient image", srcImage);
cvtColor(srcImage, grayScaleImage, COLOR_BGR2GRAY);
namedWindow("Grayscaled image", WINDOW_AUTOSIZE);
imshow("Grayscaled image", grayScaleImage);
removeGradient(grayScaleImage, outputImage);
contoursFinder(outputImage, cannyImage);
countSymbols(cannyImage, sumTab);
countVariation(cannyImage, sumTab);
waitKey(0);
break;
case 4:
cout << "You decided to pick the salt & pepper image. " << endl;
srcImage = imread("12390186-2019-11-21-144455_salt_pepper.tif", 1);
if (srcImage.empty())
return;
namedWindow("Salt & Pepper image", WINDOW_AUTOSIZE);
imshow("Salt & Pepper image", srcImage);
cvtColor(srcImage, grayScaleImage, COLOR_BGR2GRAY);
namedWindow("Grayscaled image", WINDOW_AUTOSIZE);
imshow("Grayscaled image", grayScaleImage);
medianBlur(grayScaleImage.clone(), grayScaleImage, 5);
threshold(grayScaleImage.clone(), grayScaleImage, 0, 255, THRESH_BINARY + THRESH_OTSU);
namedWindow("Binarized Image", WINDOW_AUTOSIZE);
imshow("Binarized Image", grayScaleImage);
contoursFinder(grayScaleImage, cannyImage);
countSymbols(cannyImage, sumTab);
countVariation(cannyImage, sumTab);
waitKey(0);
break;
default:
cout << "There is no option with this number! Choose again." << endl;
userDecision(srcImage, grayScaleImage, laplacianImage, cannyImage);
}
}
int main()
{
Mat srcImage;
Mat grayScaleImage;
Mat laplacianImage;
Mat cannyImage;
userDecision(srcImage, grayScaleImage, laplacianImage, cannyImage);
}