How to randomly select a pixel within a kernel and attribute its value to the center pixel?
05:35 28 Nov 2025

In order to investigate whether the decisions of a CNN for image classification are predominantly driven by image regions related to relatively fine object features, I am looking for an image pertubation technique that allows for a local disturbance of groups of pixels while retaining the general structure of the displayed object.

In this regard I was thinking about using a moving kernel (similar to a mean filter) in which I randomly select the value of one pixel within the kernel and attribute it to the center pixel. By gradually increasing the kernel size I am aiming to introduce varying levels of disturbance.

Can you help me in solving this issue effectively using Python? I would highly appreciate your coding advice (e.g. which libraries and functions would be beneficial?) or even possible solutions.

python numpy image machine-learning scikit-learn