SoFunction
Updated on 2024-10-30

Python+OpenCV image edge detection four implementation methods

import cv2 as cv
import numpy as np
import  as plt
# Setting up Chinese compatibility
[''] = ['sans-serif']
['-serif'] = ['SimHei']
D:\Anaconda\AZWZ\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:
D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.-win_amd64.dll
D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.-win_amd64.dll
  ("loaded more than 1 DLL from .libs:\n%s" %
horse = ('img/',0)
(horse,cmap=)
(horse,cmap=)

operator (math.)

# 1,0 represents the sobel operator in the x-direction.
x = (horse,cv.CV_16S,1,0)
# 0,1 represents the sobel operator in the y-direction.
y = (horse,cv.CV_16S,0,1)
# Format conversion
absx = (x)
absy = (y)
# Edge detection results
res = (absx,0.5,absy,0.5,0)
(figsize=(20,20))
(1,2,1)
m1 = (horse,cmap=)
("Original image.")
(1,2,2)
m2 = (res,cmap=)
("Sobel operator edge detection.")
Text(0.5, 1.0, 'Sobel operator edge detection')

Calculator (better for details)

# 1,0 represents the sobel operator in the x-direction.
x = (horse,cv.CV_16S,1,0,ksize=-1)
# 0,1 represents the sobel operator in the y-direction.
y = (horse,cv.CV_16S,0,1,ksize=-1)
# Format conversion
absx = (x)
absy = (y)
# Edge detection results
res = (absx,0.5,absy,0.5,0)
(figsize=(20,20))
(1,2,1)
m1 = (horse,cmap=)
("Original image.")
(1,2,2)
m2 = (res,cmap=)
("Schaar operator edge detection.")
Text(0.5, 1.0, 'Schaar operator edge detection')

Operator (based on zero crossing, 0-valued points of second-order derivatives)

res = (horse,cv.CV_16S)
res = (res)
(figsize=(20,20))
(1,2,1)
m1 = (horse,cmap=)
("Original image.")
(1,2,2)
m2 = (res,cmap=)
("Laplacian operator edge detection.")
Text(0.5, 1.0, 'Laplacian operator edge detection')

Edge detection (considered the optimal edge detection algorithm)

res = (horse,0,100)
# res = (res) Canny edge detection is a binary detection and does not require a format conversion step
(figsize=(20,20))
(1,2,1)
m1 = (horse,cmap=)
("Original image.")
(1,2,2)
m2 = (res,cmap=)
("Canny edge detection.")
Text(0.5, 1.0, 'Canny edge detection')

summarize

The above is Python OpenCV image edge detection four implementation methods in detail, more information about Python OpenCV image edge detection please pay attention to my other related articles!