Opencv+Python实现图像运动模糊和高斯模糊的示例

yipeiwu_com6年前Python基础

运动模糊:由于相机和物体之间的相对运动造成的模糊,又称为动态模糊

Opencv+Python实现运动模糊,主要用到的函数是cv2.filter2D()

# coding: utf-8
import numpy as np
import cv2
def motion_blur(image, degree=12, angle=45):
  image = np.array(image)
  # 这里生成任意角度的运动模糊kernel的矩阵, degree越大,模糊程度越高
  M = cv2.getRotationMatrix2D((degree / 2, degree / 2), angle, 1)
  motion_blur_kernel = np.diag(np.ones(degree))
  motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree))
  motion_blur_kernel = motion_blur_kernel / degree
  blurred = cv2.filter2D(image, -1, motion_blur_kernel)
  # convert to uint8
  cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX)
  blurred = np.array(blurred, dtype=np.uint8)
  return blurred
img = cv2.imread('./9.jpg')
img_ = motion_blur(img)
cv2.imshow('Source image',img)
cv2.imshow('blur image',img_)
cv2.waitKey()

原图:

运动模糊效果:

高斯模糊:图像与二维高斯分布的概率密度函数做卷积,模糊图像细节

Opencv+Python实现高斯模糊,主要用到的函数是cv2.GaussianBlur():

# coding: utf-8
import numpy as np
import cv2
img = cv2.imread('./9.jpg')
img_ = cv2.GaussianBlur(img, ksize=(9, 9), sigmaX=0, sigmaY=0)
cv2.imshow('Source image',img)
cv2.imshow('blur image',img_)
cv2.waitKey()

高斯模糊效果:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

Python函数式编程指南(四):生成器详解

4. 生成器(generator) 4.1. 生成器简介 首先请确信,生成器就是一种迭代器。生成器拥有next方法并且行为与迭代器完全相同,这意味着生成器也可以用于Python的for循...

Python 解码Base64 得到码流格式文本实例

我就废话不多说了,直接上代码吧! # coding:utf8 import base64 def BaseToFlow(): while True: str =...

python杀死一个线程的方法

最近在项目中遇到这一需求: 我需要一个函数工作,比如远程连接一个端口,远程读取文件等,但是我给的时间有限,比如,4秒钟如果你还没有读取完成或者连接成功,我就不等了,很可能对方已经宕机或者...

Python 使用 prettytable 库打印表格美化输出功能

Python 使用 prettytable 库打印表格美化输出功能

pip install prettytable 每次添加一行 from prettytable import PrettyTable # 默认表头:Field 1、Field 2....

python正则表达式re模块详解

快速入门 import re pattern = 'this' text = 'Does this text match the pattern?' match = re...