-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageProcessor.cs
164 lines (139 loc) · 5.87 KB
/
ImageProcessor.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
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ChinhDo.Functions
{
/// <summary>
/// Image processing functions
/// </summary>
public class ImageProcessor
{
/// <summary>Constructor</summary>
/// <param name="useUnsafeCode">True to use unsafe (faster code)</param>
public ImageProcessor() : this(false)
{
}
/// <summary>Constructor</summary>
/// <param name="useUnsafeCode">True to use unsafe (faster code)</param>
public ImageProcessor(bool useUnsafeCode) {
this.useUnsafeCode = useUnsafeCode;
}
/// <summary>Returns true if an image is blank.</summary>
/// <param name="imageFileName">Image file name</param>
public bool IsBlank(string imageFileName)
{
// TODO: Download more images and put in two folders... calculate stats
double stdDev = useUnsafeCode ? StdDevUnsafe(imageFileName) : StdDev(imageFileName);
Console.WriteLine(string.Format("img={0} stdDev={1}", imageFileName, stdDev));
return stdDev < blankThreshold;
}
/// <summary>
/// Get the standard deviation of pixel values.
/// </summary>
/// <param name="imageFileName">Name of the image file.</param>
/// <returns>Standard deviation.</returns>
public double StdDev(string imageFileName)
{
using (Bitmap bitmap1 = new Bitmap(imageFileName))
{
Bitmap bitmap2 = Resize(bitmap1);
double total = 0, totalVariance = 0;
double stdDev = 0;
int count = 0;
for (int x = 0; x < bitmap2.Width; x++)
{
for (int y = 0; y < bitmap2.Height; y++)
{
count++;
Color c = bitmap2.GetPixel(x, y);
int pixelValue = c.R + c.G + c.B;
total += pixelValue;
double avg = total / count;
totalVariance += Math.Pow(pixelValue - avg, 2);
stdDev = Math.Sqrt(totalVariance / count);
}
}
return stdDev;
}
}
/// <summary>
/// Get the standard deviation of pixel values. Use unsafe code for faster performance.
/// </summary>
/// <param name="imageFileName">Name of the image file.</param>
/// <returns>Standard deviation.</returns>
public double StdDevUnsafe(string imageFileName)
{
double total = 0, totalVariance = 0;
int count = 0;
double stdDev = 0;
using (Bitmap bitmap1 = new Bitmap(imageFileName))
{
// TODO: resize
// TODO: property to use unsafe
// TODO: test this
BitmapData bmData = bitmap1.LockBits(new Rectangle(0, 0, bitmap1.Width, bitmap1.Height), ImageLockMode.ReadOnly, bitmap1.PixelFormat);
int stride = bmData.Stride;
IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - bitmap1.Width * 3;
for (int y = 0; y < bitmap1.Height; y++)
{
for (int x = 0; x < bitmap1.Width; x++)
{
count++;
byte blue = p[0];
byte green = p[1];
byte red = p[2];
int pixelValue = red + green + blue;
total += pixelValue;
double avg = total / count;
totalVariance += Math.Pow(pixelValue - avg, 2);
stdDev = Math.Sqrt(totalVariance / count);
p += 3;
}
p += nOffset;
}
}
bitmap1.UnlockBits(bmData);
}
Console.WriteLine("img=" + imageFileName + " stdDev=" + stdDev);
return stdDev;
}
private float blankThreshold = 30f;
private bool useUnsafeCode = false;
private Bitmap Resize(Bitmap bitmap)
{
// If image is larger than 100x100 pixels resize it for performance
if (bitmap.Width > 100 || bitmap.Height > 100)
{
int width = 100;
float ratio = (float)bitmap.Width / (float)bitmap.Height;
int height = (int)(width / ratio);
var destRect = new Rectangle(0, 0, width, height);
Bitmap bitmap2 = new Bitmap(width, height);
bitmap2.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);
using (var graphics = Graphics.FromImage(bitmap2))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
graphics.SmoothingMode = SmoothingMode.HighSpeed;
graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(bitmap, destRect, 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return bitmap2;
}
else {
return bitmap;
}
}
}
}