opencv study notes five - gradient calculation / edge detection

To obtain the contour, an image can be dilated and then corroded, and then subtracted from the dilated result by the corroded result.

1. SobelOperator

Please add a picture description
dst = cv2.Sobel(src, ddepth, dx, dy, ksize)

  • ddepth: the depth of the image
  • dxThe nuclei dyrepresent the horizontal and vertical directions, respectively
  • ksizeis Sobelthe operator size
image1 = cv2.imread('./ro.png')
plt.imshow(image1);

Please add a picture description

image1 = cv2.imread('./ro.png')
plt.imshow(image1);
image2 = cv2.Sobel(image1, cv2.CV_64F, 1, 0, ksize=3)
plt.imshow(image2);

Please add a picture description

The convolution calculation from white to black is a positive number, and the convolution calculation from black to white is a negative value. All negative numbers will be truncated to 0, so take the absolute value

Compute xOriented Gradients

image2 = cv2.Sobel(image1, cv2.CV_64F, 1, 0, ksize=3)
image2 = cv2.convertScaleAbs(image2)
plt.imshow(image2);

Please add a picture description

Compute xOriented Gradients

image3 = cv2.Sobel(image1, cv2.CV_64F, 0, 1, ksize=3)
image3 = cv2.convertScaleAbs(image3)
plt.imshow(image3);

Please add a picture description

get the outline of the image

image4 = cv2.addWeighted(image2, 0.5, image3, 0.5, 0)
plt.imshow(image4);

Please add a picture description
try another picture

import cv2
import matplotlib.pyplot as plt
image = cv2.imread('image.png')
image2 = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
image2 = cv2.convertScaleAbs(image2)
image3 = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
image3 = cv2.convertScaleAbs(image3)
image4 = cv2.addWeighted(image2, 0.5, image3, 0.5, 0)
image4 = cv2.cvtColor(image4, cv2.COLOR_BGR2RGB)
plt.imshow(image4);

Please add a picture description

2. ScharrOperator

Please add a picture description
The usage is the same, but the calculated value will be larger and more sensitive

3. laplacianOperator

Please add a picture description

Guess you like

Origin blog.csdn.net/weixin_44669966/article/details/125644158