SoFunction
Updated on 2024-11-15

Python edge detection prewitt, sobel and laplace operators in detail

Introduction to Filter Operators

The convolution algorithm is provided in ndimage and builds on top of it by providing three filtering schemes for edge detection: prewitt, sobel, and laplace.

existconvolveA filtering operator for edge detection is enumerated in , and after unifying the dimensions, its gradient operators in the x xx and y yy directions are written, respectively, as

this isprewittCalculator.

The Sobel operator adds the weight of the center value to Prewitt, denoted as

Both of these edge detection operators, are applicable to a certain direction. ndimage also provides the lapace operator, which is essentially a second-order differential operator, and its 3×3 convolutional template can be expressed as

concrete realization

ndimageThese three convolutional filtering algorithms for encapsulation are defined as follows

prewitt(input, axis=-1, output=None, mode='reflect', cval=0.0)
sobel(input, axis=-1, output=None, mode='reflect', cval=0.0)
laplace(input, output=None, mode='reflect', cval=0.0)

Among them.modedenotes the compensation scheme for edge effects in the convolution process, let the array to be filtered bea b c dInstead, the edges are filled in different modes as follows

Left Side Fill digital Right Fill
reflect d c b a a b c d d c b a
constant k k k k a b c d k k k k
nearest a a a a a b c d d d d d
mirror d c b a b c d c b a
wrap a b c d a b c d a b c d

beta (software)

Let's test it next.

from  import prewitt, sobel, laplace
from  import ascent
import  as plt
img = ascent()

dct = {
    "origin" : lambda img:img,
    "prewitt" : prewitt,
    "sobel" : sobel,
    "laplace" : lambda img : abs(laplace(img))
}

fig = ()
for i,key in enumerate(dct):
    ax = fig.add_subplot(2,2,i+1)
    (dct[key](img), cmap=)
    (key)

()

For a cleaner look, the code encapsulates the original image, the prewitt filter, the sobel filter, and the laplace filter in a dictionary. Among themorigindenotes the original image and the corresponding function is alambdaExpressions.

During plotting, thecmapmap to, which makes the drawing appear as a grayscale image afterward.

The effect is as follows

This article on Python edge detection prewitt, sobel and laplace operator details of the article is introduced to this, more related Python edge detection content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!