图像处理 之 矩阵二维卷积

# -*- coding: utf-8 -*-
"""
Created on Wed May 02 15:08:01 2018

@author: Diko
"""

import numpy 
from skimage import io ,data

def conv2d_v0010(f_, g_):
    g = g_[::-1, ::-1]
    f = f_
    h = numpy.vstack([numpy.zeros(f_.shape[1] + g_.shape[1] - 1)] * (g_.shape[0] + f_.shape[0] -1))
    
    f_up_and_dowm = numpy.vstack([numpy.zeros(f.shape[1])]*g.shape[0])
    f = numpy.vstack((f_up_and_dowm,f,f_up_and_dowm))
    
    f_left_and_right = numpy.vstack([numpy.zeros(g.shape[1])] * f.shape[0])
    f = numpy.hstack((f_left_and_right, f, f_left_and_right))
    
    for i in xrange(f_.shape[0] + g_.shape[0] - 1):
        for j in xrange(f_.shape[1] + g_.shape[1] -1):
            h[i, j]=(f[i+1:i+1+g_.shape[0],j+1:j+1+g_.shape[1]] *g).sum()
            
    print "f=\n", f
    print "g=\n", g
    print "h=\n", h
    io.imshow(h)
    return h
    
            

if __name__ == "__main__":
    f_shape, g_shape = 10, 3
    f_, g_ = numpy.arange(f_shape ), numpy.arange(g_shape)
    f = [f_+i for i in xrange(f_shape)]
    g = [g_+i*5 for i in xrange(g_shape)]
    
    f = numpy.vstack(f) 
    g = numpy.vstack(g)
    f = data.camera()
    io.imshow(f)
    conv2d_v0010(f, g)

 

你可能感兴趣的:(图像处理)