Previous portal:

“Python Image Processing OpenCV (1) : Getting Started”

“Python Image Processing OpenCV (2) : Pixel Processing with Numpy manipulation and Matplotlib Display images”

“Python Image Processing OpenCV (3) : Image Properties, ROI Regions of Interest and Channel Processing”

“Python Image Processing OpenCV (4) : Image Arithmetic and Color Space Modification”

“Python Image Processing OpenCV (5) : Geometric Transformations of Images”

“Python Image Processing OpenCV (6) : Image Threshold Processing”

“Python Image Processing OpenCV (7) : Image Smoothing (Filtering) Processing”

“Python Image Processing OpenCV (8) : Image Decay and Image Bloat”

“Python Image Processing OpenCV (9) : Image processing Morphology open, Closed, and gradient operations”

“Python Image Processing OpenCV (10) : Top hat and Black Hat Operations for Image Processing Morphology”

“Python Image Processing OpenCV (11) : Canny Operator Edge Detection

“Python Image Processing OpenCV (12) : Edge Detection Techniques for Roberts, Prewitt, Sobel, and Laplacian Operators”

Scharr operator

Before talking about Scharr operator, it is necessary to mention the Sobel operator we introduced before. Although Sobel operator can effectively extract image edges, it has poor extraction effect on weak edges in images.

This is because when Sobel operators calculate relatively small nuclei, their approximate calculation accuracy of derivatives is relatively low. For example, when a 3 * 3 Sobel operator approaches the horizontal or vertical direction, its inaccuracy is very obvious.

So we introduce the Scharr operator. Scharr operator is the enhancement of the difference of Sobel operator, and they are the same in the principle and use of image edge detection.

The main idea of Scharr operator is to enlarge the weight coefficient in the template to increase the difference between pixel values.

Scharr operator, also known as Scharr filter, is also used to calculate the difference of images in the x or Y direction. In OpenCV, it mainly exists with the operation of Sobel operator. The filtering coefficients of its filter are as follows:



The method prototype of Scharr operator in OpenCV is as follows:

def Scharr(src, ddepth, dx, dy, dst=None, scale=None, delta=None, borderType=None):
Copy the code
  • SRC: indicates the input image
  • Ddepth: indicates the depth required by the target image. For different input images, the output target image has different depth
  • Dx: indicates the difference order in the x direction. The value is 1 or 0
  • Dy: indicates the difference order in the y direction. The value is 1 or 0

As you can see, the Scharr() and Sobel() functions are very similar and exactly the same in use. Here is an example:

import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread("maliao.jpg")
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# Scharr operator
x = cv.Scharr(gray_img, cv.CV_16S, 1.0) # X direction
y = cv.Scharr(gray_img, cv.CV_16S, 0.1) # Y direction
absX = cv.convertScaleAbs(x)
absY = cv.convertScaleAbs(y)
Scharr = cv.addWeighted(absX, 0.5, absY, 0.5.0)

# Display graphics
plt.rcParams['font.sans-serif'] = ['SimHei']

titles = ['Original image'.'Scharr operator']
images = [rgb_img, Scharr]

for i in range(2):
    plt.subplot(1.2, i + 1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()
Copy the code

LOG operator

The LOG (Laplacian of Gaussian) edge detection operator was jointly proposed by David Courtnay Marr and Ellen Hildreth in 1980, also known as Marr & Hildreth operator. According to the signal to noise ratio of image, it can find the optimal filter for edge detection. Gaussian filtering is first applied to the image, and then its Laplacian second derivative is obtained. The image boundary is detected by crossing Zero crossings of the second derivative, that is, the edge of the image or object is obtained by detecting Zero crossings of filtered results.

LOG operator is actually a combination of Gauss filter and Laplacian filter, which smooths out noise first and then carries out edge detection.

The LOG operator is similar to the mathematical model in visual physiology, so it is widely used in image processing.

It has the advantages of strong anti-interference ability, high precision of boundary location, good edge continuity and effective extraction of weak contrast boundary.

A common LOG operator is a 5 * 5 template;


The distance from the center of the LOG operator to the position weighting coefficient curves like the Mexican straw hat section, so the LOG operator is also called the Mexican straw hat filter.

Example code is as follows:

import cv2 as cv
import matplotlib.pyplot as plt

# fetch image
img = cv.imread("maliao.jpg")
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# Noise reduction through Gaussian filtering first
gaussian = cv.GaussianBlur(gray_img, (3.3), 0)

# Through the Laplace operator to do edge detection
dst = cv.Laplacian(gaussian, cv.CV_16S, ksize=3)
LOG = cv.convertScaleAbs(dst)

# used to display Chinese labels normally
plt.rcParams['font.sans-serif'] = ['SimHei']

# Display graphics
titles = ['Original image'.'the LOG operator']
images = [rgb_img, LOG]

for i in range(2):
    plt.subplot(1.2, i + 1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()
Copy the code

summary

Edge detection algorithm is mainly based on the first and second derivatives of image intensity, but the derivatives are usually sensitive to noise, so it is necessary to filter noise, and call image enhancement or thresholding algorithm for processing, and then carry out edge detection.

reference

Blog.csdn.net/Eastmount/a…

Blog.csdn.net/qq_42722197…

www.jianshu.com/p/2ac784fd2…