一. 边缘检测
边缘检测通常作用于灰度图像上。边缘检测用于检测图像中的线。

边缘检测结果 ↑
二. 最大 - 最小滤波器(Max - Min)

3*3 Max-Min Filter 算法原理 ↑
我们知道,图像的细节属于低频信息,图像的边缘属于高频信息。我们使用一定大小的 Max-Min 滤波器作用于图像,当滤波器作用于图像细节时,输出结果往往趋向于0(黑色);而滤波器作用于图像边缘时,Max-Min 输出结果往往趋向于255(白色)。所以 最大-最小滤波器 能有效地用于检测图像的边缘和轮廓。
三. python实现 最大-最小 滤波器
用3*3的 Max-Min 滤波器对图像进行边缘检测
import cv2
import numpy as np
# BGR to Gray scale
def BGR2GRAY(img):
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()
# Gray scale
out = 0.2126 * r + 0.7152 * g + 0.0722 * b
out = out.astype(np.uint8)
return out
# max-min filter
def max_min_filter(img, K_size=3):
H, W = img.shape
# Zero padding
pad = K_size // 2
out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float)
out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float)
tmp = out.copy()
# filtering
for y in range(H):
for x in range(W):
# core code
out[pad + y, pad + x] = np.max(tmp[y: y + K_size, x: x + K_size]) - \
np.min(tmp[y: y + K_size, x: x + K_size])
out = out[pad: pad + H, pad: pad + W].astype(np.uint8)
return out
# Read image
img = cv2.imread("../paojie.jpg").astype(np.float)
# get grayscale
gray = BGR2GRAY(img)
# Max-Min filtering
out = max_min_filter(gray, K_size=3)
# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
四. 实验结果:

边缘检测结果 ↑

原图 ↑
五. 参考内容:
https://www.jianshu.com/p/fea3b4d741ab
来源:https://www.cnblogs.com/wojianxin/p/12502812.html