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
 

Edge sharpening in an image using laplacian filter in VB.net - C#.net.

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

In this simple article you will learn that how we can do edge sharpening in an image by laplacian filter in VB and C#.net.

 To sharp the edges of an image we can apply laplacian filter on image. To apply a filter we need a mask. This mask can be of 3*3 matrix.

To apply the laplacian filter we make a mask of 3*3. In this mask we initialize the entire matrix with different values as mentioned below. We map the center pixel of matrix on the pixel of image and multiply each matrix pixel with corresponding image pixel and add them. 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 convert image into grayscale then apply median filter to remove the noise and then apply laplacian filter.

Mask:

0          1          0

1          -4         1

0          1          0         

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

Now write the following code on Button click event:

C#

private void btn_laplacian_Click(object sender, EventArgs e)

         {

             Bitmap img = new Bitmap(pictureBox1.Image);

             Bitmap img1 = 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] = 0;

                       }

                       else

                       {

                           mask[0] = 0;

                       }

 

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

                       {

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

                           mask[1] = 0;

                       }

                       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] = 0;

                       }

                       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] = 0;

                       }

                       else

                           mask[7] = 0;

                       c = img.GetPixel(ii, jj);

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

                       int sum = 0;

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

                           sum = sum + mask[u];

                       long r1 = Convert.ToInt64(sum);

                       long g1 = Convert.ToInt64(sum);

                       long b1 = Convert.ToInt64(sum);

                       g1 = Convert.ToInt64(g1 << 8);

                       b1 = Convert.ToInt64(b1 << 16);

                       int ans = Convert.ToInt16(((byte)r1 | (byte)g1 | (byte)b1));

                       img1.SetPixel(ii, jj, Color.FromArgb(ans,ans,ans));

                   }

               }

             pictureBox1.Image = img1;

             MessageBox.Show("successfully Done");

         }

VB

Private Sub btn_laplacian_Click(ByVal sender As ObjectByVal As EventArgs)
        
Dim img As Bitmap = New Bitmap(pictureBox1.Image)
        
Dim img1 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(
00
                
Else
                    
mask(00
                
End If
                If 
(((jj - 1)  _
                            >
0)  _
                            
AndAlso (ii + (< img.Width))) Then
                    
img.GetPixel((ii + 1), (jj - 1))
                    mask(
10
                
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(
50
                
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(
70
                
Else
                    
mask(70
                
End If
                
img.GetPixel(ii, jj)
                mask(
8(Convert.ToInt16(c.R) * -4)
                
Dim sum As Integer = 0
                
Dim As Integer = 0
                
Do While (u < 9)
                    sum 
(sum + mask(u))
                    u 
(u + 1)
                
Loop
                Dim 
r1 As Long = Convert.ToInt64(sum)
                
Dim g1 As Long = Convert.ToInt64(sum)
                
Dim b1 As Long = Convert.ToInt64(sum)
                g1 
Convert.ToInt64((g1 + 8))
                b1 
Convert.ToInt64((b1 + 16))
                
Dim ans As Integer = Convert.ToInt16((CType(r1,Byte)  _
                                
Or (CType(g1,ByteOr CType(b1,Byte))))
                img1.SetPixel(ii, jj, Color.FromArgb(ans, ans, ans))
                jj 
(jj + 1)
            
Loop
            
ii (ii + 1)
        
Loop
        
pictureBox1.Image img1
        MessageBox.Show(
"successfully Done")
    
End Sub

This is simple code to sharpen the edges in an image.

 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 do edge sharpening in an image by laplacian filter in VB and C#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Convert invalid – out of range RGB value into valid RGB value in VB.net - C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net