大家好,欢迎来到IT知识分享网。
一、sobel 算子
Sobel算子包含两组 3 ∗ 3的矩阵,分别为横向及纵向模板,将之与图像作平面卷积,即可分别得出横向及纵向的亮度差分近似值。下面是 G x G_x Gx 和 G y G_y Gy的模板。
G x = [ + 1 0 − 1 + 2 0 − 2 + 1 0 − 1 ] ∗ A G_x= \left[ \begin{array} {cccc} +1&0&-1\\ +2 &0&-2\\ +1 &0&-1 \end{array} \right]*A Gx=
+1+2+1000−1−2−1
∗A
G y = [ + 1 + 2 + 1 0 0 0 − 1 − 2 − 1 ] ∗ A G_y= \left[ \begin{array} {cccc} +1&+2&+1\\ 0&0&0\\ -1&-2&-1 \end{array} \right]*A Gy=
+10−1+20−2+10−1
∗A
如上式, G x G_x Gx与 G y G_y Gy分别表示对图像A进行横向和纵向梯度检测得到的结果。
取二者平方和即可得到图像上每一点的梯度值,即在该点同时计算 x x x方向与 y y y方向的梯度。
G = G x 2 + G y 2 G=\sqrt{G_x^2+G_y^2} G=Gx2+Gy2
该点的梯度方向可以通过取这两个值的比的反正切 a r c t a n arctan arctan得到:
Θ = a r c t a n ( G y G x ) \Theta=arctan\left(\frac{G_y}{G_x}\right) Θ=arctan(GxGy)
实现代码如下:
def SobelX(img,threshold): height = img.shape[0] width = img.shape[1] G_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): v = np.sum(G_x * img[i:i + 3, j:j + 3]) result[i,j] =v if(result[i,j]<threshold): result[i,j]=0 return result def SobelY(img,threshold): height = img.shape[0] width = img.shape[1] G_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): h = np.sum(G_y * img[i:i + 3, j:j + 3]) result[i,j] =h if(result[i,j]<threshold): result[i,j]=0 return result def Sobel(img,threshold): height = img.shape[0] width = img.shape[1] G_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) G_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): v = np.sum(G_x * img[i:i + 3, j:j + 3]) h = np.sum(G_y * img[i:i + 3, j:j + 3]) result[i,j] = np.sqrt((v 2) + (h 2)) if(result[i,j]<threshold): result[i,j]=0 return result
二、Scharr算子
def Scharr(img,threshold): height = img.shape[0] width = img.shape[1] G_x = np.array([[-3, 0, 3], [-10, 0, 10], [-3, 0, 3]]) G_y = np.array([[-3, -10, -3], [0, 0, 0], [3, 10, 3]]) result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): v = np.sum(G_x * img[i:i + 3, j:j + 3]) h = np.sum(G_y * img[i:i + 3, j:j + 3]) result[i,j] = np.sqrt((v 2) + (h 2)) if(result[i,j]<threshold): result[i,j]=0 return result
三、Roberts算子
[ − 1 0 0 1 ] \left[ \begin{array} {cccc} -1&0\\ 0&1\\ \end{array} \right] [−1001]
[ 0 − 1 1 0 ] \left[ \begin{array} {cccc} 0&-1\\ 1 &0\\ \end{array} \right] [01−10]
def Roberts(img,threshold): height = img.shape[0] width = img.shape[1] G_x = np.array([[-1, 0], [0,1]]) G_y = np.array([[0, -1], [1,0]]) result = np.zeros(img.shape) for i in range(0, width - 1): for j in range(0, height - 1): v = np.sum(G_x * img[i:i + 2, j:j + 2]) h = np.sum(G_y * img[i:i + 2, j:j + 2]) result[i,j] = np.sqrt((v 2) + (h 2)) if(result[i,j]<threshold): result[i,j]=0 return result
四、拉普拉斯算子
代码实现如下:
def Laplacian(img): temLaplacian = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) height, width = img.shape[::-1] result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): result[i][j] = np.abs(np.sum(temLaplacian * img[i:i + 3, j:j + 3])) return result
import numpy as np import cv2 import imgShow as iS def SobelX(img,threshold): height = img.shape[0] width = img.shape[1] G_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): v = np.sum(G_x * img[i:i + 3, j:j + 3]) result[i,j] =v if(result[i,j]<threshold): result[i,j]=0 return result def SobelY(img,threshold): height = img.shape[0] width = img.shape[1] G_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): h = np.sum(G_y * img[i:i + 3, j:j + 3]) result[i,j] =h if(result[i,j]<threshold): result[i,j]=0 return result def Sobel(img,threshold): height = img.shape[0] width = img.shape[1] G_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) G_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): v = np.sum(G_x * img[i:i + 3, j:j + 3]) h = np.sum(G_y * img[i:i + 3, j:j + 3]) result[i,j] = np.sqrt((v 2) + (h 2)) if(result[i,j]<threshold): result[i,j]=0 return result def Sobel(img,threshold): height = img.shape[0] width = img.shape[1] G_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) G_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): v = np.sum(G_x * img[i:i + 3, j:j + 3]) h = np.sum(G_y * img[i:i + 3, j:j + 3]) result[i,j] = np.sqrt((v 2) + (h 2)) if(result[i,j]<threshold): result[i,j]=0 return result def Scharr(img,threshold): height = img.shape[0] width = img.shape[1] G_x = np.array([[-3, 0, 3], [-10, 0, 10], [-3, 0, 3]]) G_y = np.array([[-3, -10, -3], [0, 0, 0], [3, 10, 3]]) result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): v = np.sum(G_x * img[i:i + 3, j:j + 3]) h = np.sum(G_y * img[i:i + 3, j:j + 3]) result[i,j] = np.sqrt((v 2) + (h 2)) if(result[i,j]<threshold): result[i,j]=0 return result def Roberts(img,threshold): height = img.shape[0] width = img.shape[1] G_x = np.array([[-1, 0], [0,1]]) G_y = np.array([[0, -1], [1,0]]) result = np.zeros(img.shape) for i in range(0, width - 1): for j in range(0, height - 1): v = np.sum(G_x * img[i:i + 2, j:j + 2]) h = np.sum(G_y * img[i:i + 2, j:j + 2]) result[i,j] = np.sqrt((v 2) + (h 2)) if(result[i,j]<threshold): result[i,j]=0 return result def Laplacian(img): temLaplacian = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) height, width = img.shape[::-1] result = np.zeros(img.shape) for i in range(0, width - 2): for j in range(0, height - 2): result[i][j] = np.abs(np.sum(temLaplacian * img[i:i + 3, j:j + 3])) return result img=cv2.imread("./originImg/HorizontalAndVertical.jpg") img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) sobelImg=Sobel(img,56) iS.showImagegray(sobelImg, img, 25, 15, 'sobelDetection', 'origin', './ProcessedImg/sobelDetection.jpg') imageList=[] origin_img=[img,'origin_img'] imageList.append(origin_img) sobelx=SobelX(img,0) sobel2=[sobelx,'Sobel_X'] imageList.append(sobel2) sobely=SobelY(img,0) sobel1=[sobely,'Sobel_Y'] imageList.append(sobel1) sobelImg=Sobel(img,56) sobel3=[sobelImg,'Sobel'] imageList.append(sobel3) iS.showMultipleimages(imageList,25,25,'./ProcessedImg/sobelEdge.jpg') img1=cv2.imread('./originImg/Goldhill.tif') img1=cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) LapImg=Laplacian(img1) iS.showImagegray(LapImg, img1, 25, 15, 'LapImg', 'origin', './ProcessedImg/lapImg.jpg') scharrImg=Scharr(img,56) iS.showImagegray(scharrImg, img, 25, 15, 'scharrDetection', 'origin', './ProcessedImg/scharrDetection.jpg') robertsImg=Roberts(img,56) iS.showImagegray(robertsImg, img, 25, 15, 'robertsDetection', 'origin', './ProcessedImg/robertsDetection.jpg') # cv2.imshow('sobely',sobely) # cv2.waitKey(0) # cv2.destroyAllWindows()
画图代码:
import matplotlib.pyplot as plt import numpy as np import math #图像实际大小为 W*100 * H*100 像素 , def showImagegray(newImg,oldImg,W,H,newImgtitle,oldImgtitle,saveImgpath): plt.figure(figsize=(W,H)) plt.subplot(121) plt.title(oldImgtitle,fontsize=30) plt.axis('off') plt.imshow(oldImg, cmap='gray') plt.subplot(122) plt.title(newImgtitle,fontsize=30) plt.axis('off') plt.imshow(newImg, cmap='gray') # plt.tight_layout() # 调整整体空白 plt.savefig(saveImgpath) plt.show() def showMultipleimages(imageList,W,H,saveImgpath): imageLength=len(imageList) plt.rcParams['figure.figsize'] = (W,H) col=row=math.ceil(np.sqrt(imageLength)) fig, a = plt.subplots(col, row) m = 0 for i in range(col): for j in range(row): a[i][j].set_title(imageList[m][1]) a[i][j].imshow(imageList[m][0], cmap=plt.cm.gray) m += 1 #去掉边框和刻度 for ax in a.flat: ax.set_axis_off() fig.tight_layout() # 调整整体空白 plt.subplots_adjust(wspace=0.2, hspace=0.2) # 调整子图间距 plt.savefig(saveImgpath) plt.show()
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/128006.html