-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedianFilter.cs
More file actions
57 lines (52 loc) · 1.65 KB
/
MedianFilter.cs
File metadata and controls
57 lines (52 loc) · 1.65 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Drawing.Imaging;
using System.Windows.Forms;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
namespace VKR
{
internal class MedianFilter
{
public static Bitmap MFilter(Bitmap bm)
{
List<byte> elementList = new List<byte>();
byte[,] image = new byte[bm.Width, bm.Height];
//в чб
for (int j = 0; j < bm.Height; j++)
{
for (int i = 0; i < bm.Width; i++)
{
var c = bm.GetPixel(i, j);
byte gray = (byte)(.333 * c.R + .333 * c.G + .333 * c.B);
image[i, j] = gray;
}
}
//сам фильтр
for (int j = 0; j <= bm.Height - 3; j++)
for (int i = 0; i <= bm.Width - 3; i++)
{
for (int x = i; x <= i + 2; x++)
for (int y = j; y <= j + 2; y++)
{
elementList.Add(image[x, y]);
}
byte[] elements = elementList.ToArray();
elementList.Clear();
Array.Sort<byte>(elements);
Array.Reverse(elements);
byte color = elements[4];
bm.SetPixel(i + 1, j + 1, Color.FromArgb(color, color, color));
}
return bm;
}
}
}