# -*- coding:utf8 -*-
import cv2
#加载图像 并转换成灰度
input_file = 'image/3.jpg'
img = cv2.imread(input_file)
img_gray = cv2.imread(input_file,cv2.IMREAD_GRAYSCALE)
cv2.imshow('img',img)
cv2.imshow('img_gray',img_gray)
#Sobel滤波器:是一种边检测器,它使用3*3内核来检测水平边和垂直边。
#Sobel水平检测器
sobel_horizontal = cv2.Sobel(img,cv2.CV_64F,1,0,ksize = 5)
cv2.imshow('sobel_horizontal',sobel_horizontal)
#Sobel垂直检测器
sobel_vertical = cv2.Sobel(img,cv2.CV_64F,0,1,ksize = 5)
cv2.imshow('sobel_vertical',sobel_vertical)
#Laplacian edge detector 拉普拉斯边检测器
laplacian = cv2.Laplacian(img,cv2.CV_64F)
cv2.imshow('laplacian',laplacian)
#Canny边检测器:在解决噪声问题优于拉普拉斯边检测器和索贝尔边检测器。
canny = cv2.Canny(img,50,240)
cv2.imshow('canny',canny)
cv2.waitKey()
原图

image.png
灰度图

image.png
Sobel水平检测器

image.png
Sobel垂直检测器

image.png
Laplacian拉普拉斯检测器

image.png
Canny检测器

image.png
网友评论