赞
踩
(1)首先求解X方向的导;
(2)再求Y方向的导;
(3)最终结果:|G|=|Gx|+|Gy|;
(1)如果对X方向求梯度,使用如下面这样的卷积核
对图像中的像素值进行卷积(点乘):
(2)如果对Y方向求梯度,使用如下面这样的卷积核
对图像中的像素值进行卷积(点乘):
import os import cv2 import numpy as np def SobelFilter(img_path='images/chess.png'): img_src=cv2.imread(img_path) img=cv2.resize(src=img_src,dsize=(450,450)) img_dy=cv2.Sobel(src=img,ddepth=-1,dx=1,dy=0,ksize=5) img_dx=cv2.Sobel(src=img,ddepth=-1,dx=0,dy=1,ksize=5) img_dx_dy=img_dx+img_dy # img_dx_dy=cv2.add(img_dx,img_dy) cv2.imshow('img_src',img_src) cv2.imshow('img_dx',img_dx) cv2.imshow('img_dy',img_dy) cv2.imshow('img_dx_dy',img_dx_dy) cv2.waitKey(0) cv2.destroyAllWindows() def detectSobelFilter(): cap=cv2.VideoCapture(0) while cap.isOpened(): OK,frame=cap.read() if OK: img = cv2.resize(src=frame, dsize=(450, 450)) img_dy = cv2.Sobel(src=img, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) img_dx = cv2.Sobel(src=img, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) img_dx_dy = img_dx + img_dy cv2.imshow('object',img_dx_dy) else: print('为检测到物体') if cv2.waitKey(1)&0XFF==27: break cap.release() cv2.destroyAllWindows() if __name__ == '__main__': print('Pycharm') SobelFilter() # detectSobelFilter()
对下面这张图分别进行X和Y方向求梯度进行测试:
实时测试:
(1)与索贝尔不同的是kernel的值不同,沙尔只支持kernel(3,3),所以函数中的kernel不需要设置;
(2)沙尔只能求X方向或者Y方向的边缘。
函数:Scharr(src, ddepth, dx, dy, dst=None, scale=None, delta=None, borderType=None)
(1)src:输入的原始图像;
(2)Ddepth:输出图像的位深,如CV_32F,CV_64F;
(3)Dx:对X方向的求导;
(4)Dy:对Y方向的求导;
(5)Dst:输出的图像;
(6)Scale:缩放尺寸,默认值为1;
(7)Delta:
实例代码
实例代码: import os import cv2 import numpy as np def ScharrFilter(img_path='images/chess.png'): img_src=cv2.imread(img_path) img=cv2.resize(src=img_src,dsize=(450,450)) img_dy=cv2.Scharr(src=img,ddepth=-1,dx=1,dy=0) img_dx=cv2.Scharr(src=img,ddepth=-1,dx=0,dy=1) img_dx_dy=img_dx+img_dy # img_dx_dy=cv2.add(img_dx,img_dy) cv2.imshow('img_src',img_src) cv2.imshow('img_dx',img_dx) cv2.imshow('img_dy',img_dy) cv2.imshow('img_dx_dy',img_dx_dy) cv2.waitKey(0) cv2.destroyAllWindows() def detectScharrFilter(): cap=cv2.VideoCapture(0) while cap.isOpened(): OK,frame=cap.read() if OK: img = cv2.resize(src=frame, dsize=(450, 450)) img_dy = cv2.Scharr(src=img, ddepth=cv2.CV_64F, dx=1, dy=0) img_dx = cv2.Scharr(src=img, ddepth=cv2.CV_64F, dx=0, dy=1) img_dx_dy = img_dx + img_dy cv2.imshow('object',img_dx_dy) else: print('为检测到物体') if cv2.waitKey(1)&0XFF==27: break cap.release() cv2.destroyAllWindows() if __name__ == '__main__': print('Pycharm') ScharrFilter() # detectScharrFilter()
(1)可以同时求X,Y方向的边缘;
(2)对噪音敏感,一般需要先进行去噪再调用拉普拉斯算法(中值或者高斯算法去噪);
函数:Laplacian(src, ddepth, dst=None, ksize=None, scale=None, delta=None, borderType=None):
(1)src:输入的原始图像;
(2)Ddepth:输出图像的位深,如CV_32F,CV_64F;
(3)Dx:对X方向的求导;
(4)Dy:对Y方向的求导;
(5)Dst:输出的图像;
(6)Ksize:卷积核的大小;
(7)Scale:缩放尺寸,默认值为1;
实例代码:
import os import cv2 import numpy as np def LaplacianFilter(img_path='images/chess.png'): img_src=cv2.imread(img_path) img=cv2.resize(src=img_src,dsize=(450,450)) img=cv2.Laplacian(src=img,ddepth=-1,ksize=5) cv2.imshow('img_src',img_src) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows() def detectLaplacianFilter(): cap=cv2.VideoCapture(0) while cap.isOpened(): OK,frame=cap.read() if OK: img = cv2.resize(src=frame, dsize=(450, 450)) img=cv2.Laplacian(src=img,ddepth=-1,ksize=5) cv2.imshow('object',img) else: print('为检测到物体') if cv2.waitKey(1)&0XFF==27: break cap.release() cv2.destroyAllWindows() if __name__ == '__main__': print('Pycharm') # LaplacianFilter() detectLaplacianFilter()
关于Canny边缘检测:https://mydreamambitious.blog.csdn.net/article/details/125116318
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。