【DAY_2】opencv做边沿检测

1.Sobel边缘检测

Sobel算子由两种组成:垂直边沿检测和水平边沿检测

import cv2
import numpy as np
img = cv2.imread('try.jpg', cv2.IMREAD_GRAYSCALE)
rows, cols = img.shape
sobel_horizontal = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
cv2.imshow('Original', img)
cv2.imshow('Sobel horizontal', sobel_horizontal)
cv2.imshow('Sobel vertical', sobel_vertical)
cv2.waitKey(0)

2.拉普拉斯算子

the  Sobel filter detects edges in either a horizontal or vertical direction and it doesn’t give us a
holistic view of all the edges. To overcome this, we can use the  Laplacian filter. 

Sobel算子只能在垂直方向或者水平方向进行边沿检测,并不能在整体视野上检测所有边沿。这时,我们可以使用拉普拉斯算子。

import cv2
img=cv2.imread('try.jpg',cv2.IMREAD_COLOR)
img=cv2.Laplacian(img,cv2.CV_64F)
cv2.imshow('laplacian',img)
cv2.waitKey(0)

但是拉普拉斯算子并不能很好应用于所有的image,有些图片会产生很多noise。To overcome this problem, we use the  Canny edge detector. To use the  Canny edge detector

3.canny

import cv2
img=cv2.imread('try.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('',img)
#It takes two numbers
# as arguments to indicate the thresholds. The second argument is called the low threshold
# value, and the third argument is called the high threshold value. If the gradient value is
# above the high threshold value, it is marked as a strong edge. 
img=cv2.Canny(img,20,240)
cv2.imshow('Canny',img)
cv2.waitKey()
cv2.destroyAllWindows()

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值