opencv学习之边缘检测

**Edge Detection in Image Processing**

Edge Detection is a critical环节 in the image processing workflow. In the realms of computer vision and machine learning, edge detection is particularly effective for feature extraction and feature detection. OpenCV offers a diverse array of edge detection algorithms, and below, I will introduce the API functions for the most commonly used ones.

Results described in this article can be viewed at:Edge Detection

Content Outline

  1. Convert to Grayscale

  2. Adaptive Threshold Processing

  3. Sobel Operator

  4. Scharr Operator

  5. Laplacian Operator

  6. Canny Edge Detection

  7. Sobel Operator Combined with Laplacian Operator

Convert to Grayscale

OpenCV provides a color space conversion function, where cv.COLOR_RGB2GRAY is highly frequent, serving as the foundation for other operations. Color space conversion functions:

cv.cvtColor (src, dst, code, dstCn = 0)

  • src: Original image
  • dst: Output image
  • code: Color space conversion code; for grayscale, use cv.COLOR_RGB2GRAY
  • dstcn: Number of image channels
const src = cv.imread(img); // Read image data
const dst = new cv.Mat(); // Output image
cv.cvtColor(src, dst, cv.COLOR_RGB2GRAY, 0); // Convert to grayscale
cv.imshow(canvas, dst);
src.delete();
dst.delete();

Adaptive Threshold Processing

The adaptive threshold processing method calculates the weighted average of the surrounding area of each pixel to determine the threshold, and then uses this threshold to process the current pixel. The adaptive threshold processing function is:

cv.adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType)

  • maxValue: Maximum value to be processed
  • adaptiveMethod: Adaptive threshold algorithm, options include cv.ADAPTIVE_THRESH_GAUSSIAN_C (Gaussian equation) or cv.ADAPTIVE_THRESH_MEAN_C (weighted average)
  • thresholdType: Threshold processing method, options include cv.THRESH_BINARY (binary) or cv.THRESH_BINARY_INV (binary inversion)
const src = cv.imread(img);
const dst = new cv.Mat();
cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0); // Convert to grayscale
cv.adaptiveThreshold(src, dst, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 5, 7); // Adaptive threshold processing
cv.imshow(canvas, dst); // Display output image

Effect shown:

1.png

Sobel Operator

The Sobel operator combines Gaussian smoothing with differential operations, using local differences to locate edges, resulting in a gradient approximation. The Sobel operator is special in that it separately calculates horizontal and vertical components, requiring the use of cv.addWeighted to proportionally display the x and y directions of the output image. The Sobel operator function is:

cv.Sobel (src, dst, ddepth, dx, dy, ksize = 3, scale = 1, delta = 0, borderType = cv.BORDER_DEFAULT)

  • ddepth: Depth of the output image, options: cv.CV_8U, cv.CV_16U, cv.CV_32F, cv.CV_64F
  • dx: x-direction derivative
  • dy: y-direction derivative
  • ksize: Kernel size, options: 1, 3, 5, 7
  • scale: Scaling factor, default 1
  • delta: Value of the dst image, default 0
  • borderType: Border style, see API documentation
const src = cv.imread(img);
const dstx = new cv.Mat();
const dsty = new cv.Mat();
const dst = new cv.Mat();
cv.cvtColor(src, src, cv.COLOR_RGB2GRAY, 0);
cv.Sobel(src, dstx, cv.CV_8U, 1, 0, 1, 3, 0, cv.BORDER_DEFAULT); // Sobel x-direction
cv.Sobel(src, dsty, cv.CV_8U, 0, 1, 1, 3, 0, cv.BORDER_DEFAULT); // Sobel y-direction
cv.addWeighted(dstx, 0.5, dsty, 0.5, 0, dst); // Weighted proportion for x and y directions
cv.imshow(canvas, dst);

Effect shown:

2.png

Scharr Operator

The Scharr operator is an improved version of the Sobel operator, with higher precision. The calling method is similar to the Sobel operator, but without the ksize parameter. Below, I will directly show the different parts of the calling code:

cv.Scharr(src, dstx, cv.CV_8U, 1, 0, 1, 0, cv.BORDER_DEFAULT); // Scharr x-direction
cv.Scharr(src, dsty, cv.CV_8U, 0, 1, 1, 0, cv.BORDER_DEFAULT); // Scharr y-direction
cv.addWeighted(dstx, 0.5, dsty, 0.5, 0, dst); // Weighted proportion for x and y directions

Effect shown:

3.png

Laplacian Operator

The Laplacian operator is a second-order derivative operator that can sharpen edges in images of different directions. The Laplacian operator performs two horizontal and vertical calculations separately, eliminating the need for separate x and y calculations like the Sobel and Scharr operators. The Laplacian operator function is:

cv.Laplacian (src, dst, ddepth, ksize = 1, scale = 1, delta = 0, borderType = cv.BORDER_DEFAULT)

  • ddepth: Depth of the output image, options: cv.CV_8U, cv.CV_16U, cv.CV_32F, cv.CV_64F
  • ksize: Kernel size, options: 1, 3, 5, 7
  • scale: Scaling factor, default 1
  • delta: Value of the dst image, default 0
  • borderType: Border style, see API documentation
const src = cv.imread(img);
const dst = new cv.Mat();
cv.cvtColor(src, src, cv.COLOR_RGB2GRAY, 0);
cv.Laplacian(src, dst, cv.CV_8U, 1, 1, 0, cv.BORDER_DEFAULT); // Laplacian operator
cv.imshow(canvas, dst);

Effect shown:

4.png

Canny Edge Detection

The Canny edge detection is a multi-level edge detection method. It involves noise removal, gradient calculation, non-maxima suppression, and edge determination. The Canny function is:

cv.Canny(src, dot, threshold1, threshold2, apertureSize = 3, L2gradient = false)

  • threshold1: First threshold, smaller values capture more edge information
  • threshold2: Second threshold, smaller values capture more edge information
  • apertureSize: Aperture size
  • L2gradient: Image gradient magnitude, default False
const src = cv.imread(img);
const dst = new cv.Mat();
cv.cvtColor(src, src, cv.COLOR_RGB2GRAY, 0);
cv.Canny(src, dst, 50, 100, 3, false);
cv.imshow(canvas, dst);

Effect shown:

5.png

Sobel Operator Combined with Laplacian Operator

OpenCV can also combine different algorithms to achieve better results. Here, I demonstrate the combination of the Sobel operator and Laplacian operator

const src = cv.imread(img);
const dstx = new cv.Mat();
const dsty = new cv.Mat();
const dst = new cv.Mat();
const dst2 = new cv.Mat();
cv.Sobel(src, dstx, cv.CV_8U, 1, 0, 1, 3, 0, cv.BORDER_DEFAULT); // Sobel x-direction
cv.Sobel(src, dsty, cv.CV_8U, 0, 1, 1, 3, 0, cv.BORDER_DEFAULT); // Sobel y-direction
cv.addWeighted(dstx, 0.5, dsty, 0.5, 0, dst); // Weighted proportion for x and y directions
cv.Laplacian(src, dst2, cv.CV_8U, 1, 1, 0, cv.BORDER_DEFAULT); // Laplacian operator
const mask = new cv.Mat();
cv.add(dst, dst2, dst2, mask, -1); // Image addition
cv.imshow(canvas, dst2);

Effect shown:

6.png

Summary

Edge detection in OpenCV is not limited to the several methods mentioned above. There is another more powerful way to implement edge detection, namely Fast Fourier Transform (FFT), which can fully realize the algorithms mentioned above but is more complex. We need to understand the differences in performance between each algorithm and determine the most suitable use cases.

本文由作者创作或收集,内容引用如有问题请联系站长处理。
转载请注明原文及出处:opencv学习之边缘检测