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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.