OpenCV+Python实现图像运动模糊和高斯模糊

栏目: Python · 发布时间: 7年前

内容简介:OpenCV+Python实现运动模糊,主要用到的函数是cv2.filter2D():原图与运动模糊效果如下:

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

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('linuxidc.com.jpg')
img_ = motion_blur(img)

cv2.imshow('Source image',img)
cv2.imshow('blur image',img_)
cv2.waitKey()

原图与运动模糊效果如下:

OpenCV+Python实现图像运动模糊和高斯模糊

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

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

# coding: utf-8
import numpy as np
import cv2

img = cv2.imread('linuxidc.com.jpg')
img_ = cv2.GaussianBlur(img, ksize=(9, 9), sigmaX=0, sigmaY=0)

cv2.imshow('Source image',img)
cv2.imshow('blur image',img_)
cv2.waitKey()

高斯模糊效果如下:

OpenCV+Python实现图像运动模糊和高斯模糊

更多 Python 相关信息见 Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17

Linux公社的RSS地址 : https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2019-05/158653.htm


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Java Web高级编程

Java Web高级编程

威廉斯 (Nicholas S.Williams) / 王肖锋 / 清华大学出版社 / 2015-6-1 / CNY 99.80

Java成为世界上编程语言之一是有其优势的。熟悉JavaSE的程序员可以轻松地进入到Java EE开发中,构建出安全、可靠和具有扩展性的企业级应用程序。编写《Java Web高级编程——涵盖WebSockets、Spring Framework、JPA Hibernate和 Spring Security》一书的目的正是如此。 《Java Web高级编程:涵盖WebSockets、Sp......一起来看看 《Java Web高级编程》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具