OpenCV学习——自己动手实现 sobel

前言

今天学习到了sobel,但是理解很模糊,所以就想自己编写实现。通过自己实现sobel算法,可以让我们更深刻的理解sobel的原理,以及轮廓提取是如何实现的。2

先导入图片

import cv2.cv2 as cv2

img = cv2.imread("resource/pie.png", cv2.IMREAD_GRAYSCALE)
cv2.imshow("img", img)

image.png

生成sobel算子

import numpy as np

def createKernel(ksize = 3):
    sobelx = np.zeros((ksize,ksize))
    sobelx[0][0], sobelx[0][ksize-1] = -1, 1
    sobelx[ksize//2][0], sobelx[ksize//2][ksize - 1] = -2, 2
    sobelx[ksize-1][0], sobelx[ksize-1][ksize - 1]= -1, 1

    sobely = np.zeros((ksize,ksize))
    sobely[0][0], sobely[0][ksize-1] = -1, -1
    sobely[0][ksize//2], sobely[ksize - 1][ksize//2] = -2, 2
    sobely[ksize-1][0], sobely[ksize-1][ksize - 1]= 1, 1
    
    return sobelx, sobely

我们输出看看
image.png
可以看到已经生成了! 我们在试试 5x5 的
image.png
好的,没有问题!我们继续。

我们开始吧

通俗易懂

image
image.png

公式

Gx(p5) = p3+2p6+p9-p1-2p4-p7
Gy(p5) = p7+2p8+p9-p1-2p2-p3
|G| = |Gx| + |Gy|

代码

def sobel_1(src, dx, dy, ksize = 5):

    imgSize = src.shape
    #返回结果
    Weighted = np.ones(imgSize)
    Weighted2 = np.ones(imgSize)
    
    #dx = 1 则计算Gx  dy = 1 则计算Gy
    #如果都为0,则返回None
    if dx == 0 and dy == 0:
        return None
    #用来计算每个核的6个点
    step = ksize//2
    
    #遍历像素点,这个就不说啦
    for row, row_dat in enumerate(src, step):
        if row >= imgSize[0] - step:
            break
        for col, col_dat in enumerate(row_dat, step):
            if col >= imgSize[1] - step:
                break
            #计算Gx  为了减少计算量,我们只计算6个点
            if dx == 1:
                #根据上面的公式计算
                sobelx = src[row - step][col - step] * -1 + src[row - step][col + step] + \
                            src[row][col - step] * -2 + src[row][col + step] * 2 + \
                            src[row + step][col - step] * -1 + src[row + step][col + step]
                #保存当前像素点的值
                Weighted[row][col] = sobelx
            
                if dy == 1:
                sobely = src[row - step][col - step] * -1 + src[row - step][col] * -2 + \
                            src[row - step][col + step] * -1 + src[row + step][col - step] + \
                            src[row + step][col] * 2 + src[row + step][col + step]
                Weighted2[row][col] = sobely
    # 同时计算时,返回的结果取绝对值~~~~
    if dx == dy:
        #我们对返回结果进行处理~~~~
        sobelx = cv2.convertScaleAbs(Weighted)
        sobely = cv2.convertScaleAbs(Weighted2)
        return cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
    #下面的返回结果不需要处理
    if dy == 1:
        return Weighted2
    return Weighted
我们利用 ksize//2 快速的对需要计算的几个点进行取值计算

image.png
还是这个图,我们的点是P5 需要计算的点是P1 P3 P4 P6 P7 P9
P1.X = P5.X - ksize//2
P1.Y = P5.Y - ksize//2

P3.X = P5.X - ksize//2
P3.Y = P5.Y + ksize//2

其他点类似
这样做是为了减少计算量,不用对整个核进行计算,提高程序运行的效率

看效果

img_gx = sobel_1(img,1,0)
img_gy = sobel_1(img,0,1)
img_gxy = sobel_1(img,1,1)
img1 = cv2.convertScaleAbs(img_gx)
img2 = cv2.convertScaleAbs(img_gy)

#把图像进行融合 X1α + X2β + b(亮度)
img3 = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)

#把图像拼接起来
res = np.hstack([img, img1, img2])
res2 = np.hstack([img3, img_gxy])

cv2.imshow("res",res)
cv2.imshow("res2",res2)

image.png

评论

暂无评论