04 Sobel operator


foreword

Record the learning process of the Sobel operator. This algorithm is relatively easy compared to other boundary detection algorithms. But the robustness to noise is lower than other boundary detection techniques. Such as canny.

The Sauber operator is a discrete difference operator that is mostly used for edge detection. The Sober operator has two sets of different convolution factors, which can perform horizontal and vertical plane convolutions for a picture. In detail, it is assumed that the Sober operator is to be used on the pixel (x, y) in a picture. First, the point is convolved with two convolution factors, so that the approximate brightness difference of the point in these two directions can be obtained. After combining the brightness approximations in the xy two directions, the brightness of the point can be obtained. What is obtained is a number. If this value is greater than the threshold, then this point can be considered as an edge point.


1. Operator

The specific operator template is shown in the figure below.
insert image description here

2. Programming implementation

1. Convolution function

The code is as follows (example):


import numpy as np

def my_filter2D(img,kernel):
    """
       卷积函数
       :param image: 原图像,是灰度图
       :param kernel: 卷积核
       :return: 卷积后的图像
    """
    k_rows,k_cols = kernel.shape[:2]
    i_rows,i_cols = img.shape[:2]
    new_img = np.zeros((img.shape[0],img.shape[1]),dtype=np.float32)
    #进行遍历操作
    for row in range(i_rows-k_rows+1):
        for col in range(i_cols-k_cols+1):
            #创建一个视图window,大小与kernel一致
            windows=img[row:row+k_rows,col:col+k_cols]
            #完成卷积操作
            out=abs(np.sum(windows * np.flip(kernel), axis=(0, 1)))
            #赋值 3*3的kernel是row+1,但是2*2 此时的位置是左上,那就等于row,col,将下面修改一下。
           #new_img[row+1,col+1] = out
            new_img[row+k_rows//2,col+k_cols//2] = out
            new_img = np.clip(new_img, 0, 255).astype(np.uint8)
    return new_img

2. sobel operator function

The code is as follows (example):

import numpy as np
import img_filter2D #将前面的卷积函数写成一个模块并引入

def my_roberts(img):
    h,w = img.shape[:2]
    G = np.zeros((img.shape[0],img.shape[1]),dtype=np.float32)
    dx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
    dy = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
    Gx = abs(img_filter2D.my_filter2D(img,dx))
    Gy = abs(img_filter2D.my_filter2D(img,dy))
    G = Gx+Gy
    new_img = np.clip(G, 0, 255).astype(np.uint8)
    Gx = np.clip(Gx, 0, 255).astype(np.uint8)
    Gy = np.clip(Gy, 0, 255).astype(np.uint8)
    return Gx,Gy,new_img

3 to test

import cv2
import matplotlib.pyplot as plt
import Sobel

img = cv2.imread("lena.bmp",0)
i,j,k = Sobel.my_roberts(img)

figure_ = plt.figure()
ax1 = figure_.add_subplot(141)
ax1.set_title('original img')
plt.imshow(img,cmap='gray')#不要忘记这一步
ax2= figure_.add_subplot(142)
ax2.set_title('x_convolution')
plt.imshow(i,cmap='gray')
ax3 = figure_.add_subplot(143)
ax3.set_title('y_convolution')
plt.imshow(j,cmap='gray')

ax4 = figure_.add_subplot(144)
ax4.set_title('magnitude')
plt.imshow(k,cmap='gray')
plt.show()


The result is shown in the figure:
insert image description here
Q: Have you noticed that the first picture is green?
A: Because I forgot to write cmap='gray' in the plt.imshow(img,cmap='gray') code at the beginning, it may default to color mapping, and the effect should be fine after changing it.

Guess you like

Origin blog.csdn.net/CSDN_Yangk/article/details/129838349