-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcessing.cs
More file actions
200 lines (161 loc) · 5.99 KB
/
ImageProcessing.cs
File metadata and controls
200 lines (161 loc) · 5.99 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
namespace LILO.Shell;
public class ImageProcessing
{
public class Templates
{
public Bitmap source;
public Templates(Bitmap sourceImage)
{
this.source = sourceImage;
}
public Bitmap BlurredImage()
{
ScaleFilter scale = new ScaleFilter(0.1f);
ScaleFilter scaleHigher = new ScaleFilter(0.9f);
BlurFilter blur = new BlurFilter(60);
DarkenFilter darker = new DarkenFilter(0.5f);
Bitmap scaledPic = scale.ApplyFilter(source);
Bitmap blurredPic = blur.ApplyFilter(scaledPic);
Bitmap finalPic = darker.ApplyFilter(blurredPic);
Bitmap finaleScaledBit = scaleHigher.ApplyFilter(finalPic);
return finaleScaledBit;
}
}
public class ColorManagment
{
public class ColorDetector
{
public Bitmap image;
public ColorDetector(Bitmap sourceImage)
{
this.image = sourceImage;
}
public Color DetectMainColor()
{
Dictionary<Color, int> colorCounts = new Dictionary<Color, int>();
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
Color pixelColor = image.GetPixel(x, y);
if (colorCounts.ContainsKey(pixelColor))
{
colorCounts[pixelColor]++;
}
else
{
colorCounts[pixelColor] = 1;
}
}
}
Color mainColor = Color.Black;
int maxCount = 0;
foreach (KeyValuePair<Color, int> colorCount in colorCounts)
{
if (colorCount.Value > maxCount)
{
mainColor = colorCount.Key;
maxCount = colorCount.Value;
}
}
return mainColor;
}
public Color GetOppositeColor(Color color)
{
int red = 255 - color.R;
int green = 255 - color.G;
int blue = 255 - color.B;
return Color.FromArgb(red, green, blue);
}
}
}
public class BlurFilter
{
private int kernelSize;
public BlurFilter(int kernelSize)
{
this.kernelSize = kernelSize;
}
public Bitmap ApplyFilter(Bitmap sourceImage)
{
Bitmap outputImage = new Bitmap(sourceImage.Width, sourceImage.Height);
for (int x = 0; x < sourceImage.Width; x++)
{
for (int y = 0; y < sourceImage.Height; y++)
{
int red = 0, green = 0, blue = 0;
int count = 0;
for (int i = -kernelSize / 2; i <= kernelSize / 2; i++)
{
for (int j = -kernelSize / 2; j <= kernelSize / 2; j++)
{
if (x + i >= 0 && x + i < sourceImage.Width && y + j >= 0 && y + j < sourceImage.Height)
{
Color pixelColor = sourceImage.GetPixel(x + i, y + j);
red += pixelColor.R;
green += pixelColor.G;
blue += pixelColor.B;
count++;
}
}
}
red /= count;
green /= count;
blue /= count;
outputImage.SetPixel(x, y, Color.FromArgb(red, green, blue));
}
}
return outputImage;
}
}
public class ScaleFilter
{
private float scaleFactor;
public ScaleFilter(float scaleFactor)
{
this.scaleFactor = scaleFactor;
}
public Bitmap ApplyFilter(Bitmap sourceImage)
{
int newWidth = (int)(sourceImage.Width * scaleFactor);
int newHeight = (int)(sourceImage.Height * scaleFactor);
Bitmap outputImage = new Bitmap(newWidth, newHeight);
using (Graphics graphics = Graphics.FromImage(outputImage))
{
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(sourceImage, 0, 0, newWidth, newHeight);
}
return outputImage;
}
}
public class DarkenFilter
{
private float darknessAmount;
public DarkenFilter(float darknessAmount)
{
this.darknessAmount = darknessAmount;
}
public Bitmap ApplyFilter(Bitmap sourceImage)
{
Bitmap outputImage = new Bitmap(sourceImage.Width, sourceImage.Height);
for (int x = 0; x < sourceImage.Width; x++)
{
for (int y = 0; y < sourceImage.Height; y++)
{
Color pixelColor = sourceImage.GetPixel(x, y);
int red = (int)(pixelColor.R * darknessAmount);
int green = (int)(pixelColor.G * darknessAmount);
int blue = (int)(pixelColor.B * darknessAmount);
outputImage.SetPixel(x, y, Color.FromArgb(red, green, blue));
}
}
return outputImage;
}
}
}