Definition
Introduction
A kernel is a matrix (which may be dense or sparse) used in image convolution operations. Content of such a matrix allows image processing operations such as : blurring, sharpening, edge detection, etc.
- See also
- https://en.wikipedia.org/wiki/Kernel_(image_processing)
Using kernels
In 2d case, a kernel is associated to class ipsdk::KernelXY and is represented by a collection of offsets which is associated to class ipsdk::OffsetXY defining its shape, each offset being associated to a value.
In 3d case, a kernel is associated to class ipsdk::KernelXYZ and its collection of offsets is simply replaced by a class ipsdk::OffsetXYZ.
The following examples illustrates the definition of a 2d square user custom kernel given by :
#include <IPSDKBaseData/Pattern/Kernel/KernelUtils.h>
using namespace ipsdk;
void testCustomKernel()
{
const ipInt32 halfKernelSize = 1;
const ipInt32 kernelSize = 2 * halfKernelSize + 1;
Real32Vector valueColl(kernelSize * kernelSize);
valueColl[0] = -1; valueColl[1] = 0; valueColl[2] = 1;
valueColl[3] = -1; valueColl[4] = 0; valueColl[5] = 1;
valueColl[6] = -1; valueColl[7] = 0; valueColl[8] = 1;
KernelXYConstPtr pKernel = squareKernelXY(halfKernelSize, valueColl);
}
The same kernel creation can be written in Python:
import PyIPSDK
valueColl = [-1, 0, 1,
-1, 0, 1,
-1, 0, 1]
kernel = PyIPSDK.squareKernelXY(1, valueColl)
print(str(kernel))
The following C++ example illustrates the definition of two separable gaussian kernels which can be used for smoothing operation :
#include <IPSDKBaseData/Pattern/Kernel/Instances/GaussianKernel.h>
using namespace ipsdk;
void testGaussianKernel()
{
const ipReal32 stdDevX = 1.5f;
const ipReal32 stdDevY = 1.5f;
const ipReal32 gaussianRatio = 0.997f;
const ipUInt32 minHalfKernelSize = 2;
KernelXYConstPtr pKernelX, pKernelY;
separableGaussianSmoothing(stdDevX, stdDevY, gaussianRatio, minHalfKernelSize, pKernelX, pKernelY);
}
The translation in Python is:
import PyIPSDK
kernelX, kernelY = PyIPSDK.separableGaussianSmoothing2d(1.5, 1.5, 0.997, 2)
print(str(kernelX))
Associated kernel values will be sampled on a curve looking like :
Combination of these kernels long X and Y axis allows 2d processing.
- See also
- https://en.wikipedia.org/wiki/Separable_filter
- Note
- Following functions can also be usefull to create user dedicated kernels:
- ipsdk::math::normalDistribution
- ipsdk::math::normalDistributionFirstDerivative
- ipsdk::math::normalDistributionSecondDerivative