-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
257 lines (211 loc) · 6.17 KB
/
main.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
#include "opencv2/core/core.hpp"
#include <opencv/highgui.h>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv/cv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <ctype.h>
#include <math.h>
#include "opencv2/imgcodecs.hpp"
#include <ctime>
#include <stdio.h>
#include <unistd.h>
using namespace std;
using namespace cv;
class neuron
{
public:
float sinapsis[50][50];
float value = 0;
neuron()
{
for(int x = 0; x < 50; x++)
{
for(int y = 0; y < 50; y++)
{
sinapsis[x][y] = 1;
}
}
}
};
cv::Mat frame;
const char* src_window = "Display window";
int drag = 0, select_flag = 0;
cv::Point point1, point2;
bool callback = false;
void mouseHandler(int event, int x, int y, int flags, void* param);
void mouseHandler(int event, int x, int y, int flags, void* param)
{
if (event == CV_EVENT_LBUTTONDOWN && !drag && !select_flag)
{
/* left button clicked. ROI selection begins */
point1 = cv::Point(x, y);
drag = 1;
}
if (event == CV_EVENT_MOUSEMOVE && drag && !select_flag)
{
/* mouse dragged. ROI being selected */
cv::Mat img1 = frame.clone();
point2 = cv::Point(x, y);
img1.at<uchar>(y,x) = 0;
if(y+1 < 50) img1.at<uchar>(y+1,x) = 0;
if(x+1 < 50) img1.at<uchar>(y,x+1) = 0;
if(y+1 < 50 && x+1 < 50) img1.at<uchar>(y+1,x+1) = 0;
if(y-1 >= 0) img1.at<uchar>(y-1,x) = 0;
if(x-1 >= 0) img1.at<uchar>(y,x-1) = 0;
if(y-1 >= 0 && x-1 >= 0) img1.at<uchar>(y-1,x-1) = 0;
//cv::rectangle(img1, point1, point2, CV_RGB(255, 0, 0), 3, 8, 0);
frame = img1;
cv::imshow(src_window, img1);
}
if (event == CV_EVENT_LBUTTONUP && drag && !select_flag)
{
cv::Mat img2 = frame.clone();
point2 = cv::Point(x, y);
drag = 0;
select_flag = 1;
cv::imshow(src_window, img2);
frame = img2;
callback = true;
}
}
int main()
{
cv::namedWindow(src_window,CV_WINDOW_AUTOSIZE);
frame = cv::imread("empty.png", CV_LOAD_IMAGE_GRAYSCALE);
cv::imshow(src_window,frame);
cv::setMouseCallback(src_window,mouseHandler,0);
cout << "Started" << endl;
neuron* pixels[50][50];
neuron* outpts[10];
for(int x = 0; x < 50; x++)
{
for(int y = 0; y < 50; y++)
{
pixels[x][y] = new neuron();
}
}
for(int x = 0; x < 10; x++)
{
outpts[x] = new neuron();
}
cout << pixels[1][1]->sinapsis[1][1] << endl;
cv::Mat images[50];
char alph[5] = { 'a', 'b', 'c','d','e'};
int pos = 0;
for(int o = 0; o < 50; o++)
{
images[o] = cv::imread("numbers/"+std::to_string(o%10)+"_"+alph[pos%5]+".png", CV_LOAD_IMAGE_GRAYSCALE);
if(o%10 == 9) pos++;
}
int cicle = 0;
pos = 0;
int teachFor = 100;
int testFor = 10;
while(true)
{
cicle++;
for(int out = 0; out < 10; out++)
{
outpts[out]->value = 0;
}
int imgN = (cicle % 50);
Mat image;
if(cicle >= teachFor)
{
if(cicle >= teachFor+testFor)
{
cout << "Use a pointing device to draw in the blank window. Only a single drag is allowed. It may guess wrongly in the first tries with different handwrittings." << endl;
drag = 0, select_flag = 0;
callback = false;
frame = cv::imread("empty.png", CV_LOAD_IMAGE_GRAYSCALE);
cv::imshow(src_window,frame);
for (;;)
{
if(callback)
{
if( callback )
break;
cv::imshow(src_window,frame);
}
cv::waitKey(30);
}
image = frame;
}
else
{
image = images[imgN];
}
}
else
image = images[imgN];
cout << "image: " + std::to_string(cicle%10)+"_"+alph[pos%5] << endl;
if(cicle%10 == 9) pos++;
imgN = (imgN %10);
for(int out = 0; out < 10; out++)
{
for(int x = 0; x < 50; x++)
{
for(int y = 0; y < 50; y++)
{
uint pix = image.at<uchar>(x,y);
if(pix < 50)
{
outpts[out]->value += outpts[out]->sinapsis[x][y];
}
}
}
}
for(int out = 0; out < 10; out++)
{
cout << out << ": " + std::to_string(outpts[out]->value) << endl;
}
int gueesed = -1;
int maxd = 0;
for(int out = 0; out < 10; out++)
{
if(outpts[out]->value >= maxd)
{
gueesed = out;
maxd = outpts[out]->value;
}
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(30);
cout << "Neural Network Guess: " << gueesed << " - The correct number is? " << endl;
string img;
int correct;
if(cicle >= teachFor)
{
cin >> img;
correct = atoi(img.c_str());
}
else
{
img = std::to_string(imgN);
correct = atoi(img.c_str());
cout << correct << endl;
}
//cout << "correct: " << correct << imgN<< endl;
if(correct != gueesed)
{
//cout << "incorrect" << endl;
for(int x = 0; x < 50; x++)
{
for(int y = 0; y < 50; y++)
{
uint pix = image.at<uchar>(x,y);
if(pix < 50)
{
outpts[correct]->sinapsis[x][y] += 1;
}
}
}
}
}
return 0;
}