I'm taking an online course of image processing in which all the problem sets were given in MATLAB. Nevertheless, I'm trying to solve them with Python and I get some unexpected results when I try to compute image derivative using convolution with the following kernel: [0.5,0, -0.5]
.
Given row i
, I want to calculate the columns derivatives using convolution with g
.
image[i,:] = [1,2,3,4]
g = [0.5,0,-0.5]
I convolute the two using the following code:
inImage_i_conv = np.zeros_like(inImage_i)
for j in range(0,len(inImage_i)-1):
conv = []
for m in range(len(dy)):
l = m-1
conv.append(inImage_i[j-l]*dy[l+1])
inImage_i_conv[j] = np.sum(conv)
and the result is array([-1, 1, 1, 0])
.
The reason for having -1
at the beginning is that under j = 0
and l = 1
, I actually get the [-1]
element, which in Python is the n
th element.
Should I add a 0
before the i
th row (or equivalently 0
column on the leftmost column of the image)? Should I add a copy of the first element to the left?
What is the common practice? because my results are obviously wrong.