forked from jeffheaton/encog-dotnet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownSample.cs
More file actions
205 lines (182 loc) · 6.4 KB
/
DownSample.cs
File metadata and controls
205 lines (182 loc) · 6.4 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
// Encog(tm) Artificial Intelligence Framework v2.5
// .Net Version
// http://www.heatonresearch.com/encog/
// http://code.google.com/p/encog-java/
//
// Copyright 2008-2010 by Heaton Research Inc.
//
// Released under the LGPL.
//
// This is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of
// the License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this software; if not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.
//
// Encog and Heaton Research are Trademarks of Heaton Research, Inc.
// For information on Heaton Research trademarks, visit:
//
// http://www.heatonresearch.com/copyright.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Chapter12OCR
{
public class DownSample
{
private Bitmap image;
private int downSampleTop;
private int downSampleBottom;
private int downSampleLeft;
private int downSampleRight;
private double ratioX;
private double ratioY;
public DownSample(Bitmap image)
{
this.image = image;
}
public bool[] PerformDownSample(int downSampleWidth, int downSampleHeight)
{
int size = downSampleWidth * downSampleHeight;
bool[] result = new bool[size];
FindBounds();
// now downsample
this.ratioX = (double)(this.downSampleRight - this.downSampleLeft)
/ (double)downSampleWidth;
this.ratioY = (double)(this.downSampleBottom - this.downSampleTop)
/ (double)downSampleHeight;
int index = 0;
for (int y = 0; y < downSampleHeight; y++)
{
for (int x = 0; x < downSampleWidth; x++)
{
result[index++] = DownSampleRegion(x, y);
}
}
return result;
}
/// <summary>
/// Called to downsample a quadrant of the image.
/// </summary>
/// <param name="x">The x coordinate of the resulting downsample.</param>
/// <param name="y">The y coordinate of the resulting downsample.</param>
/// <returns>Returns true if there were ANY pixels in the specified quadrant.</returns>
protected bool DownSampleRegion(int x, int y)
{
int startX = (int)(this.downSampleLeft + (x * this.ratioX));
int startY = (int)(this.downSampleTop + (y * this.ratioY));
int endX = (int)(startX + this.ratioX);
int endY = (int)(startY + this.ratioY);
for (int yy = startY; yy <= endY; yy++)
{
for (int xx = startX; xx <= endX; xx++)
{
Color pixel = this.image.GetPixel(xx, yy);
if (IsBlack(pixel))
{
return true;
}
}
}
return false;
}
/// <summary>
/// This method is called to automatically crop the image so that whitespace
/// is removed.
/// </summary>
protected void FindBounds()
{
int h = image.Height;
int w = image.Width;
// top line
for (int y = 0; y < h; y++)
{
if (!HLineClear(y))
{
this.downSampleTop = y;
break;
}
}
// bottom line
for (int y = h - 1; y >= 0; y--)
{
if (!HLineClear(y))
{
this.downSampleBottom = y;
break;
}
}
// left line
for (int x = 0; x < w; x++)
{
if (!VLineClear(x))
{
this.downSampleLeft = x;
break;
}
}
// right line
for (int x = w - 1; x >= 0; x--)
{
if (!VLineClear(x))
{
this.downSampleRight = x;
break;
}
}
}
/// <summary>
/// This method is called internally to see if there are any pixels in the
/// given scan line. This method is used to perform autocropping.
/// </summary>
/// <param name="y">The horizontal line to scan.</param>
/// <returns>True if there were any pixels in this horizontal line.</returns>
protected bool HLineClear(int y)
{
int w = this.image.Width;
for (int i = 0; i < w; i++)
{
Color pixel = this.image.GetPixel(i, y);
if (IsBlack(pixel))
{
return false;
}
}
return true;
}
/// <summary>
/// This method is called to determine if a vertical line is clear.
/// </summary>
/// <param name="x">The vertical line to scan.</param>
/// <returns>True if there are any pixels in the specified vertical line.</returns>
protected bool VLineClear(int x)
{
int w = this.image.Width;
int h = this.image.Height;
for (int i = 0; i < h; i++)
{
Color pixel = this.image.GetPixel(x, i);
if (IsBlack(pixel))
{
return false;
}
}
return true;
}
protected bool IsBlack(Color pixel)
{
return (pixel.R != 255 || pixel.G != 255 || pixel.B != 255);
}
}
}