使用差分金字塔提取图像边缘 python实现 本文提供一种提取图像边缘的快速算法

更多提取图像边缘的方法:
最大-最小滤波器实现图像边缘提取
Prewitt滤波器和Sobel滤波器提取图像边缘
拉普拉斯滤波器提取图像边缘

差分金字塔提取图像边缘算法:

将原图像先缩小2倍,再放大2倍(都使用双线性插值算法)[在此过程中主要是图像的高频部分被模糊了]。得到了原图像的模糊图像。将原图像减去模糊图像,得到了图像的边缘。就是这么简单!

实验代码

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Grayscale
def BGR2GRAY(img):
	# Grayscale
	gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
	return gray

# Bi-Linear interpolation
def bl_interpolate(img, ax=1., ay=1.):
	if len(img.shape) > 2:
		H, W, C = img.shape
	else:
		H, W = img.shape
		C = 1

	aH = int(ay * H)
	aW = int(ax * W)

	# get position of resized image
	y = np.arange(aH).repeat(aW).reshape(aW, -1)
	x = np.tile(np.arange(aW), (aH, 1))

	# get position of original position
	y = (y / ay)
	x = (x / ax)

	ix = np.floor(x).astype(np.int)
	iy = np.floor(y).astype(np.int)

	ix = np.minimum(ix, W-2)
	iy = np.minimum(iy, H-2)

	# get distance 
	dx = x - ix
	dy = y - iy

	if C > 1:
		dx = np.repeat(np.expand_dims(dx, axis=-1), C, axis=-1)
		dy = np.repeat(np.expand_dims(dy, axis=-1), C, axis=-1)

	# interpolation
	out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1]

	out = np.clip(out, 0, 255)
	out = out.astype(np.uint8)

	return out


# Read image
img = cv2.imread("../bird.png").astype(np.float)

gray = BGR2GRAY(img)

# Bilinear interpolation
out = bl_interpolate(gray.astype(np.float32), ax=0.5, ay=0.5)

# Bilinear interpolation
out = bl_interpolate(out, ax=2., ay=2.)

out = np.abs(out - gray)

out = out / out.max() * 255

out = out.astype(np.uint8)

# Save result
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.imwrite("out.jpg", out)

实验结果:

使用差分金字塔提取图像边缘 python实现
本文提供一种提取图像边缘的快速算法
使用差分金字塔提取图像边缘 python实现
本文提供一种提取图像边缘的快速算法

点个赞再走呗,谢谢了!