Last active
December 23, 2015 14:19
-
-
Save RECS-Tsukuba/6648163 to your computer and use it in GitHub Desktop.
x軸方向のsobelフィルタのサンプル(C#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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