拉普拉斯变换图像增强-《医学图像处理》小作业二-Python代码/Matlab代码

天津中医药大学-20级医学信息工程   教师:王翌   学生:邓集亲

声明:本文章中所涉及的代码并非我个人独立完成的成果。

        在撰写的过程中,我广泛地吸取了前一辈人,尤其是学长学姐们的宝贵学习经验。通过深入研究他们的学习轨迹,以及查阅和分析了众多相关的理论文献与资料,并在王老师的悉心指导下,经过反复的实验、调试与优化,最终得以总结完成本文所展现的代码。

        建议各位学弟学妹们,先根据王老师的授课内容,独立思考一下应该如何完成。如果实在是有理解上的困难,不知道从何下手,再来学习和参考本文。

        学长我是用Python写的,如果你使用MATLAB,同样可以参考此代码进行翻译。我在代码中加入了许多注释和测试环节,以便于理解。由于学长能力有限,代码中或许存在一些疏漏或错误,请批判性地参考。

实验二:锐化滤波器

作业要求:

  1. 参考“空间域图像增强”课的内容,用拉普拉斯变换对图像进行增强。

例子:

实验图片:

moon.tif,再自选其它至少20张图片(包括灰度图片和彩色图片)。

Laplace算子是用来检测图像边缘的一个算子。通过二阶微分推导出的一种图像邻域增强算法。它的基本思想是当邻域的中心像素灰度低于它所在邻域内的其他像素的平均灰度时,此中心像素的灰度应该进一步降低;当高于时进一步提高中心像素的灰度,从而实现图像锐化处理。

  当邻域内像素灰度相同时,模板的卷积运算结果为0;当中心像素灰度高于邻域内其他像素的平均灰度时,模板的卷积运算结果为正数;当中心像素的灰度低于邻域内其他像素的平均灰度时,模板的卷积的负数。对卷积运算的结果用适当的衰弱因子处理并加在原中心像素上,就可以实现图像的锐化处理。

Windows图片文件目录:

ImageSet文件目录下储存其他待处理图片

Python代码:

#导入库
import numpy as np
import cv2
import matplotlib.pyplot as plt

original = cv2.imread(r"D:\deng\smalljob\ImageSet\moon.tif")
original_gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)   #将图像从RGB颜色空间转换到灰度颜色空间

OriginalPic = np.array(original_gray, dtype='float32')
img = np.zeros((OriginalPic.shape[0]+2, OriginalPic.shape[1]+2), dtype='float32')

#制造遍历图像
for i in range(1, img.shape[0]-1):
    for j in range(1, img.shape[1]-1):
        img[i][j] = OriginalPic[i-1][j-1]

LaplacePic = np.zeros((OriginalPic.shape[0], OriginalPic.shape[1]), dtype='float32')
#中心值为-4

kernel_1 = np.asarray([[0, 1, 0],
                      [1, -4, 1],
                      [0, 1, 0]])
for i in range(0, LaplacePic.shape[0]):
    for j in range(0, LaplacePic.shape[1]):
        LaplacePic[i][j] = abs(np.sum(np.multiply(kernel_1, img[i:i+3, j:j+3])))

cv2.imwrite('D:/deng/LaplacePic_1.png',LaplacePic)
LaplacePic_1=cv2.imread(r'D:/deng/LaplacePic_1.png')

#中心值为-5

kernel_2 = np.asarray([[0, 1, 0],
                      [1, -5, 1],
                      [0, 1, 0]])
for i in range(0, LaplacePic.shape[0]):
    for j in range(0, LaplacePic.shape[1]):
        LaplacePic[i][j] = abs(np.sum(np.multiply(kernel_2, img[i:i+3, j:j+3])))

cv2.imwrite('D:/deng/LaplacePic_2.png',LaplacePic)
LaplacePic_2=cv2.imread(r'D:/deng/LaplacePic_2.png')

#展示结果
plt.subplot(131),plt.imshow(OriginalPic,cmap='gray')
plt.title('OriginalPic')
plt.axis('off')


plt.subplot(132),plt.imshow(LaplacePic_1,cmap='gray')
plt.title('LaplacePic_1:-4')
plt.axis('off')


plt.subplot(133),plt.imshow(LaplacePic_2,cmap='gray')
plt.title('LaplacePic_2:-5')
plt.axis('off')

plt.show()

运行结果:

以上是处理灰度图,以下是处理彩色图像 

#导入库
import numpy as np
import cv2
import matplotlib.pyplot as plt
 
original = cv2.imread(r"D:\deng\smalljob\ImageSet\mian.png")
original_gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)   #将图像从RGB颜色空间转换到灰度颜色空间
 
OriginalPic = np.array(original_gray, dtype='float32')
img = np.zeros((OriginalPic.shape[0]+2, OriginalPic.shape[1]+2), dtype='float32')
 
origin = cv2.cvtColor(original, cv2.COLOR_BGR2RGB) 
b,g,r=cv2.split(origin)#分离颜色通道
OriginPic = np.array(origin, dtype='float32')
img_b = np.zeros((OriginPic.shape[0]+2, OriginPic.shape[1]+2), dtype='float32')
img_g = np.zeros((OriginPic.shape[0]+2, OriginPic.shape[1]+2), dtype='float32')
img_r = np.zeros((OriginPic.shape[0]+2, OriginPic.shape[1]+2), dtype='float32')
 
for i in range(1, img_b.shape[0]-1):
    for j in range(1, img_b.shape[1]-1):
        img_b[i][j] = b[i-1][j-1]
        img_g[i][j] = g[i-1][j-1]
        img_r[i][j] = r[i-1][j-1]
 
LaplacePic_b = np.zeros((OriginPic.shape[0], OriginPic.shape[1]), dtype='float32')
LaplacePic_g = np.zeros((OriginPic.shape[0], OriginPic.shape[1]), dtype='float32')
LaplacePic_r = np.zeros((OriginPic.shape[0], OriginPic.shape[1]), dtype='float32')
 
kernel_3 = np.asarray([[0, 1, 0],
                      [1, -5, 1],
                      [0, 1, 0]])
for i in range(0, LaplacePic_b.shape[0]):
    for j in range(0, LaplacePic_b.shape[1]):
        LaplacePic_b[i][j] = abs(np.sum(np.multiply(kernel_3, img_b[i:i+3, j:j+3])))
        LaplacePic_g[i][j] = abs(np.sum(np.multiply(kernel_3, img_g[i:i+3, j:j+3])))
        LaplacePic_r[i][j] = abs(np.sum(np.multiply(kernel_3, img_r[i:i+3, j:j+3])))
resultimg=np.dstack((LaplacePic_b,LaplacePic_g,LaplacePic_r))
resultimg=np.abs(resultimg).astype(np.float32)/255.0
 
#制造遍历图像
for i in range(1, img.shape[0]-1):
    for j in range(1, img.shape[1]-1):
        img[i][j] = OriginalPic[i-1][j-1]
 
LaplacePic_1 = np.zeros((OriginalPic.shape[0], OriginalPic.shape[1]), dtype='float32')
LaplacePic_2 = np.zeros((OriginalPic.shape[0], OriginalPic.shape[1]), dtype='float32')
 
#中心值为-4
 
kernel_1 = np.asarray([[0, 1, 0],
                      [1, -4, 1],
                      [0, 1, 0]])
for i in range(0, LaplacePic_1.shape[0]):
    for j in range(0, LaplacePic_1.shape[1]):
        LaplacePic_1[i][j] = abs(np.sum(np.multiply(kernel_1, img[i:i+3, j:j+3])))
 
cv2.imwrite('D:/deng/LaplacePic_1.png',LaplacePic_1)
LaplacePic_1=cv2.imread(r'D:/deng/LaplacePic_1.png')
 
#中心值为-5
 
kernel_2 = np.asarray([[0, 1, 0],
                      [1, -5, 1],
                      [0, 1, 0]])
for i in range(0, LaplacePic_2.shape[0]):
    for j in range(0, LaplacePic_2.shape[1]):
        LaplacePic_2[i][j] = abs(np.sum(np.multiply(kernel_2, img[i:i+3, j:j+3])))
 
cv2.imwrite('D:/deng/LaplacePic_2.png',LaplacePic_2)
LaplacePic_2=cv2.imread(r'D:/deng/LaplacePic_2.png')
#展示结果
plt.subplot(131),plt.imshow(origin,cmap='gray')
plt.title('OriginalPic')
plt.axis('off')
 
plt.subplot(132),plt.imshow(LaplacePic_1,cmap='gray')
plt.title('LaplacePic_1:-4')
plt.axis('off')
 
plt.subplot(133),plt.imshow(resultimg,cmap='gray')
plt.title('LaplacePic_2:-5')
plt.axis('off')
plt.show()
 
 

运行结果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值