Please write code for a MATLAB function that takes as input an image and a specified filter kernel and outputs the filtered image. The function should handle both grayscale and RGB images, and be able to apply a wide range of filter types such as low-pass, high-pass, Gaussian, and median filters. The filter kernel should be a user-defined matrix of arbitrary size and values. The function should be well-documented and follow good software engineering practices such as modular design, efficient memory usage, and error handling.
This prompt doesn't seem malicious.