一、Robert算子
羅伯特梯度法(Robert Gradient), 是一種交叉差分方法。其數學表達式可近似爲:
G[f(x, y)] ≈|f(i, j)-f(i+1, j+1) |+|f(i+1, j)-f(i, j+1)|
################################################
# Robert算子
################################################
def robert_filter(image):
h = image.shape[0]
w = image.shape[1]
image_new = np.zeros(image.shape, np.uint8)
for i in range(1, h-1):
for j in range(1, w-1):
image_new[i][j] = np.abs((image[i][j]-image[i+1][j+1])) + np.abs(image[i+1][j]-image[i][j+1])
return image_new
二、Sobel算子
採用梯度微分銳化圖像,同時會使噪聲、條紋等得到增強, Sobel算子則在一定程度上克服了這個問題。Sobel算子法的基本原理是:計算3×3窗口的灰度, 將其作爲變換後圖像g(i, j)的灰度。公式如下:
################################################
# Sobel算子
################################################
def sobel_filter(image):
h = image.shape[0]
w = image.shape[1]
image_new = np.zeros(image.shape, np.uint8)
for i in range(1, h-1):
for j in range(1, w-1):
sx = (image[i + 1][j - 1] + 2 * image[i + 1][j] + image[i + 1][j + 1]) - \
(image[i - 1][j - 1] + 2 * image[i - 1][j] + image[i - 1][j + 1])
sy = (image[i - 1][j + 1] + 2 * image[i][j + 1] + image[i + 1][j + 1]) - \
(image[i - 1][j - 1] + 2 * image[i][j - 1] + image[i + 1][j - 1])
image_new[i][j] = np.sqrt(np.square(sx) + np.square(sy))
return image_new
三、Prewitt算子
與Sobel相比,Prewitt算子有一定的抗干擾性,圖像效果比較乾淨。
公式如下:
################################################
# Prewitt算子
################################################
def prewitt_filter(image):
h = image.shape[0]
w = image.shape[1]
image_new = np.zeros(image.shape, np.uint8)
for i in range(1, h-1):
for j in range(1, w-1):
sx = (image[i - 1][j - 1] + image[i - 1][j] + image[i - 1][j + 1]) - \
(image[i + 1][j - 1] + image[i + 1][j] + image[i + 1][j + 1])
sy = (image[i - 1][j - 1] + image[i][j - 1] + image[i + 1][j - 1]) - \
(image[i - 1][j + 1] + image[i][j + 1] + image[i + 1][j + 1])
image_new[i][j] = np.sqrt(np.square(sx) + np.square(sy))
return image_new
四、Laplacian算子
拉普拉斯運算是偏導數運算的線性組合運算,屬於二階微分運算。與以上三類一階微分運算相比,Laplacian算子獲得的邊界更爲細緻,包含了更多信息,
公式如下:
################################################
# Laplacian算子
################################################
def laplacian_filter(image):
h = image.shape[0]
w = image.shape[1]
image_new = np.zeros(image.shape, np.uint8)
for i in range(1, h-1):
for j in range(1, w-1):
image_new[i][j] = image[i + 1][j] + image[i - 1][j] + image[i][j + 1] + image[i][j - 1] - 8 * image[i][j]
return image_new
五、完整代碼
import numpy as np
import matplotlib.pyplot as plt
import random
################################################
# Robert算子
################################################
def robert_filter(image):
h = image.shape[0]
w = image.shape[1]
image_new = np.zeros(image.shape, np.uint8)
for i in range(1, h-1):
for j in range(1, w-1):
image_new[i][j] = np.abs((image[i][j]-image[i+1][j+1])) + np.abs(image[i+1][j]-image[i][j+1])
return image_new
################################################
# Sobel算子
################################################
def sobel_filter(image):
h = image.shape[0]
w = image.shape[1]
image_new = np.zeros(image.shape, np.uint8)
for i in range(1, h-1):
for j in range(1, w-1):
sx = (image[i + 1][j - 1] + 2 * image[i + 1][j] + image[i + 1][j + 1]) - \
(image[i - 1][j - 1] + 2 * image[i - 1][j] + image[i - 1][j + 1])
sy = (image[i - 1][j + 1] + 2 * image[i][j + 1] + image[i + 1][j + 1]) - \
(image[i - 1][j - 1] + 2 * image[i][j - 1] + image[i + 1][j - 1])
image_new[i][j] = np.sqrt(np.square(sx) + np.square(sy))
return image_new
################################################
# Prewitt算子
################################################
def prewitt_filter(image):
h = image.shape[0]
w = image.shape[1]
image_new = np.zeros(image.shape, np.uint8)
for i in range(1, h-1):
for j in range(1, w-1):
sx = (image[i - 1][j - 1] + image[i - 1][j] + image[i - 1][j + 1]) - \
(image[i + 1][j - 1] + image[i + 1][j] + image[i + 1][j + 1])
sy = (image[i - 1][j - 1] + image[i][j - 1] + image[i + 1][j - 1]) - \
(image[i - 1][j + 1] + image[i][j + 1] + image[i + 1][j + 1])
image_new[i][j] = np.sqrt(np.square(sx) + np.square(sy))
return image_new
################################################
# Laplacian算子
################################################
def laplacian_filter(image):
h = image.shape[0]
w = image.shape[1]
image_new = np.zeros(image.shape, np.uint8)
for i in range(1, h-1):
for j in range(1, w-1):
image_new[i][j] = image[i + 1][j] + image[i - 1][j] + image[i][j + 1] + image[i][j - 1] - 8 * image[i][j]
return image_new
#############################################################################
if __name__ == "__main__":
img = plt.imread("1.jpg")
rgb_weight = [0.299, 0.587, 0.114]
img_gray = np.dot(img, rgb_weight)
################################################
# 原圖
################################################
plt.subplot(241)
plt.imshow(img)
plt.xticks([])
plt.yticks([])
plt.title("Original")
################################################
# 灰度圖
################################################
plt.subplot(242)
plt.imshow(img_gray, cmap=plt.cm.gray)
plt.xticks([])
plt.yticks([])
plt.title("Gray")
################################################
# Robert算子
################################################
img_Robert = robert_filter(img_gray)
img_Robert = img_Robert.astype(np.float64)
plt.subplot(245)
plt.imshow(img_Robert, cmap=plt.cm.gray)
plt.xticks([])
plt.yticks([])
plt.title("robert_filter")
################################################
# Sobel算子
################################################
img_Sobel = sobel_filter(img_gray)
img_Sobel = img_Sobel.astype(np.float64)
plt.subplot(246)
plt.imshow(img_Sobel, cmap=plt.cm.gray)
plt.xticks([])
plt.yticks([])
plt.title("sobel_filter")
################################################
# Prewitt算子
################################################
img_Prewitt = prewitt_filter(img_gray)
img_Prewitt = img_Prewitt.astype(np.float64)
plt.subplot(247)
plt.imshow(img_Prewitt, cmap=plt.cm.gray)
plt.xticks([])
plt.yticks([])
plt.title("prewitt_filter")
################################################
# Laplacian算子
################################################
img_Laplacian = laplacian_filter(img_gray)
img_Laplacian = img_Laplacian.astype(np.float64)
plt.subplot(248)
plt.imshow(img_Laplacian, cmap=plt.cm.gray)
plt.xticks([])
plt.yticks([])
plt.title("laplacian_filter")
plt.show()
結果如下: