0

I'm trying to implement a convolution operation for a RGB image by applying the 2D convolution to each channel independently. Following is my code:

kernel = np.array(
    [
        [1,0,1],
        [0,0,0],
        [1,0,0]
    ])
image=images[1]
Hi, Wi, Ci= image.shape
Hk, Wk = kernel.shape
paddingHk = Hk//2
paddingWk = Wk//2
        
new_img = np.pad(image, [(paddingHk, paddingWk),(paddingHk, paddingWk),(0,0)], 'constant', \
                          constant_values=[(0, 0),(0,0),(0,0)])
        
pHi, pWi, cWi= new_img.shape
flipkernel = np.flip(kernel)
    
out = np.zeros((Hi, Wi, Ci))
for k in range(Ci):          
        for i in range(paddingHk, pHi-paddingHk):
                for j in range(paddingWk, pWi-paddingWk):
                    b = new_img[i-paddingHk:i+paddingHk+1, j-paddingWk:j+paddingWk+1,:]
                    out[i-paddingHk][j-paddingWk][k] = np.sum(b[:,:,k]*flipkernel)

out = np.clip(out, 0, 1)
display(out)

The result looks weird:3d conv This is the original image:original

Thanks!

ZeL
  • 1
  • Your kernel looks strange. What operation are you trying to accomplish with the convolution? – Pete Mar 20 '23 at 20:36
  • Also, if you sum 3 elements in the range 0..1, you'll get a result in range 0..3 which will be overly bright and washed out. Maybe you want to divide the result by 3? – Mark Setchell Mar 20 '23 at 21:13
  • I assume you are aware there are highly optimised routines for this within **OpenCV**, **scipy** and **scikit-image**. – Mark Setchell Mar 20 '23 at 21:28

0 Answers0