梯度

xy轴梯度(sobel算子):

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
kerne = np.ones((10, 10), np.uint8)
while True:
    ret, frame = cap.read()
    sobel = cv.Sobel(frame, cv.CV_64F, 0, 1, 3)  # x,y 方向都有
    sobel_abs = cv.convertScaleAbs(sobel)  # 取绝对值操作
    add = np.hstack((frame, sobel_abs))
    cv.imshow('add', add)
    if cv.waitKey(1) == 27:
        break

sobel算子

 [ -1, 0, 1

   -2, 0, 2

   -1, 0, 1 ]

xy权重相加:

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
kerne = np.ones((10, 10), np.uint8)
while True:
ret, frame = cap.read()
sobelx = cv.Sobel(frame, cv.CV_64F, 0, 1, 3)
sobelx = cv.convertScaleAbs(sobelx)
sobely = cv.Sobel(frame, cv.CV_64F, 1, 0, 3)
sobely = cv.convertScaleAbs(sobely)
sobelxy = cv.addWeighted(sobelx, 0.5, sobely, 0.5, 0) # 取绝对值操作
add = np.hstack((frame, sobelxy))
cv.imshow('add', add)
if cv.waitKey(1) == 27:
break

 scharr算子和laplacian算子:

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
kerne = np.ones((10, 10), np.uint8)
while True:
    ret, frame = cap.read()
    sobelx = cv.Sobel(frame, cv.CV_64F, 0, 1, 3)
    sobelx = cv.convertScaleAbs(sobelx)
    sobely = cv.Sobel(frame, cv.CV_64F, 1, 0, 3)
    sobely = cv.convertScaleAbs(sobely)
    sobelxy = cv.addWeighted(sobelx, 0.5, sobely, 0.5, 0)  # 取绝对值操作

    scharrx = cv.Scharr(frame, cv.CV_64F, 0, 1, 3)
    scharrx = cv.convertScaleAbs(scharrx)
    scharry = cv.Scharr(frame, cv.CV_64F, 1, 0, 3)
    scharry = cv.convertScaleAbs(scharry)
    scharrxy = cv.addWeighted(sobelx, 0.5, sobely, 0.5, 0)  # 取绝对值操作

    laplacian = cv.Laplacian(frame, cv.CV_64F)
    laplacian = cv.convertScaleAbs(laplacian)
    add = np.hstack((frame, sobelxy, scharrxy, laplacian))
    cv.imshow('add', add)
    if cv.waitKey(1) == 27:
        break

 scharr算子

 [ -3,   0,  3               

   -10, 0,  10

   -3,   0,   3 ]   

laplacian算子

 [ 0,   1,  0               

   1,  -4,  1

   0,   1,   0 ]   

Canny边缘检测:

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
kerne = np.ones((10, 10), np.uint8)
while True:
    ret, frame = cap.read()
    canny = cv.Canny(frame, 80, 100)

    cv.imshow('add', canny)
    if cv.waitKey(1) == 27:
        break
原文地址:https://www.cnblogs.com/CaiFengYuanXing/p/13781413.html