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设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

pandas进行时间数据的转换和计算时间差并提取年月日

pandas进行时间数据的转换和计算时间差并提取年月日

#pd.to_datetime函数 #读取数据 import pandas as pd data = pd.read_csv('police.csv') #将stop_date转...

Python优先队列实现方法示例

本文实例讲述了Python优先队列实现方法。分享给大家供大家参考,具体如下: 1. 代码 import Queue import threading class Job(object...

Python求解正态分布置信区间教程

Python求解正态分布置信区间教程

正态分布和置信区间 正态分布(Normal Distribution)又叫高斯分布,是一种非常重要的概率分布。其概率密度函数的数学表达如下: 置信区间是对该区间能包含未知参数的可置信的...

使用Django Form解决表单数据无法动态刷新的两种方法

使用Django Form解决表单数据无法动态刷新的两种方法

一、无法动态更新数据的实例 1. 如下,数据库中创建了班级表和教师表,两张表的对应关系为“多对多” from django.db import models class Classe...

Python Subprocess模块原理及实例

Python Subprocess模块原理及实例

前言 其实有一个模块也支持执行系统命令,那个模块就是sys.system,但他执行系统命令会直接通过主进程去执行命令,那假如,该命令的执行需要耗费一个小时,那么主进程会卡一个小时,而不...