Apply Filter as a part of preprocessing

Hello,

I want to apply a custom 5x5 filter on image as a part of preprocessing.

Can someone suggest me code.

I have already done
def HPF(img):
image = np.array(img)
kernel = np.array([[1,4,7,4,1],
[4,16,26,16,4],
[7,26,41,26,7],
[4,16,26,16,4],
[1,4,7,4,1]])
kernel = kernel/273
img_rst = cv2.filter2D(np.float32(image),-1,kernel)
new_image = img_rst
return new_image

x = Image.open("/home/iit/Kapil/Code/DIP/lenna.jpg").convert(‘RGB’)
#img = TF.to_pil_image(x)
transform = transforms.Lambda(HPF)
img = transform(x)

but it does not do any kind of blurring (Gaussian Blurr)

Your method works fine using random inputs:

def HPF(img):
    image = np.array(img)
    kernel = np.array(
        [[1,4,7,4,1],
         [4,16,26,16,4],
         [7,26,41,26,7],
         [4,16,26,16,4],
         [1,4,7,4,1]])
    kernel = kernel/273
    img_rst = cv2.filter2D(np.float32(image),-1,kernel)
    new_image = img_rst
    return new_image

x = PIL.Image.fromarray(np.random.uniform(0, 256, (224, 224, 3)).astype(np.uint8))

transform = transforms.Lambda(HPF)
img = transform(x)

plt.imshow(img.astype(np.uint8))
1 Like