python实现形态学图像处理-膨胀操作

膨胀:是使图像中的目标“生长”或“变粗”的操作。这个特殊的方法和变粗程度由被称为结构元的形状来控制【1】。

对断开的字符文本的二值图像进行膨胀操作。

 

图像来源https://www.google.com.hk/url?sa=i&url=https%3A%2F%2Fdocplayer.net%2F49492134-Morphological-image-processing.html&psig=AOvVaw3TTwbqVGfz-5FcqkN3Rjte&ust=1585883115595000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPDF-pThyOgCFQAAAAAdAAAAABAD

(1)数据分析:图像像素值分布范围[0, 255]

(2)数据处理:选择阀值200,像素值大于或等于200的,重新设置为1;像素值小于200的,重新设置为0;

(3)结构元选择:大小3x3, [[0,1,0],[1,1,1],[0,1,0]]

 (4)代码主要思路:遍历图像的像素点,像素点的”灰度值“与结构元的原点,相同时候(都为真);对该像素点的3x3邻域与结构元对应位置,进行判断;如果像素点的值为0,而对应结构元的值为1,则将该像素点的值重设为1。

代码实现:

import skimage.io as io
import matplotlib.pyplot as plt
import numpy as np
import copy
A = io.imread("/Applications/Python/Image/morphological operation/broken_text.jpg")
from skimage.morphology.binary import binary_erosion,binary_dilation
from skimage.morphology import dilation

# B = np.eye(3) # 定义3x3的对角矩阵
# B = np.array([[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]])
B = np.array([[0,1,0],[1,1,1],[0,1,0]])
print(B)
# 字体灰度值为255左右,背景灰度值为0
# 等于255,设置为1;等于0,设置为0。使用matlab,图像显示窗口的数据提示工具提取像素点灰度追
Threshold = 200
A[A<Threshold]=0
A[A>=Threshold]=1

# plt.subplot(131)
# plt.imshow(A)
# plt.set_cmap("binary")
# plt.subplot(132)
# plt.show()
#
center = [np.floor((i)/2).astype(np.int) for i in B.shape]

D = dilation(A)

C = copy.deepcopy(A)
for row in range(center[0],A.shape[0]-center[0]):
    for col in range(center[1], A.shape[1]-center[1]):
        if A[row, col] == B[center[0],center[1]]:

            for i in range(-1*(B.shape[0]//2), (B.shape[0]//2)+1):
                for j in range(-1*(B.shape[1]//2), (B.shape[1]//2)+1):
                    if A[row+i,col+j]==0 and B[center[0]+i,center[1]+j]==1:
                        C[row+i, col+j]=1
plt.subplot(131)
plt.imshow(A)
plt.set_cmap("binary")
plt.title("Input Image")
plt.axis('off')

plt.subplot(132)
plt.imshow(C)
plt.set_cmap("binary")
plt.title("My Dilation")
plt.axis('off')

plt.subplot(133)
plt.imshow(D)
plt.set_cmap("binary")
plt.title("Skimage Dilation")
plt.axis('off')
plt.show()
print("Read Image Success")

运行结果:

参考文献:

【1】数字图像处理MATLAB实现(第2版) 冈萨雷斯等著.

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值