边缘检测算子Roberts,Sobel,Prewitt,LOG,Canny 代码python
时间: 2024-06-10 14:03:55 浏览: 96
以下是Python中常用的边缘检测算子代码:
1. Roberts算子:
import cv2
img = cv2.imread('image.jpg',0)
kernel = np.array([[1, 0], [0, -1]])
roberts = cv2.filter2D(img, -1, kernel)
cv2.imshow('Roberts', roberts)
cv2.waitKey(0)
cv2.destroyAllWindows()
2. Sobel算子:
import cv2
import numpy as np
img = cv2.imread('image.jpg',0)
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
sobel = np.sqrt(sobelx**2 + sobely**2)
cv2.imshow('Sobel', sobel)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. Prewitt算子:
import cv2
import numpy as np
img = cv2.imread('image.jpg',0)
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)
prewittx = cv2.filter2D(img, -1, kernelx)
prewitty = cv2.filter2D(img, -1, kernely)
prewitt = np.sqrt(prewittx**2 + prewitty**2)
cv2.imshow('Prewitt', prewitt)
cv2.waitKey(0)
cv2.destroyAllWindows()
4. LOG算子:
import cv2
img = cv2.imread('image.jpg', 0)
canny = cv2.Canny(img, 50, 150)
cv2.imshow('Canny', canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
5. Canny算子:
import cv2
img = cv2.imread('image.jpg', 0)
canny = cv2.Canny(img, 50, 150)
cv2.imshow('Canny', canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
阅读全文