图像膨胀、腐蚀算法实现 python源码

如果您觉得本文不错!记得点赞哦!


一. 图像形态学简介:

图解图像腐蚀、膨胀 ↑

    经验之谈:形态学操作一般作用于二值图像,来连接相邻的元素(膨胀)或分离成独立的元素(侵蚀)。腐蚀和膨胀是针对图片中的白色(即前景)部分!


二. 图像形态学操作 膨胀和腐蚀的算法:

    膨胀算法:

        对于待操作的像素 f(x,y),不论 f(x,y-1) 、f(x,y+1) 、f(x-1,y) 、f(x+1,y) 哪一个为255,则 f(x,y)=255。

膨胀操作 ↑

 

        换句话说:将待操作的图像像素与以下  4-近邻矩阵 相乘,结果大于255的话,将中心像素设为255。

膨胀:待操作像素 * 上面矩阵 > =255,f(x,y) = 255。 ↑

 

    腐蚀算法:

        对于待操作的像素 f(x,y),只有 f(x,y-1) 、f(x,y+1) 、f(x-1,y) 、f(x+1,y) 都为255,则 f(x,y)=255。

        换句话说:将待操作的图像像素与以下  4-近邻矩阵 相乘,结果小于255*4的话,将中心像素设为0。

腐蚀:待操作像素 * 上面矩阵 < 255*4,f(x,y) = 0 。↑


三. python实现图像膨胀和腐蚀

# Writer : wojianxinygcl@163.com
# Date   : 2020.3.21
import cv2
import numpy as np
import matplotlib.pyplot as plt

# 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

# Otsu Binalization
def otsu_binarization(img, th=128):
	H, W = img.shape
	out = img.copy()

	max_sigma = 0
	max_t = 0

	# determine threshold
	for _t in range(1, 255):
		v0 = out[np.where(out < _t)]
		m0 = np.mean(v0) if len(v0) > 0 else 0.
		w0 = len(v0) / (H * W)
		v1 = out[np.where(out >= _t)]
		m1 = np.mean(v1) if len(v1) > 0 else 0.
		w1 = len(v1) / (H * W)
		sigma = w0 * w1 * ((m0 - m1) ** 2)
		if sigma > max_sigma:
			max_sigma = sigma
			max_t = _t

	# Binarization
	print("threshold >>", max_t)
	th = max_t
	out[out < th] = 0
	out[out >= th] = 255

	return out


# Morphology Dilate
def Morphology_Dilate(img, Dil_time=1):
	H, W = img.shape

	# kernel
	MF = np.array(((0, 1, 0),
				(1, 0, 1),
				(0, 1, 0)), dtype=np.int)

	# each dilate time
	out = img.copy()
	for i in range(Dil_time):
		tmp = np.pad(out, (1, 1), 'edge')
		for y in range(1, H):
			for x in range(1, W):
				if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255:
					out[y, x] = 255

	return out


# Morphology Erode
def Morphology_Erode(img, Erode_time=1):
	H, W = img.shape
	out = img.copy()

	# kernel
	MF = np.array(((0, 1, 0),
				(1, 0, 1),
				(0, 1, 0)), dtype=np.int)

	# each erode
	for i in range(Erode_time):
		tmp = np.pad(out, (1, 1), 'edge')
		# erode
		for y in range(1, H):
			for x in range(1, W):
				if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4:
					out[y, x] = 0

	return out


# Read image
img = cv2.imread("../paojie.jpg").astype(np.float32)

# Grayscale
gray = BGR2GRAY(img)

# Otsu's binarization
otsu = otsu_binarization(gray)

# Morphology - dilate
erode_result = Morphology_Erode(otsu, Erode_time=2)
dilate_result = Morphology_Dilate(otsu,Dil_time=2)

# Save result
cv2.imwrite("Black_and_white.jpg",otsu)
cv2.imshow("Black_and_white",otsu)
cv2.imwrite("erode_result.jpg", erode_result)
cv2.imshow("erode_result", erode_result)
cv2.imwrite("dilate_result.jpg", dilate_result)
cv2.imshow("dilate_result",dilate_result)
cv2.waitKey(0)
cv2.destroyAllWindows()

四. 实验结果:

二值图像(左),膨胀图像(中),侵蚀图像(右) ↑


五. 参考内容:

    ①  https://www.jianshu.com/p/ba2cec49c981

    ②  https://www.cnblogs.com/wojianxin/p/12542004.html


六. 版权声明:

    未经作者允许,请勿随意转载抄袭,抄袭情节严重者,作者将考虑追究其法律责任,创作不易,感谢您的理解和配合!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值