Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

conv2 python

import numpy as np
from scipy.ndimage.filters import convolve

def conv2(x,y,mode='same'):
    """
    Emulate the function conv2 from Mathworks.

    Usage:

    z = conv2(x,y,mode='same')

    TODO: 
     - Support other modes than 'same' (see conv2.m)
    """

    if not(mode == 'same'):
        raise Exception("Mode not supported")

    # Add singleton dimensions
    if (len(x.shape) < len(y.shape)):
        dim = x.shape
        for i in range(len(x.shape),len(y.shape)):
            dim = (1,) + dim
        x = x.reshape(dim)
    elif (len(y.shape) < len(x.shape)):
        dim = y.shape
        for i in range(len(y.shape),len(x.shape)):
            dim = (1,) + dim
        y = y.reshape(dim)

    origin = ()

    # Apparently, the origin must be set in a special way to reproduce
    # the results of scipy.signal.convolve and Matlab
    for i in range(len(x.shape)):
        if ( (x.shape[i] - y.shape[i]) % 2 == 0 and
             x.shape[i] > 1 and
             y.shape[i] > 1):
            origin = origin + (-1,)
        else:
            origin = origin + (0,)

    z = convolve(x,y, mode='constant', origin=origin)

    return z
Comment

PREVIOUS NEXT
Code Example
Python :: convert python script to exe 
Python :: python __lt__ 
Python :: how to find the longest string python 
Python :: map a list to another list python 
Python :: python sort an array 
Python :: python math functions 
Python :: python create random mac 
Python :: how to numbered jupyter notebook 
Python :: separate words in a text to make a list python 
Python :: free download django app for windows 10 
Python :: python set python key default 
Python :: cv2 frame size 
Python :: how to declare private attribute in python 
Python :: gyp err! stack error: command failed: c:python39python.exe -c import sys; print "%s.%s.%s" % sys.version_info[:3]; 
Python :: sort 2 lists together python 
Python :: How to take multiple input form python 
Python :: request session python 
Python :: traversing dictionary in python 
Python :: keyboard python 
Python :: python sys 
Python :: add timestamp csv python 
Python :: quantile calcultion using pandas 
Python :: pdf to string python 
Python :: Convert datetime object to a String of date only in Python 
Python :: smtp django 
Python :: stop flask server 
Python :: len of iterator python 
Python :: reply_photo bot telegram python 
Python :: python unpacking 
Python :: python3 call parent constructor 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =