Python + OpenCV边沿检测(Edge Detection)

本文通过Python和OpenCV实现三种边沿检测算法:Laplacian、Sobel和Canny。对比不同算法的效果,Canny算法表现最佳,适用于需要明确边界的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python + OpenCV边沿检测(Edge Detection)


  • senchenrui@126.com

OpenCV提供了3种边沿检测算法

  • Laplacian
  • sobel
  • canny

本文分别采用这3种算法进行边沿检测,并给出比较结果

1 基于Laplacian的边沿检测

代码如下

    import numpy
    import argparse
    import cv2

    image = cv2.imread('1.jpg')
    cv2.imshow("Original", image)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    cv2.imshow("Gray", gray)

    #if don't use a floating point data type when computing
    #the gradient magnitude image, you will miss edges
    lap = cv2.Laplacian(gray, cv2.CV_64F)
    lap = numpy.uint8(numpy.absolute(lap))


    #display two images in a figure
    cv2.imshow("Edge detection by Laplacaian", numpy.hstack([lap, gray]))

    cv2.imwrite("1_edge_by_laplacian.jpg", numpy.hstack([gray, lap]))


    if(cv2.waitKey(0)==27):
     cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

2 基于Sobel的边沿检测

代码如下

    import numpy
    import argparse
    import cv2

    image = cv2.imread('1.jpg')
    cv2.imshow("Original", image)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    cv2.imshow("Gray", gray)

    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1)

    sobelx = numpy.uint8(numpy.absolute(sobelx))
    sobely = numpy.uint8(numpy.absolute(sobely))
    sobelcombine = cv2.bitwise_or(sobelx,sobely)
    #display two images in a figure
    cv2.imshow("Edge detection by Sobel", numpy.hstack([gray,sobelx,sobely, sobelcombine]))

    cv2.imwrite("1_edge_by_sobel.jpg", numpy.hstack([gray,sobelx,sobely, sobelcombine]))


    if(cv2.waitKey(0)==27):
      cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

3 基于Canny的边沿检测

代码如下

    import numpy
    import argparse
    import cv2

    image = cv2.imread('1.jpg')
    cv2.imshow("Original", image)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    cv2.imshow("Gray", gray)

    #30 and 150 is the threshold, larger than 150 is considered as edge,
    #less than 30 is considered as not edge
    canny = cv2.Canny(gray, 30, 150)

    canny = numpy.uint8(numpy.absolute(canny))
    #display two images in a figure
    cv2.imshow("Edge detection by Canny", numpy.hstack([gray,canny]))

    cv2.imwrite("1_edge_by_canny.jpg", numpy.hstack([gray,canny]))


    if(cv2.waitKey(0)==27):
     cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

以下是3种边沿检测算法的处理结果。 从图中可以看出,Lapacian的检测效果最差,Canny的最明显,因为该算法将边沿检测结果进行了二值化处理。Sobel的检测结果也是较好的,从图中可以看到边沿检测结果是较为清晰的。

  • Lapacian
    这里写图片描述

  • Sobel
    这里写图片描述

  • Canny
    这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值