Skip to content

Instantly share code, notes, and snippets.

@RECS-Tsukuba
Last active December 23, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RECS-Tsukuba/6648163 to your computer and use it in GitHub Desktop.
Save RECS-Tsukuba/6648163 to your computer and use it in GitHub Desktop.
x軸方向のsobelフィルタのサンプル(C#)
using System;
using System.Drawing;
namespace image_filter
{
class MainClass
{
public static Byte getGrayScale(Color c)
{
return (Byte)((c.A + c.G + c.B) / 3);
}
public static void Main()
{
// ファイルの読み込み
using (Bitmap input = new Bitmap(@"input.png"))
{
// 出力画像の生成
using (Bitmap filtered = new Bitmap(input))
{
for (int x = 1; x < input.Width - 1; ++x)
{
for (int y = 1; y < input.Height - 1; ++y)
{
// ============この部分を書き換える===============
// ここでは例としてx軸方向のsobelフィルタの処理を記述する
int x_sobel = (
getGrayScale(input.GetPixel(x + 1, y + 1)) +
2 * getGrayScale(input.GetPixel(x + 1, y)) +
getGrayScale(input.GetPixel(x + 1, y - 1))
) - (
getGrayScale(input.GetPixel(x - 1, y + 1)) +
2 * getGrayScale(input.GetPixel(x - 1, y)) +
getGrayScale(input.GetPixel(x - 1, y - 1))
);
Byte pixel = (Byte)(Math.Abs(x_sobel));
// ===============================================
filtered.SetPixel(
x, y, Color.FromArgb(pixel, pixel, pixel)
);
}
}
// 画像を出力
filtered.Save(@"output.png");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment