-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcessing.cs
More file actions
158 lines (143 loc) · 5.97 KB
/
ImageProcessing.cs
File metadata and controls
158 lines (143 loc) · 5.97 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
namespace BarcodeRecoveryCS
{
class ImageProcessing
{
public Bitmap Threshold(Bitmap image, int lower, int upper)
{
Color col;
Bitmap ret = image;
// go through each pixel in the image
for (int r = 0; r < image.Height; r++)
for (int c = 0; c < image.Width; c++)
{
col = image.GetPixel(c, r);
// check to see if the pixels red value is between the upper and lower limits
// if it is set the new pixel as white otherwise black
if (col.R >= lower && col.R <= upper)
ret.SetPixel(c, r, Color.White);
else
ret.SetPixel(c, r, Color.Black);
}
return ret;
}
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Bitmap toBW(Bitmap raw)
{
Color pixel;
int newBWV;
try
{
// go through each pixel one at a time and calcualte the avrage color
for (int r = 0; r < raw.Width; r++)
{
for (int c = 0; c < raw.Height; c++)
{
// get pixel
pixel = raw.GetPixel(r, c);
// calculate avrage and save into newImage image variable
newBWV = (int)(pixel.R + pixel.G + pixel.B) / 3;
//newBWV = (int)(pixel.R * 0.299 + pixel.G * 0.587 + pixel.B * 0.114);
raw.SetPixel(r, c, Color.FromArgb(newBWV, newBWV, newBWV));
}
}
return raw;
}
catch (Exception ex)
{
// if no image was passed return null
return raw;
}
}
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Bitmap invert(Bitmap image)
{
Color col, ncol;
Bitmap ret = image;
// go through all pixels in the image
for (int r = 0; r < image.Height; r++)
for (int c = 0; c < image.Width; c++){
// get the pixel
col = image.GetPixel(c,r);
// subtract the maximum value by the the red green and blue values
ncol = Color.FromArgb(255 - col.R, 255 - col.G, 255 - col.B);
// set the inverted pixel in the new image
ret.SetPixel(c, r, ncol);
}
return ret;
}
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Bitmap bwThresh(Bitmap raw, int lower, int upper)
{
Color pixel;
int newBWV;
try
{
// go through each pixel one at a time and calcualte the avrage color
for (int r = 0; r < raw.Height; r++)
{
for (int c = 0; c < raw.Width; c++)
{
// get pixel
pixel = raw.GetPixel(c, r);
// calculate avrage and save into newImage image variable
newBWV = (int)(pixel.R + pixel.G + pixel.B) / 3;
//newBWV = (int)(pixel.R * 0.299 + pixel.G * 0.587 + pixel.B * 0.114);
if (newBWV >= lower && newBWV <= upper)
raw.SetPixel(c, r, Color.Black);
else
raw.SetPixel(c, r, Color.White);
}
}
return raw;
}
catch (Exception ex)
{
// if no image was passed return null
MessageBox.Show(ex.ToString());
return raw;
}
}
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Bitmap blur(Bitmap image, int blurRad)
{
Bitmap img = toBW(image), ret;
List<int> filter = new List<int>();
int mid = (int)Math.Ceiling((blurRad * 1.0) / 2),
sum = 0;
ret = new Bitmap(image);
for (int r = 0; r < img.Height; r++)
for (int c = 0; c < img.Width; c++)
{
for (int subr = -mid*3; subr < mid*3; subr++)
{
for (int subc = -mid; subc < mid; subc++)
{
// get a list of values around the pixel
// if the pixel goes over the image set that value to 255
if (((r + subr) < 0 || (c + subc) < 0) || ((r + subr) >= img.Height || (c + subc) >= img.Width))
filter.Add(255);
else
filter.Add(img.GetPixel(c + subc, r + subr).R);
}
}
// do convolution
// gothrough all elemtnts in the list, normalize the numbers then sum up the results
for (int x = 0; x < filter.Count; x++)
sum += filter[x] / filter.Count;
// get the sum value and set it as a ne color
ret.SetPixel(c, r, Color.FromArgb(sum, sum, sum));
// reset for nest loop
sum = 0;
filter.Clear();
}
return ret;
}
}
}