Search code examples
opencvgaussianderivative

How to apply a partial derivative Gaussian kernel to an image with OpenCV?


I'm trying reproduce results from a paper, in which they convolve the image with an horizontal partial derivative of a Gaussian kernel. I haven't found any way to achieve that with OpenCV. Is that possible ?

Do I have to get Gaussian filter and then compute the partial derivatives by hand ?


Solution

  • OpenCV doesn't have built-in function to calculate Gaussian partial derivatives. But you may use cv::getGaussianKernel and cv::filter2D to do so.

    For example:

      cv::Mat kernel = cv::getGaussianKernel(3, 0.85, CV_32F);
      kernel = kernel.reshape(1, 1);
      cv::filter2D(img, img, CV_8U, kernel);
    

    Please note that cv::getGaussianKernel returns column filter, so you need reshape to make it horizontal.