-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.c
More file actions
295 lines (294 loc) · 9.07 KB
/
Copy pathSource.c
File metadata and controls
295 lines (294 loc) · 9.07 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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
struct Color
{
unsigned char R, G, B;
};
struct Size
{
int Dimension, Width, Height, Padding;
};
struct Bitmap
{
struct Size size;
struct Color* Map;
char *Header;
};
void Initialize(char *adress, struct Bitmap *image)
{
FILE *source;
source = fopen(adress, "rb");
if (source == NULL)
{
printf("Nu am gasit imaginea sursa din care citesc");
return;
}
fseek(source, 2, SEEK_SET);
fread(&image->size.Dimension, sizeof(unsigned int), 1, source);
fseek(source, 18, SEEK_SET);
fread(&image->size.Width, sizeof(unsigned int), 1, source);
fread(&image->size.Height, sizeof(unsigned int), 1, source);
int padding;
if (image->size.Width % 4 != 0)
padding = 4 - (3 * image->size.Width) % 4;
else
padding = 0;
image->size.Padding = padding;
image->Map = malloc(sizeof(struct Color)*image->size.Width*image->size.Height);
fseek(source, 0, SEEK_SET);
image->Header = malloc(54);
fread(image->Header, 1, 54, source);
int i,j;
unsigned char c;
fseek(source, 54, SEEK_SET);
for (i = 0; i < image->size.Height; i++)
{
for (j = 0; j < image->size.Width; j++)
{
fread(&c, 1, 1, source);
image->Map[i+j*(image->size.Height)].B = c;
fread(&c, 1, 1, source);
image->Map[i + j*(image->size.Height)].G = c;
fread(&c, 1, 1, source);
image->Map[i + j*(image->size.Height)].R = c;
}
unsigned char* rezidual=malloc(4);
fread(rezidual, 1, image->size.Padding, source);
}
fclose(source);
}
void Write(char *adress, struct Bitmap *image)
{
FILE *destination;
destination = fopen(adress, "wb");
if (destination == NULL)
{
printf("Nu am gasit imaginea sursa din care citesc");
return;
}
int i, j;
fwrite(image->Header, 1, 54, destination);
for (i = 0; i < image->size.Height; i++)
{
for (j = 0; j < image->size.Width; j++)
{
fwrite(&image->Map[i + j*(image->size.Height)].B, 1, 1, destination);
fwrite(&image->Map[i + j*(image->size.Height)].G, 1, 1, destination);
fwrite(&image->Map[i + j*(image->size.Height)].R, 1, 1, destination);
}
unsigned char* rezidual = calloc(1,4);
fwrite(rezidual, 1, image->size.Padding, destination);
}
fclose(destination);
}
void TurnToGray(struct Bitmap *bmp)
{
int i;
for (i = 0; i < bmp->size.Width*bmp->size.Height; i++)
{
bmp->Map[i].R = bmp->Map[i].G = bmp->Map[i].B = bmp->Map[i].R*0.299 + bmp->Map[i].G*0.587 + bmp->Map[i].B*0.114;
}
}
struct Window
{
int x, y;
};
struct OneMatch
{
struct Window window;
float Corelation;
unsigned char Digit;
};
struct TemplateMatch
{
struct OneMatch *Matches;
int NrOfMatches;
};
float MediumIntensity(struct Bitmap *image, int x, int y)
{
int i, j;
float resoult = 0;
for (i = x; i < x + 11; i++)
{
for (j = y; j < y + 15; j++)
{
resoult += image->Map[i + j * (image->size.Width)].R;
}
}
resoult /= 11 * 15;
return resoult;
}
float StandardIntensityDeviation(struct Bitmap *image, int x, int y)
{
int i, j;
float resoult = 0;
float mediumIntensity = MediumIntensity(image, x, y);
for (i = x; i < x + 11; i++)
{
for (j = y; j < y + 15; j++)
{
resoult += (image->Map[j + i * (image->size.Height)].R - mediumIntensity)*(image->Map[j + i * (image->size.Height)].R - mediumIntensity);
}
}
resoult /= 11 * 15 - 1;
resoult = sqrt(resoult);
return resoult;
}
float Corelation(struct Bitmap *image, struct Bitmap *sablon, int x, int y)
{
int i, j;
float resoult = 0;
float imageMediumIntensity = MediumIntensity(image, x, y);
float sablonMediumIntensity = MediumIntensity(sablon, 0, 0);
float imageStandardIntensityDeviation = StandardIntensityDeviation(image, x, y);
float sablonStandardIntenistyDeviation = StandardIntensityDeviation(sablon, 0, 0);
for (i = x; i < x + 11; i++)
{
for (j = y; j < y + 15; j++)
{
resoult += (image->Map[i + j * (image->size.Width)].R - imageMediumIntensity)*(sablon->Map[(i - x) + (j - y)* (sablon->size.Width)].R - sablonMediumIntensity);
}
}
resoult /= 11 * 15 * (imageStandardIntensityDeviation*sablonStandardIntenistyDeviation);
return resoult;
}
void TemplateMatching(struct TemplateMatch *f, struct Bitmap *image, struct Bitmap *sablon, float prag, unsigned char digit)
{
TurnToGray(image);
int x, y;
for (x = 0; x < image->size.Width - 11; x++)
{
for (y = 0; y < image->size.Height - 15; y++)
{
float cor = Corelation(image, sablon, x, y);
if (cor >= prag)
{
f->NrOfMatches++;
f->Matches = realloc(f->Matches, f->NrOfMatches * sizeof(struct OneMatch));
f->Matches[f->NrOfMatches - 1].window.x = y;
f->Matches[f->NrOfMatches - 1].window.y = x;
f->Matches[f->NrOfMatches - 1].Corelation = cor;
f->Matches[f->NrOfMatches - 1].Digit = digit;
}
}
}
}
void DrawRectangle(struct Bitmap *image, struct Window window, struct Color color)
{
int i, j;
for (i = window.x; i < window.x + 15; i++)
{
image->Map[i + window.y * (image->size.Height)] = color;
image->Map[i + (window.y + 10) * (image->size.Height)] = color;
}
for (j = window.y; j < window.y + 11; j++)
{
image->Map[window.x + j * (image->size.Height)] = color;
image->Map[window.x + 14 + j * (image->size.Height)] = color;
}
}
int CorelationsComparer(void *corelation1, void *corelation2)
{
struct OneMatch *C1 = corelation1;
struct OneMatch *C2 = corelation2;
return C1->Corelation < C2->Corelation;
}
void SortDetections(struct TemplateMatch *matches)
{
qsort(matches->Matches, matches->NrOfMatches, sizeof(struct OneMatch), CorelationsComparer);
}
int IsOver(struct Window *Important, struct Window *Unimportant)
{
return (Unimportant->x - Important->x < 15 && Unimportant->x - Important->x > -15) && (Unimportant->y - Important->y < 11 && Unimportant->y - Important->y > -11);
}
struct TemplateMatch DeleteNonMaximResoults(struct TemplateMatch *toDelete)
{
SortDetections(toDelete);
int i, j;
for (i = 0; i < toDelete->NrOfMatches; i++)
{
if (toDelete->Matches[i].window.x != -1)
{
for (j = i + 1; j < toDelete->NrOfMatches; j++)
{
if (IsOver(&(toDelete->Matches[i].window), &(toDelete->Matches[j].window)))
{
toDelete->Matches[j].window.x = -1;
}
}
}
}
struct TemplateMatch resoult;
resoult.Matches = malloc(sizeof(struct OneMatch));
resoult.NrOfMatches = 0;
int x;
for (x = 0; x < toDelete->NrOfMatches; x++)
{
if (toDelete->Matches[x].window.x != -1)
{
resoult.NrOfMatches++;
resoult.Matches = realloc(resoult.Matches, resoult.NrOfMatches * sizeof(struct OneMatch));
resoult.Matches[resoult.NrOfMatches - 1].window = toDelete->Matches[x].window;
resoult.Matches[resoult.NrOfMatches - 1].Digit = toDelete->Matches[x].Digit;
}
}
return resoult;
}
void FinalFunction()
{
char *PatternsRecognitionFolder, *PatternsRecognition,**Digits;
PatternsRecognitionFolder = malloc(200);
PatternsRecognition = malloc(200);
Digits = malloc(40);
printf("Type the path of the folder containing all the necessary images(digit<i>.bmp, test.bmp): ");
scanf("%s", PatternsRecognitionFolder);
strcpy(PatternsRecognition, PatternsRecognitionFolder);
strcat(PatternsRecognition, "test.bmp");
unsigned char i;
for (i = 0; i < 10; i++)
{
char *str = malloc(2);
sprintf(str, "%d", i);
Digits[i] = malloc(200);
strcpy(Digits[i], PatternsRecognitionFolder);
strcat(Digits[i], "digit");
strcat(Digits[i], str);
strcat(Digits[i], ".bmp");
}
struct TemplateMatch match;
match.NrOfMatches = 0;
match.Matches = malloc(sizeof(struct OneMatch));
struct Bitmap PatternsRecognitionImage;
Initialize(PatternsRecognition, &PatternsRecognitionImage);
for (i = 0; i < 10; i++)
{
struct Bitmap CurrentDigit;
Initialize(Digits[i], &CurrentDigit);
TemplateMatching(&match, &PatternsRecognitionImage, &CurrentDigit, 0.5, i);
}
struct TemplateMatch FinalResoult = DeleteNonMaximResoults(&match);
struct Color *ColorsForDigits = malloc(10 * sizeof(struct Color));
ColorsForDigits[0].R = 255; ColorsForDigits[0].G = 0; ColorsForDigits[0].B = 0;
ColorsForDigits[1].R = 255; ColorsForDigits[1].G = 255; ColorsForDigits[1].B = 0;
ColorsForDigits[2].R = 0; ColorsForDigits[2].G = 255; ColorsForDigits[2].B = 0;
ColorsForDigits[3].R = 0; ColorsForDigits[3].G = 255; ColorsForDigits[3].B = 255;
ColorsForDigits[4].R = 255; ColorsForDigits[4].G = 0; ColorsForDigits[4].B = 255;
ColorsForDigits[5].R = 0; ColorsForDigits[5].G = 0; ColorsForDigits[5].B = 255;
ColorsForDigits[6].R = 192; ColorsForDigits[6].G = 192; ColorsForDigits[6].B = 192;
ColorsForDigits[7].R = 255; ColorsForDigits[7].G = 140; ColorsForDigits[7].B = 0;
ColorsForDigits[8].R = 128; ColorsForDigits[8].G = 0; ColorsForDigits[8].B = 128;
ColorsForDigits[9].R = 128; ColorsForDigits[9].G = 0; ColorsForDigits[9].B = 0;
int k;
for (k = 0; k < FinalResoult.NrOfMatches; k++)
{
DrawRectangle(&PatternsRecognitionImage, FinalResoult.Matches[k].window, ColorsForDigits[FinalResoult.Matches[k].Digit]);
}
Write(PatternsRecognition, &PatternsRecognitionImage);
}
int main()
{
FinalFunction();
}