Skip to content

Instantly share code, notes, and snippets.

@RECS-Tsukuba RECS-Tsukuba/Main.cs
Last active Dec 23, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.