How to reduce the number of for loops I used for convolution of a 2D gray scale Image?

Viewed 18

Constraints: can not call on convolve, correlate, fftconvolve, or any other similar functions.

Query: Is there a way to get rid of the for loops I used to perform the convolution because for larger sized images the efficiency of the program would reduce or is there a better method altogether to perform the same task?

Pre-Context: img0 is a 2D gray scale image and h is the 2D convolution mask used with odd dimensions.

Here is the code I used:

import numpy as np

def myImageFilter(img0, h):
    img0_r = img0.shape[0]
    img0_c = img0.shape[1]
    m = h.shape[0]
    n = h.shape[1]
    img1 = np.zeros((img0_r,img0_c))
    row_pad = m//2
    col_pad = n//2
    f = np.pad( img0, ( (row_pad,row_pad) , (col_pad,col_pad)), 'edge')
    h_prime = np.flip(h)

    for i in range( 0 + row_pad , img0.shape[0]+row_pad ):
        for j in range( 0 + col_pad , img0.shape[1]+col_pad):
            g = np.sum( np.multiply( f[ i-row_pad : i+row_pad +1 , j-col_pad : j+col_pad +1], h_prime ) )
            img1[i-row_pad][j-col_pad] = g
    print(img1)
    return img1
0 Answers
Related