23_sobel边缘检测源代码

#sobel边缘检测源代码
import cv2
import numpy as np 
import math
img = cv2.imread('ruonan.jpg',1)
Info = img.shape
height = Info[0]
width = Info[1]
#  算子模板    图像卷积   梯度    阈值
#竖直方向[1  2  1          水平方向[1   0   -1
#        0   0  0                   2   0   -2
#        -1 -2  -1]                 1   0   -1]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = np.zeros((height,width,1),np.uint8)
for i in range(0,height-2):
    for j in range(0,width-2):
        gy = gray[i,j]+2*gray[i,j+1]+gray[i,j+2]-(gray[i+2,j]+gray[i+2,j+1]+gray[i+2,j+2])
        gx = gray[i,j]+2*gray[i+1,j]+gray[i+2,j]-(gray[i,j+2]+gray[i+1,j+2]+gray[i+2,j+2])
        grad = math.sqrt(gx*gx+gy*gy)  #求梯度大小
        if grad>=100:
            dst[i,j] = 255
        else:
            dst[i,j] = 0
cv2.imshow('dst',dst)
cv2.waitKey(0)

上一篇:ITK:Sobel边缘检测图像滤镜


下一篇:图像的边缘检测(canny、sobel、prewitt的比较)