zoukankan      html  css  js  c++  java
  • python+opencv模拟生成运动模糊核

    参考:OPENCV2学习(1)_生成运动模糊核(OPENCV2 实现Matlab中fspecial的motion功能)

    运动模糊时,模糊后图片某点的值应该与原图沿运动角度方向前面的点有关,并且越近邻影响越大,即权值越大。所以除了确定卷积核之外,还确定了锚点(anchor)

    import math
    import numpy as np
    import cv2
    #生成卷积核和锚点
    def genaratePsf(length,angle):
        EPS=np.finfo(float).eps                                 
        alpha = (angle-math.floor(angle/ 180) *180) /180* math.pi
        cosalpha = math.cos(alpha)  
        sinalpha = math.sin(alpha)  
        if cosalpha < 0:
            xsign = -1
        elif angle == 90:
            xsign = 0
        else:  
            xsign = 1
        psfwdt = 1;  
        #模糊核大小
        sx = int(math.fabs(length*cosalpha + psfwdt*xsign - length*EPS))  
        sy = int(math.fabs(length*sinalpha + psfwdt - length*EPS))
        psf1=np.zeros((sy,sx))
    #psf1是左上角的权值较大,越往右下角权值越小的核。 #这时运动像是从右下角到左上角移动 for i in range(0,sy): for j in range(0,sx): psf1[i][j] = i*math.fabs(cosalpha) - j*sinalpha rad = math.sqrt(i*i + j*j) if rad >= half and math.fabs(psf1[i][j]) <= psfwdt: temp = half - math.fabs((j + psf1[i][j] * sinalpha) / cosalpha) psf1[i][j] = math.sqrt(psf1[i][j] * psf1[i][j] + temp*temp) psf1[i][j] = psfwdt + EPS - math.fabs(psf1[i][j]); if psf1[i][j] < 0: psf1[i][j] = 0 #运动方向是往左上运动,锚点在(0,0) anchor=(0,0) #运动方向是往右上角移动,锚点一个在右上角
    #同时,左右翻转核函数,使得越靠近锚点,权值越大
    if angle<90 and angle>0: psf1=np.fliplr(psf1) anchor=(psf1.shape[1]-1,0) elif angle>-90 and angle<0:#同理:往右下角移动 psf1=np.flipud(psf1) psf1=np.fliplr(psf1) anchor=(psf1.shape[1]-1,psf1.shape[0]-1) elif anchor<-90:#同理:往左下角移动 psf1=np.flipud(psf1) anchor=(0,psf1.shape[0]-1) psf1=psf1/psf1.sum() return psf1,anchor

    demo:

    import motionBlur
    kernel,anchor=motionBlur.genaratePsf(20,40)
    motion_blur=cv2.filter2D(im,-1,kernel,anchor=anchor)

                 after blurred

    至于核权值的计算完全使用参考的博文。

  • 相关阅读:
    spring boot三种方式设置跨域
    完整卸载Mysql
    【OBIEE】OBIEE集成Echarts作图
    【OBIEE】BIEE培训(一)
    【Oracle】Oracle物化视图
    【Oracle】oracle11g安装过程提示swap size 检查失败问题
    【Linux】centOS7下安装GUI图形界面
    【Nginx】Linux环境搭建nginx负载
    【oracle】Oracle创建带参数视图
    抢票:搭建github最火的12306项目
  • 原文地址:https://www.cnblogs.com/wyh1993/p/7118559.html
Copyright © 2011-2022 走看看