Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Remove noise from image using median filter in VB.net - C#.net.

Author: Shehzad Hemani
Download Source Code : 1070_source.zip

In this simple article you will learn that how we can remove noise from image by applying median filter in VB and C#.net.

 Image is consisting of pixels and each pixel has RGB values. Which means each pixel is made of red green and blue colors. By combining these colors we can get a new color. Each color has its value or intensity. These values lie between 0 to 255.

We can have different kind of noises in an image; one of them is salt and pepper noise. White dots on black image or vice versa. To remove this kind of noise we can apply median filter on image. To apply a filter we need a mask. This mask can be of 3*3 matrix, 5*5 matrix or 7*7 matrix and so on.

To apply the median filter we make a mask of 3*3. In this mask we initialize the entire matrix values with 1. We map the center pixel of matrix on the pixel of image and multiply each matrix pixel with corresponding image pixel, then sort the matrix. After sorting we get the middle value and set that value on the image pixel for that we map the center pixel of matrix. Do this process for each pixel of image. For pixels that are outside the bounds of the image we use zero padding.

First of all you will have to convert the image in gray scale because this filter is only applicable for gray scale images. You can read my previous article to know that how we can convert color image in gray scale. I have also given a code of gray scale conversion in this article.

Example:

Mask:

1          1          1

1          1          1

1          1          1         

First convert in gray scale.

To demonstrate make a window application. Drag two buttons and a picture box on form.

Now write the following code on Button click event:

C#

private void btn_grayscale_Click(object sender, EventArgs e)

        {

            try

            {

                Bitmap img = new Bitmap(pictureBox1.Image);

                Color c;

                for (int i = 0; i < img.Width; i++)

                {

                    for (int j = 0; j < img.Height; j++)

                    {

                        c = img.GetPixel(i, j);

                        int r = 0;

                        r = Convert.ToInt16(c.R);

                        int g = 0;

                        g = Convert.ToInt16(c.G);

                        int b = 0;

                        b = Convert.ToInt16(c.B);

                        int max1 = System.Math.Max(r, g);

                        int final = System.Math.Max(max1, b);

                        r = final;

                        g = final;

                        b = final;

                        c = Color.FromArgb(r, g, b);

                        img.SetPixel(i, j, c);

                    }

                }

                pictureBox1.Image = img;

            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

VB

Private Sub btn_grayscale_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
img As Bitmap = New Bitmap(pictureBox1.Image)
            
Dim As Color
            
Dim As Integer = 0
            
Do While (i < img.Width)
                
Dim As Integer = 0
                
Do While (j < img.Height)
                    c 
img.GetPixel(i, j)
                    
Dim As Integer = 0
                    
Convert.ToInt16(c.R)
                    
Dim As Integer = 0
                    
Convert.ToInt16(c.G)
                    
Dim As Integer = 0
                    
Convert.ToInt16(c.B)
                    
Dim max1 As Integer = System.Math.Max(r, g)
                    
Dim final As Integer = System.Math.Max(max1, b)
                    r 
final
                    g 
final
                    b 
final
                    c 
Color.FromArgb(r, g, b)
                    img.SetPixel(i, j, c)
                    j 
(j + 1)
                
Loop
                
(i + 1)
            
Loop
            
pictureBox1.Image img

        Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to convert image in gray scale.

Now apply the median filter.

C#

private void btn_Median_Click(object sender, EventArgs e)

        {

            Bitmap img = new Bitmap(pictureBox1.Image);

            Color c;

 

            for (int ii = 0; ii < img.Width; ii++)

            {

                for (int jj = 0; jj < img.Height; jj++)

                {

 

                    if (ii - 1 >= 0 && jj - 1 >= 0)

                    {

                        c = img.GetPixel(ii - 1, jj - 1);

                        mask[0] = Convert.ToInt16(c.R);

                    }

                    else

                    {

                        mask[0] = 0;

                    }

 

                    if (jj - 1 >= 0 && ii + 1 < img.Width)

                    {

                        c = img.GetPixel(ii + 1, jj - 1);

                        mask[1] = Convert.ToInt16(c.R);

                    }

                    else

                        mask[1] = 0;

 

                    if (jj - 1 >= 0)

                    {

                        c = img.GetPixel(ii, jj - 1);

                        mask[2] = Convert.ToInt16(c.R);

                    }

                    else

                        mask[2] = 0;

 

                    if (ii + 1 < img.Width)

                    {

                        c = img.GetPixel(ii + 1, jj);

                        mask[3] = Convert.ToInt16(c.R);

                    }

                    else

                        mask[3] = 0;

 

                    if (ii - 1 >= 0)

                    {

                        c = img.GetPixel(ii - 1, jj);

                        mask[4] = Convert.ToInt16(c.R);

                    }

                    else

                        mask[4] = 0;

 

                    if (ii - 1 >= 0 && jj + 1 < img.Height)

                    {

                        c = img.GetPixel(ii - 1, jj + 1);

                        mask[5] = Convert.ToInt16(c.R);

                    }

                    else

                        mask[5] = 0;

 

                    if (jj + 1 < img.Height)

                    {

                        c = img.GetPixel(ii, jj + 1);

                        mask[6] = Convert.ToInt16(c.R);

                    }

                    else

                        mask[6] = 0;

 

 

                    if (ii + 1 < img.Width && jj + 1 < img.Height)

                    {

                        c = img.GetPixel(ii + 1, jj + 1);

                        mask[7] = Convert.ToInt16(c.R);

                    }

                    else

                        mask[7] = 0;

                    c = img.GetPixel(ii, jj);

                    mask[8] = Convert.ToInt16(c.R);

                    Array.Sort(mask);

                    int mid = mask[4];

                    img.SetPixel(ii, jj, Color.FromArgb(mid, mid, mid));

                }

            }

            pictureBox1.Image = img;

            MessageBox.Show("successfully Done");

        }

VB

Private Sub btn_Median_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim img As Bitmap = New Bitmap(pictureBox1.Image)
        
Dim As Color
        
Dim ii As Integer = 0
        
Do While (ii < img.Width)
            
Dim jj As Integer = 0
            
Do While (jj < img.Height)
                
If (((ii - 1)  _
                            >
0)  _
                            
AndAlso ((jj - 1)  _
                            >
0)) Then
                    
img.GetPixel((ii - 1), (jj - 1))
                    mask(
0Convert.ToInt16(c.R)
                
Else
                    
mask(00
                
End If
                If 
(((jj - 1)  _
                            >
0)  _
                            
AndAlso (ii + (< img.Width))) Then
                    
img.GetPixel((ii + 1), (jj - 1))
                    mask(
1Convert.ToInt16(c.R)
                
Else
                    
mask(10
                
End If
                If 
((jj - 1)  _
                            >
0Then
                    
img.GetPixel(ii, (jj - 1))
                    mask(
2Convert.ToInt16(c.R)
                
Else
                    
mask(20
                
End If
                If 
(ii + (< img.Width)) Then
                    
img.GetPixel((ii + 1), jj)
                    mask(
3Convert.ToInt16(c.R)
                
Else
                    
mask(30
                
End If
                If 
((ii - 1)  _
                            >
0Then
                    
img.GetPixel((ii - 1), jj)
                    mask(
4Convert.ToInt16(c.R)
                
Else
                    
mask(40
                
End If
                If 
(((ii - 1)  _
                            >
0)  _
                            
AndAlso (jj + (< img.Height))) Then
                    
img.GetPixel((ii - 1), (jj + 1))
                    mask(
5Convert.ToInt16(c.R)
                
Else
                    
mask(50
                
End If
                If 
(jj + (< img.Height)) Then
                    
img.GetPixel(ii, (jj + 1))
                    mask(
6Convert.ToInt16(c.R)
                
Else
                    
mask(60
                
End If
                If 
((ii + (< img.Width))  _
                            
AndAlso (jj + (< img.Height))) Then
                    
img.GetPixel((ii + 1), (jj + 1))
                    mask(
7Convert.ToInt16(c.R)
                
Else
                    
mask(70
                
End If
                
img.GetPixel(ii, jj)
                mask(
8Convert.ToInt16(c.R)
                
Array.Sort(mask)
                
Dim mid As Integer = mask(4)
                img.SetPixel(ii, jj, Color.FromArgb(
midmidmid))
                jj 
(jj + 1)
            
Loop
            
ii (ii + 1)
        
Loop
        
pictureBox1.Image img
        MessageBox.Show(
"successfully Done")
    
End Sub

This is simple code to remove noise from image using median filter.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {          

                this.Text = "DEVASP Image APPLICATION";

                for (int i = 0; i < 9; i++)

                {

                    mask[i] = 1;

                }

        }

VB

 
    
Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text "DEVASP Image APPLICATION"
        
Dim As Integer = 0
        
Do While (i < 9)
            mask(i) 
1
            
(i + 1)
        
Loop
    End Sub

This simple article tells that how we can remove noise from image by applying median filter in VB and C#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Remove noise from image using mean filter in VB.net - C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net