How to do it...

The filter that we will use here is called the Sobel filter. It is said to be a directional filter, because it only affects the vertical or the horizontal image frequencies depending on which kernel of the filter is used. OpenCV has a function that applies the Sobel operator on an image. The following steps will help us to apply the filters as follows:

  1. To apply a Sobel operator in the horizontal direction, we have to use the following function and parameters:
  cv::Sobel(image,    // input 
           sobelX,    // output 
        CV_8U,     // image type 
        1, 0,      // kernel specification 
        3,         // size of the square kernel  
        0.4, 128); // scale and offset 
  1. Vertical filtering is achieved by the following call (which is very similar to the horizontal filter):

Get OpenCV 4 Computer Vision Application Programming Cookbook - Fourth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.