1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

画像のエッジ抽出(ラプラシアンフィルタ)

Posted at

境界(エッジ)抽出例

無題.png

ディジタル画像処理の基礎と応用
p.45
ラプラシアンフィルタ

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];//← 書籍のソースは誤り

	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){ //閾値は、適当に
				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;

    }
 }

}

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?