Convolutions are a very important tool for anyone interested in signal processing. Image Convolutions is a simpler method to do convolutions on images. And they have a lot of uses too. So of course, OpenCV does have a way to do image convolutions easily and super efficiently!
The one single function that does image convolutions in OpenCV is the Filter2D function. Here's the syntax:
void filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta, int borderType);
You'll have to include the cv namespace for the above to work. You can do that with
using namespace cv;
Now for the parameters:
Here's some example code:
filter2D(img, imgFiltered, -1, kernelLoG, Point(-1,-1), 5.0, BORDER_REPLICATE);
The image img is filtered and stored in imgFiltered. The bit depth of imgFiltered will be the same as img (the -1). The convolution will be done using the matrix kernelLog whose anchor is at the center. Also, after the convolution is done, a value of 5.0 will be added to all pixels. The borders are taken care of by replicating pixels around the edges.
The C equivalent of the above function is:
cvFilter2D(IplImage* src, IplImage* dst, CvMat* kernel, CvPoint anchor);
Very similar to the C++ equivalent and a lot simpler too. It doesn't have a lot of extra parameters.
Pixels "outside" the image are set to the value of the pixel nearest inside the image.
The filtering function actually calculates correlation. If you have a symmetrical convolution kernel, the mathematical expressions for correlation and convolution are the same.
If the kernel is not symmetric, you must flip the kernel and set the anchor point to (kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1). This will calculate the actual convolution.
Also, it isn't always necessary that the filtering will happen on the image directly. If the convolution kernel is large enough, OpenCV will automatically switch to a discrete Fourier transform based algorithm for speedy execution.
You got to know how convolutions are done in OpenCV. You learned about the C++ function as well as the C function.