python+opencv图像处理(四十二)
Kirsch算子
1、Kirsch算子
2、Kirsch算子具体实现
import cv2
import numpy as np
from matplotlib import pyplot as plt
#定义Kirsch 卷积模板
m1 = np.array([[5, 5, 5],[-3,0,-3],[-3,-3,-3]])
m2 = np.array([[-3, 5,5],[-3,0,5],[-3,-3,-3]])
m3 = np.array([[-3,-3,5],[-3,0,5],[-3,-3,5]])
m4 = np.array([[-3,-3,-3],[-3,0,5],[-3,5,5]])
m5 = np.array([[-3, -3, -3],[-3,0,-3],[5,5,5]])
m6 = np.array([[-3, -3, -3],[5,0,-3],[5,5,-3]])
m7 = np.array([[5, -3, -3],[5,0,-3],[5,-3,-3]])
m8 = np.array([[5, 5, -3],[5,0,-3],[-3,-3,-3]])
img = cv2.imread("E:\image\lena.jpg",0)
img = cv2.copyMakeBorder(img,1,1,1,1,borderType=cv2.BORDER_REPLICATE)
temp = list(range(8))
img1 = np.zeros(img.shape)
for i in range(1,img.shape[0]-1):
for j in range(1,img.shape[1]-1):
temp[0] = np.abs( ( np.dot( np.array([1,1,1]) , ( m1*img[i-1:i+2,j-1:j+2]) ) ).dot(np.array([[1],[1],[1]])))
temp[2] = np.abs( ( np.dot( np.array([1,1,1]) , ( m1*img[i-1:i+2,j-1:j+2]) ) ).dot(np.array([[1],[1],[1]])))
temp[3] = np.abs((np.dot(np.array([1, 1, 1]), (m3 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1],[1],[1]])))
temp[4] = np.abs((np.dot(np.array([1, 1, 1]), (m4 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1],[1],[1]])))
temp[5] = np.abs((np.dot(np.array([1, 1, 1]), (m5 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1],[1],[1]])))
temp[6] = np.abs((np.dot(np.array([1, 1, 1]), (m6 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1],[1],[1]])))
temp[7] = np.abs((np.dot(np.array([1, 1, 1]), (m7 * img[i - 1:i + 2, j - 1:j + 2]))).dot(np.array([[1],[1],[1]])))
temp[1] = np.abs((np.dot(np.array([1, 1, 1]), (m2 * img[i-1:i+2, j-1:j+2]))).dot(np.array([[1],[1],[1]])))
img1[i,j] = np.max(temp)
if img1[i, j] > 255:
img1[i, j] = 255
else:
img1[i, j] = 0
cv2.imshow("origin",img)
cv2.imshow("1",img1)
cv2.waitKey(0)
本篇参考自链接:
https://blog.csdn.net/weixin_44403952/article/details/90267123