In image processing, why do we always use odd square kernels(3x3, 5x5, 7x7, etc.) for filters?
Why can't we use kernels of size 3x1, 5x3, etc. that are rectangular kernels? Also, why do we not prefer even kernels (2x2, 4x4, 6x6, etc.)?
You can use whatever size of kernel you like. The kernel is not necessary to be a square especially when you want to pay more attention to process along a specific orientation. In fact, moving average along a specific axis in a image is a simple filter with rectangular shape.
Gaussian filters, probably one of the most used filters in image processing, are based on gaussian function in which the top value is achieved on the axis of symmetry. This is the main reason why such kinds of kernels are preferably to be odd.
Kernel size selection is often supported in the filter kernel options in the image processing packages, such as all the
imfilter related functions in Matlab image processing toolbox. Yet I would also like to take an high-pass filter example if you design the filters by yourself. For a square kernel with odd dimensions, the value for all filter weights can be set to a negative value, except for the center cell, which has a positive value which increases with the size of the kernel; A square kernel that has even dimensions has the positive value in the central group of four cells; A rectangular kernel has a center group of positive value cells in proportion to the kernel's dimensions. They all work as a high-pass filter.
If the kernel is odd then you can center it on top of a pixel, which is nice. Lots of kernels are symmetric, so that's probably why they are often square. You can certainly use non-square or even kernels though.
You don't. This must be the consequence of a special implementation. Convolution is defined in all these cases.
Okay, so what is the source pixel? It is the anchor point at which the kernel is centered and we are encoding all the neighboring pixels, including the anchor/source pixel. Since, the kernel is symmetrically shaped (not symmetric in kernel values), there is equal number (n) of the pixel on all sides (4- connectivity) of the anchor pixel. Therefore, whatever this number of pixels maybe, the length of each side of our symmetrically shaped kernel is 2*n+1 (each side of the anchor + the anchor pixel), and therefore filter/kernels are always odd sized.