Thursday, August 13, 2020

OpenCV convolution

  Convolution is general purpose filter for image processing where center pixel and its neighboring pixels gets multiplied with a matrix i.e kernel 


some example of kernel:

 


OpenCV does convolution using filter2D function. 

The basic usage is given below.

void cv::filter2D   (   InputArray  src,
OutputArray     dst,
int     ddepth,
InputArray  kernel,
Point   anchor = Point(-1,-1),
double  delta = 0,
int     borderType = BORDER_DEFAULT 
)

where, 

  • src is input image.
  • dst is output image of the same size and the same number of channels as src.
  • ddepth is desired depth of the destination image.
  • kernel is convolution kernel, a single-channel floating point matrix; i
  • anchor anchor of the kernel that indicates the relative position of a filtered point within the kernel; the anchor should lie within the kernel; default value (-1,-1) means that the anchor is at the kernel center.
  • delta optional value added to the filtered pixels before storing them in dst.
  • borderType pixel extrapolation method.


// Result image 
Mat result; 

// Apply convolution
filter2D(image, result, -1 , kernel, Point(-1, -1), 0, BORDER_DEFAULT);