This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
QuadrilateralFinder.cs
239 lines (219 loc) · 9.4 KB
/
QuadrilateralFinder.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
// AForge Image Processing Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © Andrew Kirillov, 2007-2009
// andrew.kirillov@aforgenet.com
//
namespace Accord.Imaging
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using AForge;
using Accord.Math.Geometry;
/// <summary>
/// Searching of quadrilateral/triangle corners.
/// </summary>
///
/// <remarks><para>The class searches for quadrilateral's/triangle's corners on the specified image.
/// It first collects edge points of the object and then uses
/// <see cref="Accord.Math.Geometry.PointsCloud.FindQuadrilateralCorners"/> to find corners
/// the quadrilateral/triangle.</para>
///
/// <para><note>The class treats all black pixels as background (none-object) and
/// all none-black pixels as object.</note></para>
///
/// <para>The class processes grayscale 8 bpp and color 24/32 bpp images.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // get corners of the quadrilateral
/// QuadrilateralFinder qf = new QuadrilateralFinder( );
/// List<IntPoint> corners = qf.ProcessImage( image );
///
/// // lock image to draw on it with AForge.NET's methods
/// // (or draw directly on image without locking if it is unmanaged image)
/// BitmapData data = image.LockBits( new Rectangle( 0, 0, image.Width, image.Height ),
/// ImageLockMode.ReadWrite, image.PixelFormat );
///
/// Drawing.Polygon( data, corners, Color.Red );
/// for ( int i = 0; i < corners.Count; i++ )
/// {
/// Drawing.FillRectangle( data,
/// new Rectangle( corners[i].X - 2, corners[i].Y - 2, 5, 5 ),
/// Color.FromArgb( i * 32 + 127 + 32, i * 64, i * 64 ) );
/// }
///
/// image.UnlockBits( data );
/// </code>
///
/// <para><b>Source image:</b></para>
/// <img src="..\images\imaging\sample17.png" width="320" height="240" />
/// <para><b>Result image:</b></para>
/// <img src="..\images\imaging\quadrilateral_finder.png" width="320" height="240" />
/// </remarks>
///
public class QuadrilateralFinder
{
/// <summary>
/// Find corners of quadrilateral/triangular area in the specified image.
/// </summary>
///
/// <param name="image">Source image to search quadrilateral for.</param>
///
/// <returns>Returns a list of points, which are corners of the quadrilateral/triangular area found
/// in the specified image. The first point in the list is the point with lowest
/// X coordinate (and with lowest Y if there are several points with the same X value).
/// Points are in clockwise order (screen coordinates system).</returns>
///
/// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the source image.</exception>
///
public List<IntPoint> ProcessImage(Bitmap image)
{
CheckPixelFormat(image.PixelFormat);
// lock source image
BitmapData imageData = image.LockBits(
new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly, image.PixelFormat);
List<IntPoint> corners = null;
try
{
// process the image
corners = ProcessImage(new UnmanagedImage(imageData));
}
finally
{
// unlock image
image.UnlockBits(imageData);
}
return corners;
}
/// <summary>
/// Find corners of quadrilateral/triangular area in the specified image.
/// </summary>
///
/// <param name="imageData">Source image data to search quadrilateral for.</param>
///
/// <returns>Returns a list of points, which are corners of the quadrilateral/triangular area found
/// in the specified image. The first point in the list is the point with lowest
/// X coordinate (and with lowest Y if there are several points with the same X value).
/// Points are in clockwise order (screen coordinates system).</returns>
///
/// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the source image.</exception>
///
public List<IntPoint> ProcessImage(BitmapData imageData)
{
return ProcessImage(new UnmanagedImage(imageData));
}
/// <summary>
/// Find corners of quadrilateral/triangular area in the specified image.
/// </summary>
///
/// <param name="image">Source image to search quadrilateral for.</param>
///
/// <returns>Returns a list of points, which are corners of the quadrilateral/triangular area found
/// in the specified image. The first point in the list is the point with lowest
/// X coordinate (and with lowest Y if there are several points with the same X value).
/// Points are in clockwise order (screen coordinates system).</returns>
///
/// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of the source image.</exception>
///
public List<IntPoint> ProcessImage(UnmanagedImage image)
{
CheckPixelFormat(image.PixelFormat);
// get source image size
int width = image.Width;
int height = image.Height;
// collection of points
List<IntPoint> points = new List<IntPoint>();
// collect edge points
unsafe
{
byte* src = (byte*)image.ImageData.ToPointer();
int stride = image.Stride;
bool lineIsEmpty;
if (image.PixelFormat == PixelFormat.Format8bppIndexed)
{
// for each row
for (int y = 0; y < height; y++)
{
lineIsEmpty = true;
// scan from left to right
for (int x = 0; x < width; x++)
{
if (src[x] != 0)
{
points.Add(new IntPoint(x, y));
lineIsEmpty = false;
break;
}
}
if (!lineIsEmpty)
{
// scan from right to left
for (int x = width - 1; x >= 0; x--)
{
if (src[x] != 0)
{
points.Add(new IntPoint(x, y));
break;
}
}
}
src += stride;
}
}
else
{
// 24 or 32 bpp color image
int pixelSize = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
byte* ptr = null;
// for each row
for (int y = 0; y < height; y++)
{
lineIsEmpty = true;
// scan from left to right
ptr = src;
for (int x = 0; x < width; x++, ptr += pixelSize)
{
if ((ptr[RGB.R] != 0) || (ptr[RGB.G] != 0) || (ptr[RGB.B] != 0))
{
points.Add(new IntPoint(x, y));
lineIsEmpty = false;
break;
}
}
if (!lineIsEmpty)
{
// scan from right to left
ptr = src + width * pixelSize - pixelSize;
for (int x = width - 1; x >= 0; x--, ptr -= pixelSize)
{
if ((ptr[RGB.R] != 0) || (ptr[RGB.G] != 0) || (ptr[RGB.B] != 0))
{
points.Add(new IntPoint(x, y));
break;
}
}
}
src += stride;
}
}
}
return PointsCloud.FindQuadrilateralCorners(points);
}
private static void CheckPixelFormat(PixelFormat format)
{
// check image format
if ((format != PixelFormat.Format8bppIndexed) &&
(format != PixelFormat.Format24bppRgb) &&
(format != PixelFormat.Format32bppArgb) &&
(format != PixelFormat.Format32bppPArgb))
{
throw new UnsupportedImageFormatException("Unsupported pixel format of the source image.");
}
}
}
}