opencv sobel robert prewitt canny算子
程序员文章站
2022-07-14 11:02:11
...
原理详解请查看另一篇文章
Canny
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('/Users/。。。/Desktop/test.jpg',0)
edges = cv2.Canny(img,100,200)
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('canny Image'), plt.xticks([]), plt.yticks([])
plt.show()
Prewitt
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('/Users/panhaokang/Desktop/test.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gaussian = cv2.GaussianBlur(gray,(3,3),0)
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
img_prewittx = cv2.filter2D(img_gaussian, -1, kernelx)
img_prewitty = cv2.filter2D(img_gaussian, -1, kernely)
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(img_prewittx + img_prewitty,cmap = 'gray')
plt.title('prewitt'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(img_prewittx,cmap = 'gray')
plt.title('prewitt X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(img_prewitty,cmap = 'gray')
plt.title('prewitt Y'), plt.xticks([]), plt.yticks([])
plt.show()
sobel
import cv2
import numpy as np
from matplotlib import pyplot as plt
# loading image
#img0 = cv2.imread('SanFrancisco.jpg',)
img0 = cv2.imread('/Users/panhaokang/Desktop/test.jpg',)
# converting to gray scale
gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)
# remove noise
img = cv2.GaussianBlur(gray,(3,3),0)
# convolute with proper kernels
laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # x
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5) # y
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.show()
Robert
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('/Users/panhaokang/Desktop/test.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gaussian = cv2.GaussianBlur(gray,(3,3),0)
kernelx = np.array([[-1,0],[0,1]])
kernely = np.array([[0,-1],[1,0]])
img_robertx = cv2.filter2D(img_gaussian, -1, kernelx)
img_roberty = cv2.filter2D(img_gaussian, -1, kernely)
plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,2),plt.imshow(img_robertx + img_roberty,cmap = 'gray')
plt.title('robert'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,3),plt.imshow(img_robertx,cmap = 'gray')
plt.title('robert X'), plt.xticks([]), plt.yticks([])
plt.subplot(2,2,4),plt.imshow(img_roberty,cmap = 'gray')
plt.title('robert Y'), plt.xticks([]), plt.yticks([])
plt.show()
推荐阅读
-
OpenCV图像处理教程C++(十五)边缘检测算法--sobel算子、拉普拉斯算子、Canny算子
-
【OpenCV】边缘检测:Sobel、拉普拉斯算子
-
python—opencv图像膨胀|图像腐蚀|图像边缘检测sobel算子/拉普拉斯算子/canny算子
-
Opencv图像处理——边缘处理Sobel、拉普拉斯算子
-
opencv使用sobel算子提取图像的边缘信息
-
MATLAB-梯度Roberts算子、拉普拉斯算子、Sobel算子、Prewitt算子对图像进行锐化
-
边缘检测之Sobel算子&&Prewitt算子
-
opencv sobel robert prewitt canny算子
-
sobel、prewitt、Isotropic算子处理加噪图片
-
Python实现卷积计算,平滑过滤,高斯过滤,Sobel算子,Prewitt算子