To obtain the contour, an image can be dilated and then corroded, and then subtracted from the dilated result by the corroded result.
1. Sobel
Operator
dst = cv2.Sobel(src, ddepth, dx, dy, ksize)
ddepth
: the depth of the imagedx
The nucleidy
represent the horizontal and vertical directions, respectivelyksize
isSobel
the operator size
image1 = cv2.imread('./ro.png')
plt.imshow(image1);
image1 = cv2.imread('./ro.png')
plt.imshow(image1);
image2 = cv2.Sobel(image1, cv2.CV_64F, 1, 0, ksize=3)
plt.imshow(image2);
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 x
Oriented Gradients
image2 = cv2.Sobel(image1, cv2.CV_64F, 1, 0, ksize=3)
image2 = cv2.convertScaleAbs(image2)
plt.imshow(image2);
Compute x
Oriented Gradients
image3 = cv2.Sobel(image1, cv2.CV_64F, 0, 1, ksize=3)
image3 = cv2.convertScaleAbs(image3)
plt.imshow(image3);
get the outline of the image
image4 = cv2.addWeighted(image2, 0.5, image3, 0.5, 0)
plt.imshow(image4);
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);
2. Scharr
Operator
The usage is the same, but the calculated value will be larger and more sensitive