-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageHelper.cs
267 lines (235 loc) · 10.6 KB
/
ImageHelper.cs
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
using AForge.Imaging.Filters;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
namespace Arkose_maze_Solver
{
public class ImageHelper
{
public Bitmap ImageCleanerANDFindTargets(Image img)
{
Bitmap btm = null;
try
{
Bitmap imagem = new Bitmap(img);
imagem = imagem.Clone(new Rectangle(0, 0, img.Width, img.Height), PixelFormat.Format24bppRgb);
Invert inverter = new Invert();
GaussianSharpen gsp = new GaussianSharpen(4, 11);
EuclideanColorFiltering Eufilter = new EuclideanColorFiltering();
Eufilter.CenterColor = new AForge.Imaging.RGB(200, 200, 200);
Eufilter.Radius = 100;
ConservativeSmoothing Cofilter = new ConservativeSmoothing();
FiltersSequence seq = new FiltersSequence(inverter, gsp, Eufilter, Cofilter);
btm = (Bitmap)seq.Apply(imagem);
List<Point> pixels_loc = new List<Point>();
for (int i = 0; i < imagem.Width; i++)
{
for (int j = 0; j < imagem.Height; j++)
{
Color c = btm.GetPixel(i, j);
Color cOrg = imagem.GetPixel(i, j);
if (c.R > 50 && c.G > 50 && c.B > 50) // turn all bright colors into white
{
btm.SetPixel(i, j, Color.White);
}
if (c.R <= 50 && c.G <= 50 && c.B <= 50) // turn all bright colors into arrows (The color of the walls is currently white, in line 59 the color of the walls will change to black)
{
btm.SetPixel(i, j, Color.Black);
}
if (cOrg.R > 100 && cOrg.G > 100 && cOrg.B < 100) // add pixels of cheese colors to mark the list
{
pixels_loc.Add(new Point(i, j));
}
if (cOrg.R > 150 && cOrg.G > 150 && cOrg.B > 150) // add mouse (and cheese) pixels for marking in the list
{
pixels_loc.Add(new Point(i, j));
}
}
}
inverter.ApplyInPlace(btm); // Changing white and black colors to highlight the walls in black
pixels_loc = pixels_loc.OrderBy(p => p.X).ThenBy(p => p.Y).ToList(); // Sort Pixels Location
List<Point> pixels = new List<Point>(); // We have two targets, mouse and cheese. The variable < pixels_loc > contains the pixels of one target and the variable < pixels > contains the pixels of the second target.
foreach (Point i in pixels_loc) // Mouse and cheese pixel separation loop
{
if (pixels.Count == 0)
{
pixels.Add(i);
continue;
}
Point centerPoint = new Point
{
X = (int)Math.Round(pixels.Average(p => p.X)),
Y = (int)Math.Round(pixels.Average(p => p.Y))
};
if (Math.Abs(centerPoint.X - i.X) < 10 && Math.Abs(centerPoint.Y - i.Y) < 10)
{
pixels.Add(i);
}
else if (pixels.Count < 5)
{
pixels.Clear();
}
}
if (pixels.Count > 10) // If the second target is found
{
pixels_loc = pixels_loc.Where(x => !pixels.Contains(x)).ToList(); // Remove the wrong pixels from the variable
}
List<Point> Temp = new List<Point>();
Point centerpixelsPoint = new Point
{
X = (int)Math.Round(pixels.Average(p => p.X)),
Y = (int)Math.Round(pixels.Average(p => p.Y))
};
Point avgTempPoint = new Point
{
X = (int)Math.Round(pixels_loc.Average(p => p.X)),
Y = (int)Math.Round(pixels_loc.Average(p => p.Y))
};
List<Point> noise = new List<Point>(); // There may be some pixels in the image similar to mouse and cheese pixels, which are placed in this variable and finally deleted
foreach (Point p in pixels_loc)
{
if (Math.Sqrt(Math.Pow(p.X - avgTempPoint.X, 2) + Math.Pow(p.Y - avgTempPoint.Y, 2)) > 25)
{
if (Math.Sqrt(Math.Pow(p.X - centerpixelsPoint.X, 2) + Math.Pow(p.Y - centerpixelsPoint.Y, 2)) < 15)
{
Temp.Add(p);
}
else
{
noise.Add(p);
}
}
}
pixels_loc = pixels_loc.Where(x => !Temp.Contains(x) && !noise.Contains(x)).ToList();// Remove the wrong pixels from the variable
pixels.AddRange(Temp);// Add some pixels that were skipped
Temp.Clear();
if (pixels_loc.Count > 10)
{
pixels_loc = clearnoise(pixels_loc); // Removing pixels that are more than 15 pixels away from our target helps us find the center of the target image better
Point centerPoint = new Point
{
X = (int)Math.Round(pixels_loc.Average(p => p.X)),
Y = (int)Math.Round(pixels_loc.Average(p => p.Y))
};
int pensize = 5;
if (pixels_loc.Count > 75)
{
pensize = 7;
}
using (Graphics graphics = Graphics.FromImage(btm))
{
using (Pen pen = new Pen(Color.Red, pensize+1))
{
graphics.DrawRectangle(pen, centerPoint.X - (pensize / 2), centerPoint.Y - (pensize / 2), pensize, pensize);
}
}
}
if (pixels.Count > 10)
{
pixels = clearnoise(pixels);// Removing pixels that are more than 15 pixels away from our target helps us find the center of the target image better
centerpixelsPoint = new Point
{
X = (int)Math.Round(pixels.Average(p => p.X)),
Y = (int)Math.Round(pixels.Average(p => p.Y))
};
int pensize = 5;
if (pixels.Count > 75)
{
pensize = 7;
}
using (Graphics graphics = Graphics.FromImage(btm))
{
using (Pen pen = new Pen(Color.Blue, pensize+1))
{
graphics.DrawRectangle(pen, centerpixelsPoint.X - (pensize / 2), centerpixelsPoint.Y - (pensize / 2), pensize, pensize);
}
}
}
}
catch
{
return null;
}
return btm;
}
private List<Point> clearnoise(List<Point> lp)
{
List<Point> result = new List<Point>();
Point centerPoint = new Point
{
X = (int)Math.Round(lp.Average(p => p.X)),
Y = (int)Math.Round(lp.Average(p => p.Y))
};
foreach (Point p in lp)
{
if (Math.Sqrt(Math.Pow(p.X - centerPoint.X, 2) + Math.Pow(p.Y - centerPoint.Y, 2)) < 15) // Find pixels that are less than 15 pixels apart
{
result.Add(p);
}
}
return result;
}
public List<Bitmap> cropImage(Bitmap img)
{
List<Bitmap> result = new List<Bitmap>();
for (int x = 0; x < 300; x += 100)
{
for (int y = 0; y < 200; y += 100)
{
Bitmap target = new Bitmap(100, 100);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(img, new Rectangle(0, 0, target.Width, target.Height),
new Rectangle(x, y, 100, 100),
GraphicsUnit.Pixel);
}
result.Add(target);
}
}
return result;
}
public Bitmap mergeImage(List<CropDetail> imgs, Bitmap orgImg, bool final = false)
{
Bitmap result = new Bitmap(300, 200);
int counter = 0;
for (int x = 0; x < 300; x += 100)
{
for (int y = 0; y < 200; y += 100)
{
using (Graphics g = Graphics.FromImage(result))
{
if (imgs[counter].solve)
{
if (final)
{
g.DrawImage(orgImg, new Rectangle(x, y, 100, 100),
new Rectangle(x, y, 100, 100),
GraphicsUnit.Pixel);
}
else
{
g.DrawImage(imgs[counter].img, new Rectangle(x, y, 100, 100),
new Rectangle(0, 0, 100, 100),
GraphicsUnit.Pixel);
}
}
else
{
g.DrawImage(orgImg, new Rectangle(x, y, 100, 100),
new Rectangle(x, y, 100, 100),
GraphicsUnit.Pixel);
using (Pen pen = new Pen(Color.Red, 2))
{
g.DrawRectangle(pen, new Rectangle(x, y, 100, 100));
}
}
counter++;
}
}
}
return result;
}
}
}