-
Notifications
You must be signed in to change notification settings - Fork 5
/
Image_utils.cs
131 lines (110 loc) · 4.4 KB
/
Image_utils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using System.Drawing;
using Emgu.CV.CvEnum;
namespace ANPR_System
{
/// <summary>
/// This class contain functions for image processing
/// </summary>
public class Image_utils
{
#region preprocessing functions
/// <summary>
///
/// </summary>
/// <param name="input_image"></param>
/// <returns></returns>
public static Image<Gray, byte> gaussian_blur(Image<Gray, byte> input_image)
{
Image<Gray, byte> output_image = input_image.SmoothGaussian(7);
return output_image;
}
/// <summary>
///
/// </summary>
/// <param name="input_image"></param>
/// <returns></returns>
public static Image<Gray, float> sobel_X(Image<Gray, byte> input_image)
{
Image<Gray, float> output_image = input_image.Sobel(1, 0, 3);
output_image = output_image.ThresholdBinary(new Gray(100), new Gray(255));
//CvInvoke.BitwiseNot(output_image, output_image);
return output_image;
}
/// <summary>
///
/// </summary>
/// <param name="input_image"></param>
/// <returns></returns>
public static Image<Gray, byte> adaptive_threshold(Image<Gray, byte> input_image)
{
Image<Gray, byte> output_image = input_image.ThresholdAdaptive(new Gray(255), AdaptiveThresholdType.GaussianC, ThresholdType.BinaryInv, 11, new Gray(2));
return output_image;
}
/// <summary>
/// Implement Binary and then Otsu threshold
/// </summary>
/// <param name="input_image"></param>
/// <returns></returns>
public static Image<Gray, byte> threshold(Image<Gray, byte> input_image)
{
Image<Gray, byte> binary_thresh = new Image<Gray, byte>(input_image.Width, input_image.Height);
CvInvoke.Threshold(input_image, binary_thresh, 0, 255, ThresholdType.Binary);
Image<Gray, byte> output_image = new Image<Gray, byte>(input_image.Width, input_image.Height);
CvInvoke.Threshold(binary_thresh, output_image, 0, 255, ThresholdType.Otsu);
return output_image;
}
/// <summary>
///
/// </summary>
/// <param name="input_image"></param>
/// <returns></returns>
public static Image<Gray, byte> morphology_ex(Image<Gray, byte> input_image, Plate_feature.type_of_plate type)
{
Mat structure = new Mat();
if (type == Plate_feature.type_of_plate.RECT_PLATE)
{
structure = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(22,3), new Point(-1, -1));
}
else if (type == Plate_feature.type_of_plate.SQUARE_PLATE)
{
structure = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(26, 5), new Point(-1, -1));
}
Image<Gray, byte> output_image = input_image.MorphologyEx(MorphOp.Close, structure, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(0));
return output_image;
}
#endregion
#region contours and edges
/// <summary>
/// Find all contours in the input image
/// </summary>
/// <param name="image">Image after preprocessing</param>
/// <returns>null: if there is no contour</returns>
public static VectorOfVectorOfPoint find_all_contours(Image<Gray, byte> image)
{
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
Mat hier = new Mat();
CvInvoke.FindContours(image, contours, hier, RetrType.Tree, ChainApproxMethod.ChainApproxSimple);
//for (int i = 0; i < contours.Size; i++)
//{
// CvInvoke.DrawContours(img, contours, i, new MCvScalar(255, 0, 0), 2);
//}
if (contours.Size == 0)
{
return null;
}
else
{
return contours;
}
}
#endregion
}
}