Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9529

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

$
0
0

境界(エッジ)抽出例

無題.png

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

Laplacian1.cs
//c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Laplacian1.csusingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem;publicclassLaplacian1{publicstaticvoidMain(string[]args){if(args.Length==2){stringpath1;//in file namestringpath2;//out file namepath1=@".\"+args[0];path2=@".\"+args[1];Bitmapimage1;image1=newBitmap(path1,true);inti,j,nx,ny;intgray;Colorcol;//int[,] f = new int[256,256];//← 書籍のソースは誤りnx=(int)image1.Width;ny=(int)image1.Height;int[,]f=newint[nx,ny];Bitmapbmp=newBitmap(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;}}}

Viewing all articles
Browse latest Browse all 9529

Trending Articles