Python | cv2 gaussianblur()方法

时间:2020-02-23 14:42:14  来源:igfitidea点击:

在本教程中,我们将看到如何使用Python中的Python编程语言模糊图像,该语言存在于Python中的CV2(计算机视觉)库。

我们可以使用 GaussianBlur()的方法 cv2库 模糊图像。
要使用CV2库,我们需要使用CV2库使用 import statement
我们可以阅读更多关于高斯函数的更多信息

图像的模糊意味着图像的平滑,即去除可能在图像中噪声的异常值像素。

现在让我们看看语法和返回值 GaussianBlur()方法,然后我们将继续执行示例。

语法

cv2.GaussianBlur(src, ksize, sigmaX, sigmaY, borderType)

参数

  • src:n维数组的源/输入。
  • ksize:Kernal是矩阵的(不。行)*(禁用列)的顺序。尺寸以元组的形式给出(没有。列,没有。列)。不。行和否。列应该是奇数。如果给出(0 0),则ksize从给定的sigma值计算ksize:sigmax和sigmay。
  • sigmaX:沿水平方向核的标准偏差值。
  • sigmaY:沿垂直方向核的标准偏差值。
  • borderType:此指定图像的边界,而内核应用于图像的边框。

Bordertype的可能值为:

  • cv2.border_constant.
  • cv2.border_replicate
  • cv2.border_reflect.
  • cv2.border_wrap.
  • cv2.border_reflect_101
  • cv2.border_transparent.
  • cv2.border_reflect101
  • cv2.border_default.
  • cv2.border_isolated.

返回值

它返回N维数组的输出模糊图像。

a) In GaussianBlur() method, you need to pass src and ksize values everytime and either one, two, or all parameters value from remaining sigmax, sigJan and borderType parameter should be passed.
b) Both sigmaX and sigJan parameters become optional if you mention the ksize(kernal size) value other than (0,0).

cv2 gaussianblur()方法示例

现在让我们看看Python代码:

示例 1:使用Gaussianblur()方法 srcksizeborderType参数。

# import computer vision library(cv2) in this code
import cv2
 
# main code
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "C:\Users\user\Desktop\flower.jpg"
 
    # Load/Read an image
    image = cv2.imread(img_path)
 
    # show the image on the newly created image window
    cv2.imshow('Input image',image)
 
    # applying gaussian blur on the image
    blur_img = cv2.GaussianBlur(image,(5,5),cv2.BORDER_DEFAULT)
 
    # show the image on the newly created image window
    cv2.imshow('Blur image',blur_img)

示例 2:使用 GaussianBlur()方法 srcksizesigmaX参数。

# import computer vision library(cv2) in this code
import cv2
 
# main code
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "C:\Users\user\Desktop\flower.jpg"
 
    # Load/Read an image
    image = cv2.imread(img_path)
 
    # show the image on the newly created image window
    cv2.imshow('Input image',image)
 
    # applying gaussian blur on the image with kernal size(5,5)
    # and sigmaX = 5
    blur_img = cv2.GaussianBlur(image,(5,5),5)
 
    # show the image on the newly created image window
    cv2.imshow('Blur image',blur_img)