Last active
December 23, 2015 14:19
-
-
Save RECS-Tsukuba/6648180 to your computer and use it in GitHub Desktop.
x軸方向のsobelフィルタのサンプル(Python)
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
# coding: UTF-8 | |
from PIL import Image | |
# 画像を取得 | |
image = Image.open('input.png', 'r').convert('L') | |
# サイズを取得 | |
width, height = image.size | |
# 出力画像を生成 | |
filtered = image.copy() | |
for x in range(1, image.size[0] - 1): | |
for y in range(1, image.size[1] - 1): | |
# ============この部分を書き換える=============== | |
# ここでは例としてx軸方向のsobelフィルタの処理を記述する | |
# x軸方向のsobelフィルタを適用 | |
x_sobel = (image.getpixel((x + 1, y - 1)) \ | |
+ 2 * image.getpixel((x + 1, y)) \ | |
# (x + 1, y + 1)の画素を取得 | |
+ image.getpixel((x + 1, y + 1))) \ | |
- (image.getpixel((x - 1, y - 1)) \ | |
+ 2 * image.getpixel((x - 1, y)) \ | |
+ image.getpixel((x - 1, y + 1))) | |
# 画素を設定 | |
filtered.putpixel((x, y), abs(x_sobel / 4)) | |
# =============================================== | |
# 画像を出力 | |
filtered.save('output.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment