示例图像
将原图像转换为灰度图后进行测试
Robert算子
[−1001] \left[ \begin{matrix} -1 & 0\\ 0 & 1 \end{matrix} \right] [−1001]
[0−110] \left[ \begin{matrix} 0 & -1\\ 1 & 0 \end{matrix} \right] [01−10]
robert算子由如图所示两个算子相加而成,以下为实现过程
def Robert(img):
operator_first = np.array([[-1,0],[0,1]])
operator_second = np.array([[0,-1],[1,0]])
"""
copyMakeBorder 边缘扩充
* BORDER_REPLICATE 复制最边缘像素
* BORDER_REFLECT
* BORDER_REFLECT_101 以最边缘像素为轴,对称
* BORDER_WRAP
* BORDER_CONSTANT 以一个常量像素值填充扩充的边界值
* BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh
* BORDER_REFLECT: fedcba|abcdefgh|hgfedcb
* BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba
* BORDER_WRAP: cdefgh|abcdefgh|abcdefg
* BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified 'i'
"""
img2 = cv2.copyMakeBorder(img,1,1,1,1,cv2.BORDER_DEFAULT)
img3 = img2.copy()
for i in range(1,img2.shape[0]):
for j in range(1,img2.shape[1]):
kernel = img2[i-1:i+2,j-1:j+2]
img3[i,j] = np.abs(np.sum(kernel[1:,1:]*operator_first))+np.abs(np.sum(kernel[1:,1:]*operator_second))
cv2.imshow("img3",img3)
Sobel算子
颜色变化一阶导数
(垂直梯度)[−1−2−1000121] \left [ \begin{matrix} -1 & -2 & -1\\ 0 & 0 & 0\\ 1 & 2 &1 \end{matrix} \right] \tag{垂直梯度} ⎣⎡−101−202−101⎦⎤(垂直梯度