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

yipeiwu_com5年前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用于url解码和中文解析的小脚本(python url decoder)

复制代码 代码如下: # -*- coding: utf8 -*- #! python print(repr("测试报警,xxxx是大猪头".decode("UTF8").encode(...

python读csv文件时指定行为表头或无表头的方法

python读csv文件时指定行为表头或无表头的方法

pd.read_csv()方法中header参数,默认为0,标签为0(即第1行)的行为表头。若设置为-1,则无表头。示例如下: (1)不设置header参数(默认)时: df1 =...

tensorflow 1.0用CNN进行图像分类

tensorflow升级到1.0之后,增加了一些高级模块: 如tf.layers, tf.metrics, 和tf.losses,使得代码稍微有些简化。 任务:花卉分类 版本:tenso...

Python使用SQLite和Excel操作进行数据分析

昨日,女票拿了一个Excel文档,里面有上万条数据要进行分析,刚开始一个字段分析,Excel用的不错,还能搞定,到后来两个字段的分析,还有区间比如年龄段的数据分析,实在是心疼的不行,于是...

Python坐标线性插值应用实现

Python坐标线性插值应用实现

一、背景 在野外布设700米的测线,点距为10米,用GPS每隔50米测量一个坐标,再把测线的头和为测量一个坐标。现在需使用线性插值的方法求取每两个坐标之间的其他4个点的值。 二、插值原...