【课堂练习】空域图像增强技术

2D 卷积

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/rice.png")
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
cv2.imshow('ori',img)
cv2.imshow('avr',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

实现效果

【课堂练习】空域图像增强技术_第1张图片

图像模糊(图像平滑)

平均

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/rice.png")
blur = cv2.blur(img,(3,3))
cv2.imshow('ori',img)
cv2.imshow('avr',blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

实现效果

【课堂练习】空域图像增强技术_第2张图片
实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/rice.png")
blur = cv2.blur(img,(3,3))
cv2.imshow('ori',img)
cv2.imshow('avr',blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

实现效果

【课堂练习】空域图像增强技术_第3张图片

高斯滤波

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/noise.png")
blur = cv2.GaussianBlur(img,(9,9),0)
cv2.imshow('ori',img)
cv2.imshow('avr',blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

实现效果

【课堂练习】空域图像增强技术_第4张图片

getGaussianKernel 函数

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/noise.png")
GaussianKernel = cv2.getGaussianKernel(10, 3)
print(GaussianKernel)
cv2.imshow('ori',img)
dst = cv2.filter2D(img,-1,GaussianKernel)
cv2.imshow('avr',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

二维高斯核(二维高斯矩阵)

【课堂练习】空域图像增强技术_第5张图片

实现效果

【课堂练习】空域图像增强技术_第6张图片

中值滤波

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/noise.png")
blur = cv2.medianBlur(img,5)
cv2.imshow('ori',img)
cv2.imshow('avr',blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

实现效果
【课堂练习】空域图像增强技术_第7张图片

图像梯度

Roberts 算子利用 filter2D 函数实现

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/noise.png")
kernelx = np.array([[-1,0],[0,1]], dtype=int)
kernely = np.array([[0,-1],[1,0]], dtype=int)
img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
x = cv2.filter2D(img_gray, cv2.CV_16S, kernelx)
y = cv2.filter2D(img_gray, cv2.CV_16S, kernely)
#转 uint8 
absX = cv2.convertScaleAbs(x) 
absY = cv2.convertScaleAbs(y) 
Roberts = cv2.addWeighted(absX,0.5,absY,0.5,0)

#用来正常显示中文标签
plt.rcParams['font.sans-serif']=['SimHei']
#显示图形
titles = [u'原始图像', u'Roberts 算子'] 
images = [img_rgb, Roberts] 
for i in range(2): 
 plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray') 
 plt.title(titles[i]) 
 plt.xticks([]),plt.yticks([]) 
plt.show()

实现效果

【课堂练习】空域图像增强技术_第8张图片
【课堂练习】空域图像增强技术_第9张图片

Roberts 算子利用 Sobel 函数实现

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/rice.png")
x = cv2.Sobel(img, cv2.CV_16S, 1, 0)
y = cv2.Sobel(img, cv2.CV_16S, 0, 1)
Scale_absX = cv2.convertScaleAbs(x) # convert 转换 scale 缩放
Scale_absY = cv2.convertScaleAbs(y)
result = cv2.addWeighted(Scale_absX, 0.5, Scale_absY, 0.5, 0)
cv2.imshow('img', img)
cv2.imshow('Scale_absX', Scale_absX)
cv2.imshow('Scale_absY', Scale_absY)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

实现效果

Prewitt 算子利用 filter2D 函数实现

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/noise.png")
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]],dtype=int)
kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]],dtype=int)
img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
x = cv2.filter2D(img_gray, cv2.CV_16S, kernelx)
y = cv2.filter2D(img_gray, cv2.CV_16S, kernely)
#转 uint8
absX = cv2.convertScaleAbs(x) 
absY = cv2.convertScaleAbs(y) 
Prewitt = cv2.addWeighted(absX,0.5,absY,0.5,0)
#用来正常显示中文标签
plt.rcParams['font.sans-serif']=['SimHei']
#显示图形
titles = [u'原始图像', u'Prewitt 算子'] 
images = [img_rgb, Prewitt] 
for i in range(2): 
	plt.subplot(1,2,i+1)
	plt.imshow(images[i], 'gray') 
	plt.title(titles[i]) 
	plt.xticks([]),plt.yticks([]) 
plt.show()

实现效果

【课堂练习】空域图像增强技术_第10张图片

Sobel 算子

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/noise.png")
kernelx = np.array([[1,2,1],[0,0,0],[-1,-2,-1]],dtype=int)
kernely = np.array([[-1,0,1],[-2,0,2],[-1,0,1]],dtype=int)
img_rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
x = cv2.filter2D(img_gray, cv2.CV_16S, kernelx)
y = cv2.filter2D(img_gray, cv2.CV_16S, kernely)
#转 uint8
absX = cv2.convertScaleAbs(x) 
absY = cv2.convertScaleAbs(y) 
Sobel = cv2.addWeighted(absX,0.5,absY,0.5,0)
#用来正常显示中文标签
plt.rcParams['font.sans-serif']=['SimHei']
#显示图形
titles = [u'原始图像', u'Sobel 算子'] 
images = [img_rgb, Sobel] 
for i in range(2): 
	plt.subplot(1,2,i+1)
	plt.imshow(images[i], 'gray') 
	plt.title(titles[i]) 
	plt.xticks([]),plt.yticks([]) 
plt.show()

实现效果

【课堂练习】空域图像增强技术_第11张图片

Laplacian 算子

实现代码

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/rice.png")
dst = cv2.Laplacian(src=img, ksize=3, ddepth=-1)
cv2.imshow("ori", img)
cv2.imshow("Laplacian k=3", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

实现效果

【课堂练习】空域图像增强技术_第12张图片

总结

实现代码

import cv2 
import numpy as np 
import matplotlib.pyplot as plt
#读取图像
img = cv2.imread("D:/Project/PythonProject/GraphicAnalysis/class3/rice.png")
lenna_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#灰度化处理图像
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#高斯滤波
gaussianBlur = cv2.GaussianBlur(grayImage, (3,3), 0)
#阈值处理
ret, binary = cv2.threshold(gaussianBlur, 127, 255, cv2.THRESH_BINARY)
#Roberts 算子
kernelx = np.array([[-1,0],[0,1]], dtype=int)
kernely = np.array([[0,-1],[1,0]], dtype=int)
x = cv2.filter2D(binary, cv2.CV_16S, kernelx)
y = cv2.filter2D(binary, cv2.CV_16S, kernely)
absX = cv2.convertScaleAbs(x) 
absY = cv2.convertScaleAbs(y) 
Roberts = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
#Prewitt 算子
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]], dtype=int)
kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]], dtype=int)
x = cv2.filter2D(binary, cv2.CV_16S, kernelx)
y = cv2.filter2D(binary, cv2.CV_16S, kernely)
absX = cv2.convertScaleAbs(x) 
absY = cv2.convertScaleAbs(y) 
Prewitt = cv2.addWeighted(absX,0.5,absY,0.5,0)
#Sobel 算子
x = cv2.Sobel(binary, cv2.CV_16S, 1, 0)
y = cv2.Sobel(binary, cv2.CV_16S, 0, 1) 
absX = cv2.convertScaleAbs(x) 
absY = cv2.convertScaleAbs(y) 
Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
#拉普拉斯算法
dst = cv2.Laplacian(binary, cv2.CV_16S, ksize = 3)
Laplacian = cv2.convertScaleAbs(dst) 
#效果图
titles = ['Source Image', 'Binary Image', 'Roberts Image',
 'Prewitt Image','Sobel Image', 'Laplacian Image'] 
images = [lenna_img, binary, Roberts, Prewitt, Sobel, Laplacian] 
for i in np.arange(6): 
 plt.subplot(2,3,i+1),plt.imshow(images[i],'gray') 
 plt.title(titles[i]) 
 plt.xticks([]),plt.yticks([]) 
plt.show()

实现效果

【课堂练习】空域图像增强技术_第13张图片

你可能感兴趣的:(图像处理,opencv,计算机视觉,python)