Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a function as a convolution kernel

Hello fellow programmers,

I am currently doing this years advent of code. On day 9 I need to find out wether a pixel in an image has the smallest value of the adjacent pixels.

I first solved this very intuitively and crudely, but now after looking at it for a while was wondering if there was a way to define a convolution kernel for opencv's filter2D-function or another appropriate library I could try where I can define the kernel as a function, such that I can not only perform linear transformations, but also others.

In this specific example I was thinking about a kernel, that can tell if the middle pixel has the lowest value. Maybe there even is a way to do this using linear transformations, however i am not able to find it.

Any help is appreciated.

like image 657
Lenny Eing Avatar asked Feb 26 '26 05:02

Lenny Eing


1 Answers

I found a solution myself simply using scipy.ndimage.generic_filter

import numpy as np
from scipy.ndimage import generic_filter

# First define the footprint of the filter you are going to use:
footprint = np.array([[False, True, False],
                     [True, True, True],
                     [False, True, False]])

# Then define the filter you want to use:
def lowpoint_kernel(a):
    return 0 if a[2] < a[0] and a[2] < a[1] and a[2] < a[3] and a[2] < a[4] else 10

# Get your input here
image = ...

# Pad your image
image = np.pad(image, 1, constant_values=10)

lowpoints = generic_filter(image, lowpoint_kernel, footprint=footprint)
lowpoint_indices = lowpoints == 0

This might very well be a very complicated solution, but maybe someone can find this useful. See my full solution here.

like image 166
Lenny Eing Avatar answered Feb 27 '26 18:02

Lenny Eing