Image edge extraction (Laplacian filter)

less than 1 minute read

Boundary (edge) extraction example

無題.png

Basics and applications of digital image processing
p.45
Laplacian filter

Laplacian1.cs



//c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Laplacian1.cs

using System.Drawing;
using System.Drawing.Imaging;
using System;

public class Laplacian1
{
 public static void Main(string[] args)
 {
    if(args.Length==2){
    string path1;//in file name
    string path2;//out file name

    path1 = @".\" + args[0];
    path2 = @".\" + args[1];

    Bitmap image1;
    image1 = new Bitmap(path1, true);

	int i,j,nx,ny;
	int gray;
	Color col;
	//int[,] f = new int[256,256];//← The source of the book is incorrect

	nx = (int)image1.Width;
	ny = (int)image1.Height;

	int[,] f = new int[nx,ny];


	Bitmap bmp = new Bitmap(image1);

	for(j=0;j<ny;j++){
		for(i=0;i<nx;i++){
			col = bmp.GetPixel(i,j);
			f[i,j] = (int)((col.R+col.G+col.B)/3);
		}
	}

	Console.WriteLine(" _1 ");

	for(j=1;j<ny-1;j++){
		for(i=1;i<nx-1;i++){
			gray=f[i,j-1] + f[i-1,j] - 4*f[i,j] + f[i+1,j] + f[i,j+1];
			gray += 128; 
			if(gray>158){ //The threshold is appropriate
				gray=0;
			}else{
				gray=255;
			}
			bmp.SetPixel(i,j,Color.FromArgb(gray,gray,gray));
		}
	}

	Console.WriteLine(" _2 ");
    bmp.Save(path2, System.Drawing.Imaging.ImageFormat.Png);
	Console.WriteLine(" _3 ");
	return;

    }
 }

}

Tags:

Updated: