Giter VIP home page Giter VIP logo

conv2-python-numpy's Introduction

Conv2-python-numpy

在学习lucy-richardson算法过程中,发现numpy中没有自带的二维卷积函数。因此用numpy写一个。 二维卷积的原理是,先在矩阵周围填充上0,之后再滑动卷积核,在卷积范围内的矩阵与卷积核对应位置相乘再相加,其中卷积核要翻转180度,但在本程序中没有写。 假如矩阵是$m_1,n_1$ 大小,卷积核是$m_2,n_2$ ,在矩阵前后填充$m_2-1,n_2-1$ 排列的0.

输出形状为$full$

def Conv2_full(martix ,psf):  
    [m1 ,n1] = np.shape(martix)  
    [m2 ,n2] = np.shape(psf)  
    img_pad = np.pad(martix ,((m2 - 1 ,n2 - 1) ,(m2 - 1 ,n2 - 1)) ,'constant' ,constant_values=(0 ,0))  
    res_t = np.zeros_like(img_pad)  
    for i in range(m1 + m2 - 1):  
        for j in range(n1 + n2 - 1):      
            temp = img_pad[i:i + m2 ,j:j + n2]  
            temp = np.multiply(temp ,psf).sum()  
            res_t[i][j] = temp  
    return res

输出形状为$same$

def Conv2_same(martix ,psf):  
    [m1 ,n1] = np.shape(martix)  
    [m2 ,n2] = np.shape(psf)  
    img_pad = np.pad(martix ,((m2 - 1 ,n2 - 1) ,(m2 - 1 ,n2 - 1)) ,'constant' ,constant_values=(0 ,0))  
    res_t = np.zeros_like(img_pad)     
    for i in range(int(m2 / 2) ,int(m1 + m2 / 2)):  
        for j in range(int(n2 / 2) ,int(n1 + n2 / 2)):  
            temp = img_pad[i:i + m2 ,j:j + n2]  
            temp = np.multiply(temp ,psf).sum()  
            res_t[i][j] = temp  
    res = res_t[int(m2 / 2):int(m1 + m2 / 2) ,int(n2 / 2):int(n1 + n2 / 2)]  
    return res

conv2-python-numpy's People

Contributors

s33j33j33 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.