图像锐化和边缘检测算子
1 roberts算子
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图像
img=cv2.imread('4.jpg')
lenna_img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
grayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#Roberts算子
kernelx=np.array([[-1,0],[0,1]], dtype=int)
kernely=np.array([[0,-1],[-1,0]], dtype=int)
x=cv2.filter2D(grayImage,cv2.CV_16S,kernelx)
y=cv2.filter2D(grayImage,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=[lenna_img,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()
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图像
img=cv2.imread('4.jpg')
lenna_img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
grayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#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(grayImage,cv2.CV_16S,kernelx)
y=cv2.filter2D(grayImage,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=[lenna_img,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()
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图像
img=cv2.imread('4.jpg')
lenna_img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
grayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#sobel算子
x=cv2.Sobel(grayImage,cv2.CV_16S,1,0)
y=cv2.Sobel(grayImage,cv2.CV_16S,0,1)
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=[lenna_img,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()
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图像
img=cv2.imread('4.jpg')
lenna_img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
grayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#laplacian算子
dst=cv2.Laplacian(grayImage,cv2.CV_16S,ksize=3)
Laplacian=cv2.convertScaleAbs(dst)
#用来正常显示中文标签
plt.rcParams['font.sans-serif']=['SimHei']
#显示图形
titles=[u'原始图像',u'laplacian算子']
images=[lenna_img,Laplacian]
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()
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图像
img=cv2.imread('4.jpg')
lenna_img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
grayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#scharr算子
x=cv2.Scharr(grayImage,cv2.CV_32F,1,0)
y=cv2.Scharr(grayImage,cv2.CV_32F,0,1)
absX=cv2.convertScaleAbs(x)
absY=cv2.convertScaleAbs(y)
Scharr=cv2.addWeighted(absX,0.5,absY,0.5,0)
#用来正常显示中文标签
plt.rcParams['font.sans-serif']=['SimHei']
#显示图形
titles=[u'原始图像',u'scharr算子']
images=[lenna_img,Scharr]
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()
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图像
img=cv2.imread('4.jpg')
lenna_img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
grayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#高斯滤波降噪
gaussian = cv2.GaussianBlur(grayImage, (3,3), 0)
#Canny算子
Canny = cv2.Canny(gaussian, 50, 150)
#用来正常显示中文标签
plt.rcParams['font.sans-serif']=['SimHei']
#显示图形
titles=[u'原始图像',u'canny算子']
images=[lenna_img,Canny]
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()
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图像
img=cv2.imread('4.jpg')
lenna_img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
#灰度化处理图像
grayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#高斯滤波降噪
gaussian = cv2.GaussianBlur(grayImage, (3,3), 0)
#再通过拉普拉斯算子做边缘检测
dst = cv2.Laplacian(gaussian, cv2.CV_16S, ksize = 3)
LOG = cv2.convertScaleAbs(dst)
#用来正常显示中文标签
plt.rcParams['font.sans-serif']=['SimHei']
#显示图形
titles=[u'原始图像',u'log算子']
images=[lenna_img,LOG]
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()