Book Image

OpenCV 4 Computer Vision Application Programming Cookbook - Fourth Edition

By : David Millán Escrivá, Robert Laganiere
Book Image

OpenCV 4 Computer Vision Application Programming Cookbook - Fourth Edition

By: David Millán Escrivá, Robert Laganiere

Overview of this book

OpenCV is an image and video processing library used for all types of image and video analysis. Throughout the book, you'll work with recipes to implement a variety of tasks. With 70 self-contained tutorials, this book examines common pain points and best practices for computer vision (CV) developers. Each recipe addresses a specific problem and offers a proven, best-practice solution with insights into how it works, so that you can copy the code and configuration files and modify them to suit your needs. This book begins by guiding you through setting up OpenCV, and explaining how to manipulate pixels. You'll understand how you can process images with classes and count pixels with histograms. You'll also learn detecting, describing, and matching interest points. As you advance through the chapters, you'll get to grips with estimating projective relations in images, reconstructing 3D scenes, processing video sequences, and tracking visual motion. In the final chapters, you'll cover deep learning concepts such as face and object detection. By the end of this book, you'll have the skills you need to confidently implement a range of computer vision algorithms to meet the technical requirements of your complex CV projects.
Table of Contents (17 chapters)

Opening and closing images using morphological filters

The previous recipe introduced you to the two fundamental morphological operators—dilation and erosion. From these, other operators can be defined. This and the following recipe will present some of them. The opening and closing operators are presented in this recipe.

How to do it...

In order to apply higher level morphological filters, you need to use the cv::morphologyEx function with the appropriate function code:

  1. To create a closing or opening operator, we have to create a cv::Mat element that will be used as an opening/close kernel:
cv::Mat element5(5,5,CV_8U,cv::Scalar(1)); 
  1. Now, we have to create another cv::Mat to store the result of applying our morphological...