-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathComputerVision.cs
492 lines (420 loc) · 20.5 KB
/
ComputerVision.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
using OpenCvSharp;
using OpenCvSharp.XImgProc;
using System;
using System.Linq;
using System.Threading;
using Windows.Graphics.Imaging;
using Windows.UI;
namespace ComputerVision
{
public static class ComputerVisionProvider
{
static ComputerVisionProvider()
{
if (!Cv2.UseOptimized())
{
Cv2.SetUseOptimized(true);
}
}
public static SoftwareBitmap CreateCircleBitmapFromColor(int Width, int Height, Color FillColor)
{
if (Width <= 0)
{
throw new ArgumentOutOfRangeException(nameof(Width), "Argument must be positive");
}
if (Height <= 0)
{
throw new ArgumentOutOfRangeException(nameof(Height), "Argument must be positive");
}
using (Mat OutputMat = CreateEmptyOutputBitmap(Width, Height, out SoftwareBitmap Output))
{
Cv2.Circle(OutputMat, Width / 2, Height / 2, Math.Min(Width, Height) / 4, new Scalar(FillColor.B, FillColor.G, FillColor.R, FillColor.A), -1, LineTypes.AntiAlias);
return Output;
}
}
public static float DetectAvgBrightness(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat BGRMat = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_8UC3))
{
Cv2.CvtColor(InputMat, BGRMat, ColorConversionCodes.BGRA2BGR);
using (Mat HSVMat = new Mat(BGRMat.Rows, BGRMat.Cols, MatType.CV_8UC3))
{
Cv2.CvtColor(BGRMat, HSVMat, ColorConversionCodes.BGR2HSV);
using (Mat VChannel = HSVMat.ExtractChannel(2))
{
return Convert.ToSingle(Cv2.Mean(VChannel).Val0);
}
}
}
}
public static SoftwareBitmap RotateEffect(SoftwareBitmap Input, RotateFlags Flags)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
{
switch (Flags)
{
case RotateFlags.Rotate90Clockwise:
case RotateFlags.Rotate90Counterclockwise:
{
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelHeight, Input.PixelWidth, out SoftwareBitmap Output))
{
Cv2.Rotate(InputMat, OutputMat, RotateFlags.Rotate90Clockwise);
return Output;
}
}
default:
{
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
Cv2.Rotate(InputMat, OutputMat, RotateFlags.Rotate90Clockwise);
return Output;
}
}
}
}
}
public static SoftwareBitmap FlipEffect(SoftwareBitmap Input, FlipMode Mode)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
Cv2.Flip(InputMat, OutputMat, Mode);
return Output;
}
}
public static SoftwareBitmap AdjustBrightnessContrast(SoftwareBitmap Input, double Alpha, double Beta)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
InputMat.ConvertTo(OutputMat, MatType.CV_8UC4, Alpha, Beta);
return Output;
}
}
public static SoftwareBitmap AutoColorEnhancement(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
using (Mat Temp = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_32FC3))
using (Mat Gray = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_32FC3))
{
InputMat.ConvertTo(Temp, MatType.CV_32FC3);
Cv2.CvtColor(Temp, Gray, ColorConversionCodes.BGR2GRAY);
Cv2.MinMaxLoc(Gray, out _, out double LwMax);
float LwAver;
int Num;
using (Mat Lw_ = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_32FC3))
{
Num = InputMat.Rows * InputMat.Cols;
//计算每个数组元素绝对值的自然对数
Cv2.Log(Gray + 1e-3f, Lw_);
//矩阵自然指数
LwAver = Convert.ToSingle(Math.Exp(Cv2.Sum(Lw_)[0] / Num));
}
using (Mat Lg = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_32FC3))
{
Cv2.Log(Gray / LwAver + 1f, Lg);
Cv2.Divide(Lg, Math.Log(LwMax / LwAver + 1f), Lg);
//局部自适应
using (Mat Lout = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_32FC3))
{
int kernelSize = Convert.ToInt32(Math.Floor(Convert.ToDouble(Math.Max(3, Math.Max(InputMat.Rows / 100, InputMat.Cols / 100)))));
using (Mat Lp = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_32FC3))
{
using (Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(kernelSize, kernelSize)))
{
Cv2.Dilate(Lg, Lp, kernel);
}
using (Mat Hg = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_32FC3))
{
CvXImgProc.GuidedFilter(Lg, Lp, Hg, 10, 0.01);
Cv2.MinMaxLoc(Lg, out _, out double LgMax);
using (Mat alpha = 1.0f + Lg * (36 / LgMax))
using (Mat Lg_ = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_32FC3))
{
Cv2.Log(Lg + 1e-3f, Lg_);
Cv2.Log(Lg / Hg + (float)(10 * Convert.ToSingle(Math.Exp(Cv2.Sum(Lg_)[0] / Num))), Lout);
Cv2.Multiply(alpha, Lout, Lout);
Cv2.Normalize(Lout, Lout, 0, 255, NormTypes.MinMax);
}
}
}
using (Mat Gain = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_32F))
{
for (int i = 0; i < InputMat.Rows; i++)
{
for (int j = 0; j < InputMat.Cols; j++)
{
float x = Gray.At<float>(i, j);
float y = Lout.At<float>(i, j);
Gain.At<float>(i, j) = x == 0 ? y : y / x;
}
}
Mat[] BGRChannel = Cv2.Split(Temp).Select<Mat, Mat>((Channel) => (Gain.Mul(Channel + Gray) + Channel - Gray) * 0.5f).ToArray();
try
{
Cv2.Merge(BGRChannel, Temp);
Temp.ConvertTo(OutputMat, MatType.CV_8UC4);
}
finally
{
Array.ForEach(BGRChannel, (Channel) => Channel.Dispose());
}
}
}
}
}
return Output;
}
}
public static SoftwareBitmap AutoWhiteBalance(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
using (Mat TempMat = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_8UC3))
{
Cv2.CvtColor(InputMat, TempMat, ColorConversionCodes.BGRA2BGR);
int[] HistRGB = new int[767];
int MaxVal = 0;
for (int i = 0; i < InputMat.Rows; i++)
{
for (int j = 0; j < InputMat.Cols; j++)
{
Vec3b Vec = TempMat.At<Vec3b>(i, j);
MaxVal = Math.Max(Math.Max(Math.Max(MaxVal, Vec.Item0), Vec.Item1), Vec.Item2);
HistRGB[Vec.Item0 + Vec.Item1 + Vec.Item2]++;
}
}
int Threshold = 766;
for (int Sum = 0; Threshold >= 0; Threshold--)
{
if ((Sum += HistRGB[Threshold]) > InputMat.Rows * InputMat.Cols * 0.1)
{
break;
}
}
int SumB = 0;
int SumG = 0;
int SumR = 0;
int Count = 0;
unsafe
{
TempMat.ForEachAsVec3b((value, position) =>
{
if (value->Item0 + value->Item1 + value->Item2 > Threshold)
{
Interlocked.Add(ref SumB, value->Item0);
Interlocked.Add(ref SumG, value->Item1);
Interlocked.Add(ref SumR, value->Item2);
Interlocked.Increment(ref Count);
}
});
}
int AvgB = SumB / Count;
int AvgG = SumG / Count;
int AvgR = SumR / Count;
unsafe
{
TempMat.ForEachAsVec3b((value, position) =>
{
value->Item0 = (byte)Math.Max(0, Math.Min(255, value->Item0 * MaxVal / AvgB));
value->Item1 = (byte)Math.Max(0, Math.Min(255, value->Item1 * MaxVal / AvgG));
value->Item2 = (byte)Math.Max(0, Math.Min(255, value->Item2 * MaxVal / AvgR));
});
}
Cv2.CvtColor(TempMat, OutputMat, ColorConversionCodes.BGR2BGRA);
}
return Output;
}
}
public static SoftwareBitmap InvertEffect(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
using (Mat TempMat = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_8UC4))
{
Cv2.BitwiseNot(InputMat, TempMat);
TempMat.CopyTo(OutputMat);
}
return Output;
}
}
public static SoftwareBitmap GenenateResizedThumbnail(SoftwareBitmap Input, int Height, int Width)
{
if (Width <= 0)
{
throw new ArgumentOutOfRangeException(nameof(Width), "Argument must be positive");
}
if (Height <= 0)
{
throw new ArgumentOutOfRangeException(nameof(Height), "Argument must be positive");
}
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Width, Height, out SoftwareBitmap Output))
{
int MaxOffset = Math.Min(InputMat.Cols - Width, InputMat.Rows - Height);
int CropWidth = Width + MaxOffset;
int CropHeight = Height + MaxOffset;
using (Mat CroppedMat = InputMat[new Rect((InputMat.Cols - CropWidth) / 2, (InputMat.Rows - CropHeight) / 2, CropWidth, CropHeight)])
{
Cv2.Resize(CroppedMat, OutputMat, new Size(Width, Height), 0, 0, InterpolationFlags.Area);
}
return Output;
}
}
public static SoftwareBitmap ThresholdEffect(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
using (Mat TempMat = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_8UC4))
{
Cv2.CvtColor(InputMat, TempMat, ColorConversionCodes.BGRA2GRAY);
Cv2.Threshold(TempMat, TempMat, 128, 255, ThresholdTypes.Otsu);
Cv2.CvtColor(TempMat, OutputMat, ColorConversionCodes.GRAY2BGRA);
}
return Output;
}
}
public static SoftwareBitmap SketchEffect(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
using (Mat TempMat = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_8UC4))
{
Cv2.CvtColor(InputMat, TempMat, ColorConversionCodes.BGRA2GRAY);
Cv2.AdaptiveThreshold(TempMat, TempMat, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 21, 4);
Cv2.CvtColor(TempMat, OutputMat, ColorConversionCodes.GRAY2BGRA);
}
return Output;
}
}
public static SoftwareBitmap GaussianBlurEffect(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
Cv2.GaussianBlur(InputMat, OutputMat, new Size(0, 0), 8);
return Output;
}
}
public static SoftwareBitmap SepiaEffect(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
using (Mat TempMat = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_8UC4))
{
Cv2.CvtColor(InputMat, TempMat, ColorConversionCodes.BGRA2BGR);
using (Mat Kernel = new Mat<float>(3, 3, new float[9] { 0.273f, 0.534f, 0.131f, 0.349f, 0.686f, 0.168f, 0.393f, 0.769f, 0.189f }))
{
Cv2.Transform(TempMat, TempMat, Kernel);
}
Cv2.CvtColor(TempMat, OutputMat, ColorConversionCodes.BGR2BGRA);
}
return Output;
}
}
public static SoftwareBitmap OilPaintingEffect(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
using (Mat TempMat = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_8UC4))
{
Cv2.CvtColor(InputMat, TempMat, ColorConversionCodes.BGRA2BGR);
Cv2.PyrMeanShiftFiltering(TempMat, TempMat, 12, 60);
Cv2.CvtColor(TempMat, OutputMat, ColorConversionCodes.BGR2BGRA);
}
return Output;
}
}
public static SoftwareBitmap GrayEffect(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
using (Mat TempMat = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_8UC4))
{
Cv2.CvtColor(InputMat, TempMat, ColorConversionCodes.BGRA2GRAY);
Cv2.CvtColor(TempMat, OutputMat, ColorConversionCodes.GRAY2BGRA);
}
return Output;
}
}
public static SoftwareBitmap MosaicEffect(SoftwareBitmap Input, int Level = 20)
{
if (Level <= 0)
{
throw new ArgumentOutOfRangeException(nameof(Level), "Argument must equals or larger than 0");
}
using (Mat InputMat = Input.SoftwareBitmapToMat())
using (Mat OutputMat = CreateEmptyOutputBitmap(Input.PixelWidth, Input.PixelHeight, out SoftwareBitmap Output))
{
using (Mat TempMat = new Mat(InputMat.Rows, InputMat.Cols, MatType.CV_8UC4))
{
Cv2.CvtColor(InputMat, TempMat, ColorConversionCodes.BGRA2BGR);
for (int i = 0; i < TempMat.Rows; i += Level)
{
for (int j = 0; j < TempMat.Cols; j += Level)
{
Vec3b Vec = TempMat.At<Vec3b>(i, j);
Rect MosaicRect = new Rect(j, i, Math.Min(TempMat.Cols - j, Level), Math.Min(TempMat.Rows - i, Level));
TempMat[MosaicRect] = new Mat(MosaicRect.Size, MatType.CV_8UC3, new Scalar(Vec.Item0, Vec.Item1, Vec.Item2));
}
}
Cv2.CvtColor(TempMat, OutputMat, ColorConversionCodes.BGR2BGRA);
}
return Output;
}
}
public static SoftwareBitmap ResizeToActual(SoftwareBitmap Input)
{
using (Mat InputMat = Input.SoftwareBitmapToMat())
{
Mat[] Channels = Cv2.Split(InputMat);
try
{
if (Channels.Length != 4)
{
throw new ArgumentException("Input must be BGRA image");
}
Mat Contour = Channels.Last().FindNonZero();
Rect ActualArea = Cv2.BoundingRect(Contour);
Rect ExtraArea = new Rect(Math.Max(ActualArea.X - 5, 0), Math.Max(ActualArea.Y - 5, 0), Math.Min(ActualArea.Width + 10, InputMat.Width - ActualArea.X), Math.Min(ActualArea.Height + 10, InputMat.Height - ActualArea.Y));
using (Mat OutputMat = CreateEmptyOutputBitmap(ExtraArea.Width, ExtraArea.Height, out SoftwareBitmap Output))
{
using (Mat TempMat = InputMat[ExtraArea])
{
TempMat.CopyTo(OutputMat);
}
return Output;
}
}
finally
{
Array.ForEach(Channels, (Channel) => Channel.Dispose());
}
}
}
private static Mat CreateEmptyOutputBitmap(int Width, int Height, out SoftwareBitmap Output)
{
Output = new SoftwareBitmap(BitmapPixelFormat.Bgra8, Width, Height, BitmapAlphaMode.Premultiplied);
return Output.SoftwareBitmapToMat();
}
private static unsafe Mat SoftwareBitmapToMat(this SoftwareBitmap Bitmap)
{
using (BitmapBuffer Buffer = Bitmap.LockBuffer(BitmapBufferAccessMode.ReadWrite))
using (Windows.Foundation.IMemoryBufferReference Reference = Buffer.CreateReference())
{
((IMemoryBufferByteAccess)Reference).GetBuffer(out byte* DataInBytes, out _);
return new Mat(Bitmap.PixelHeight, Bitmap.PixelWidth, MatType.CV_8UC4, (IntPtr)DataInBytes);
}
}
}
}