016 OpenCV Laplacian edge detection

Table of contents

1. Environment

2. Laplace’s principle

3. Complete code


1. Environment

The usage environment of this article is:

  • Windows10
  • Python 3.9.17
  • opencv-python 4.8.0.74

2. Laplace’s principle

The Laplacian operator is an edge detection technique commonly used in image processing, which helps identify edge and texture features in images. In principle, the Laplacian operator uses second-order differences to calculate edges. In a first-order differential diagram, the maximum or minimum value is considered an edge; in a second-order differential diagram, the zero-passing point between the maximum value and the minimum value is also considered an edge.

Specifically, the first-order difference of the Laplacian operator is defined as f ' (x) = f (x) - f (x - 1), and the second-order difference is defined as f ' (x) = (f (x + 1) - f (x)) - (f (x) - f (x - 1)). After simplification, we get f ' (x) = f (x - 1) - 2 f (x)) + f (x + 1). In the two-dimensional case, the Laplacian operator is defined as f ' (x, y) = -4 f (x, y) + f (x-1, y) + f (x+1, y) + f (x, y-1) + f (x, y+1).

Therefore, the Laplacian operator has the characteristics of isotropy in all directions and can extract edges in any direction. It has the advantage of being non-directional. Using the Laplacian operator to extract edges does not require separate detection of edges in the X direction and edges in the Y direction. , only one edge detection is required.

cv2.Laplacian()It is a function in the OpenCV library for Laplacian edge detection of images.

The basic syntax of this function is as follows:

cv2.Laplacian(src, ddepth, ksize[, dst[, scale[, delta[, borderType]]]])

Parameter Description:

  • src: Input image, usually grayscale.
  • ddepth: The depth of the output image, optional parameter, the default value is -1, which means the same as the input image.
  • ksize: The kernel size used to calculate the Laplacian operator, must be an odd number.
  • dst: Output image, optional parameter, default value is None.
  • scale: Optional parameter, default value is 1.
  • delta: Optional parameter, default value is 0.
  • borderType: Optional parameter, the default value is cv2.BORDER_DEFAULT.

return value:

  • dst: Output image after Laplacian edge detection.

3. Complete code

"""
使用拉普拉斯检测图像边缘
"""
import sys
import cv2 as cv

def main(argv):
    # 边缘输出数据类型,后续转uint8
    ddepth = cv.CV_16S
    kernel_size = 3
    window_name = "Laplace Demo"
    # 读个图片
    imageName = argv[0] if len(argv) > 0 else 'data/lena.jpg'
    src = cv.imread(cv.samples.findFile(imageName), cv.IMREAD_COLOR) 
    if src is None:
        print ('Error opening image')
        print ('Program Arguments: [image_name -- default lena.jpg]')
        return -1
    # 高斯降噪
    src = cv.GaussianBlur(src, (3, 3), 0)
    # 彩色图转灰度图
    src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
    # 拉普拉斯检测边缘
    dst = cv.Laplacian(src_gray, ddepth, ksize=kernel_size)
    # 边缘数据类型转为uint8
    abs_dst = cv.convertScaleAbs(dst)
    cv.imshow(window_name, abs_dst)
    cv.waitKey(0)
    return 0

if __name__ == "__main__":
    main(sys.argv[1:])

Guess you like

Origin blog.csdn.net/m0_72734364/article/details/134846657