罗伯特梯度法(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算子法的基本原理是:计算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
与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算子
################################################
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()
结果如下: