Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ImageProcessing
Hey friends please help me in coding median filter to remove noise from the fingerprint image using pointers. I have done this much...
 
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 4-Mar-11 7:27am
Edited 4-Mar-11 8:04am
Henry Minute223.9K
v2
Comments
SAKryukov at 4-Mar-11 15:18pm
   
You have done this, and...
--SA
sairam.bhat at 5-Apr-11 0:55am
   
thank you good question i upvoted +5

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Noise Reduction of an Image in C# using Median Filters
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:
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);
 29:                             RValues.Add(TempColor.R);
 30:                             GValues.Add(TempColor.G);
 31:                             BValues.Add(TempColor.B);
 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: }
  Permalink  
Comments
Noel3090 at 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 at 24-Jul-13 9:56am
   
thank you c# code MedianFilter Function good work.
sairam.bhat at 5-Apr-11 0:42am
   
Thank you for accepting my answer
shehee at 14-Apr-13 6:49am
   
thank you for the help. Thank you very much again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 325
1 Sergey Alexandrovich Kryukov 289
2 CPallini 275
3 DamithSL 260
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 24 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100