计算机视觉 (六) -- Edge Detection(sobel) & Gaussian Blur

Sobel算子是像素图像边缘检测中最重要的算子之一,在机器学习、数字媒体、计算机视觉等信息科技领域起着举足轻重的作用。在技术上,它是一个离散的一阶 差分算子,用来计算图像亮度函数的一阶梯度之近似值。在图像的任何一点使用此算子,将会产生该点对应的梯度矢量或是其法矢量。 [1]

该算子包含两组3x3的 矩阵,分别为横向及纵向,将之与图像作平面 卷积,即可分别得出横向及纵向的亮度 差分近似值。如果以A代表原始图像,Gx及Gy分别代表经横向及纵向 边缘检测的图像,其公式如下:
图像的每一个像素的横向及纵向梯度近似值可用以下的公式结合,来计算梯度的大小。
可用以下公式计算梯度方向。
在以上例子中,如果以上的角度Θ等于零,即代表图像该处拥有纵向边缘,左方较右方暗。

高斯模糊(https://en.wikipedia.org/wiki/Gaussian_blur),也叫高斯平滑,是在Adobe PhotoshopGIMP以及Paint.NET图像处理软件中广泛使用的处理效果,通常用它来减少图像噪声以及降低细节层次。



import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
import numpy as np

image = cv2.imread( "./imgs/9.jpg" )

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

gray_blur = cv2.GaussianBlur(gray,( 9 , 9 ), 5 )

f = np.fft.fft2(gray / 225.0 )
f_shift = np.fft.fftshift(f)
grayfft = 10 * np.log(np.abs(f_shift))

core_sobelx = np.array([[ - 1 , 0 , 1 ],[ - 2 , 0 , 2 ],[ - 1 , 0 , 1 ]])
core_sobely = np.array([[ - 1 , 0 , 1 ],[ - 2 , 0 , 2 ],[ - 1 , 0 , 1 ]])


gray_blur_sobelx = cv2.filter2D(gray_blur, 2 ,core_sobelx)
gray_blur_sobely = cv2.filter2D(gray_blur, 2 ,core_sobely)
gray_blur = cv2.filter2D(gray, 2 ,core_sobelx)

f,(a0,a1,a2,a3,a4,a5) = plt.subplots( 1 , 6 , figsize = ( 30 , 30 ))

a5.set_title( "fft gray" )
a5.imshow(grayfft, cmap = 'gray' )

a0.set_title( "oraginal gray" )
a0.imshow(gray, cmap = 'gray' )

a1.set_title( "oraginal gray filter" )
a1.imshow(gray_blur, cmap = 'gray' )

a2.set_title( "blur gray" )
a2.imshow(gray_blur, cmap = 'gray' )

a3.set_title( "blur gray with sobel x" )
a3.imshow(gray_blur_sobelx, cmap = 'gray' )

a4.set_title( "blur gray with sobel y" )
a4.imshow(gray_blur_sobely, cmap = 'gray' )

retval,binary_image = cv2.threshold(gray_blur_sobelx, 100 , 225 ,cv2.THRESH_BINARY)

plt.figure()
plt.imshow(binary_image, cmap = 'gray' )
plt.show()




猜你喜欢

转载自blog.csdn.net/u010676526/article/details/80110524