Python+OpenCV拉普拉斯图像锐化
**Python实现基于OpenCV的拉普拉斯图像锐化**
1
研一学习数字图像处理(刚萨雷斯版),导师让我用Python编写基于拉普拉斯算子的图像锐化,并且是在不直接调用OpenCV的情况下,由于现在还没有学习锐化彩色图像,所以本博客先联系锐化灰度图。
Python代码如下:
import cv2 as cv
import numpy as np
rgb = cv.imread("D:/a.jpg")
weight=rgb.shape[0]
height=rgb.shape[1]
number=rgb.shape[2]
print("原图像大小:""weight: %d height: %d number: %d" %(weight,height,number)) # 检查图像大小
img=cv.resize(rgb,(int(weight/6),int(height/6)),interpolation=cv.INTER_CUBIC) # 将图像缩小为原来的六分之一倍
grayimg=np.zeros((img.shape[0],img.shape[1],1),np.uint8)
weight=int(weight/6)
height=int(height/6)
print("裁剪后图像大小:""weight: %d height: %d number: %d" %(weight,height,number))
for i in range(weight):
for j in range(height):
grayimg[i,j] = 0.299 * img[i, j, 0] + 0.587 * img[i, j, 1] + 0.114 * img[i, j, 2] # 将原图片转为灰度图片
t1 = list([[0,1,0],
[1,-4,1],
[0,1,0]]) # 定义拉普拉斯滤波器
shp=grayimg*1 # 设置一个新的图片变量,防止修改原图片
shp=np.pad(grayimg,((1, 1), (1, 1),(0,0)),"constant",constant_values=0) # 为原图片加一层边缘
for i in range(1,weight-1):
for j in range(1,height-1):
shp[i,j]=abs(np.sum(shp[i:i+3,j:j+3]*t1)) # 对灰度图进行锐化
cv.imshow('srcImage', img)
cv.imshow('grayImage', grayimg)
cv.imshow("Laplacian",grayimg+shp[1:shp.shape[0]-1,1:shp.shape[1]-1])
cv.waitKey(0)
cv.destroyAllWindow()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
写着玩,只想记录自己在Python和图像处理的成长。