15,610,660 members
5.00/5 (1 vote)
See more:
Hey friends please help me in coding median filter to remove noise from the fingerprint image using pointers. I have done this much...

C#
```byte* imgPtr = (byte*)(void*)(data.Scan0);
byte* src = (byte*)(void*)(data.Scan0);
int sOffset = stride - 9;
int nOffset = stride - image.Width * 3;
int i,j,v = 0,h=0;
for ( i = 0; i < image.Width; i++)
{
for ( j = 1; j < image.Height; j++)
{
imgPtr = src;
for(x=0;x<3;x++)
{
for (y = 0; y <3; y++)
{
red[x, y] = imgPtr[0];
r1[h] = imgPtr[0];
green[x, y] = imgPtr[1];
g1[h] = imgPtr[1];
blue[x, y] = imgPtr[2];
b1[h] = imgPtr[2];
imgPtr += 3;
h++;
}
imgPtr += sOffset;
}
src += 3;
Array.Sort(r1);
r[v] = r1[4];
Array.Sort(g1);
g[v] = g1[4];
Array.Sort(b1);
b[v] = b1[4];
//MPtr += 3;
v++;```
Posted
Updated 4-Mar-11 7:04am
v2
Sergey Alexandrovich Kryukov 4-Mar-11 15:18pm
You have done this, and...
--SA
sairam.bhat 5-Apr-11 0:55am
thank you good question i upvoted +5

## Solution 1

Noise Reduction of an Image in C# using Median Filters
VB
```One of the main issues when trying to do image processing is the simple fact that images usually contain some degree of noise. This noise can in turn cause issues. For instance if you're doing edge detection, a spot on the image may cause the algorithm to detect edges that it shouldn't. One of the easiest ways to fix this issue is to use a median filter on an image. Unlike box blurs and gaussian blurs, we're not looking for the average of the pixels. In the case of a median filter, we're looking for the median (sort the values, take the one in the middle). We're still looking at a set of pixels around each pixel but we're simply taking the median instead of the mean.  The advantage of this approach is that it keeps the edges of an image but simply degrades the details a bit, unlike a box blur which will slowly bleed the edges.
Unfortunately in C# there is no built in function to do this (or at least none that I'm aware of) and as such I wrote code to accomplish it and as always I'm sharing it with you:```

XML
```1: public static Bitmap MedianFilter(Bitmap Image, int Size)
2: {
3:     System.Drawing.Bitmap TempBitmap = Image;
4:     System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
5:     System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
6:     NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
7:     NewGraphics.Dispose();
8:     Random TempRandom = new Random();
9:     int ApetureMin = -(Size / 2);
10:     int ApetureMax = (Size / 2);
11:     for (int x = 0; x < NewBitmap.Width; ++x)
12:     {
13:         for (int y = 0; y < NewBitmap.Height; ++y)
14:         {
15:             List<int> RValues = new List<int>();
16:             List<int> GValues = new List<int>();
17:             List<int> BValues = new List<int>();
18:             for (int x2 = ApetureMin; x2 < ApetureMax; ++x2)
19:             {
20:                 int TempX = x + x2;
21:                 if (TempX >= 0 && TempX < NewBitmap.Width)
22:                 {
23:                     for (int y2 = ApetureMin; y2 < ApetureMax; ++y2)
24:                     {
25:                         int TempY = y + y2;
26:                         if (TempY >= 0 && TempY < NewBitmap.Height)
27:                         {
28:                             Color TempColor = TempBitmap.GetPixel(TempX, TempY);
32:                         }
33:                     }
34:                 }
35:             }
36:             RValues.Sort();
37:             GValues.Sort();
38:             BValues.Sort();
39:             Color MedianPixel = Color.FromArgb(RValues[RValues.Count / 2],
40:                 GValues[GValues.Count / 2],
41:                 BValues[BValues.Count / 2]);
42:             NewBitmap.SetPixel(x, y, MedianPixel);
43:         }
44:     }
45:     return NewBitmap;
46: }```

Noel3090 5-Mar-11 1:49am
Thanks sairam, but dont you think this will take time to process 500x500 image. I wanted to try with unsafe method which is quite faster!! Can you please help me with this
dogansoft 24-Jul-13 9:56am
thank you c# code MedianFilter Function good work.
sairam.bhat 5-Apr-11 0:42am
Thank you for accepting my answer
shehee 14-Apr-13 6:49am
thank you for the help. Thank you very much again
yudha rizki 6-Apr-15 6:16am
how to call the medianFilter method?
it doesn't work for me

private void OnMedianFilter(object sender, System.EventArgs ){
BitmapFilter.MedianFilter(m_Bitmap, 9);
this.Invalidate();
}