title: "Python實現圖像邊緣檢測算法"
date: 2018-06-12T17:06:53+08:00
tags: ["圖形學"]
categories: ["python"]python
#!/usr/bin/env python3 # coding=utf-8 from PIL import Image import numpy as np img_name = input("輸入要處理的圖片\n") # img_name = "t3.png" img = Image.open(img_name).convert("L") # 讀圖片並轉化爲灰度圖 img.show() img_array = np.array(img) # 轉化爲數組 w, h = img_array.shape img_border = np.zeros((w-1, h-1)) for x in range(1, w - 1): for y in range(1, h - 1): Sx = img_array[x + 1][y - 1] + 2 * img_array[x + 1][y] + img_array[x + 1][y + 1] - \ img_array[x - 1][y - 1] - 2 * \ img_array[x - 1][y] - img_array[x - 1][y + 1] Sy = img_array[x - 1][y + 1] + 2 * img_array[x][y + 1] + img_array[x + 1][y + 1] - \ img_array[x - 1][y - 1] - 2 * \ img_array[x][y - 1] - img_array[x + 1][y - 1] img_border[x][y] = (Sx * Sx + Sy * Sy) ** 0.5 img2 = Image.fromarray(img_border) img2.show()