Reader small image

You're reading from  OpenCV 4 Computer Vision Application Programming Cookbook - Fourth Edition

Product typeBook
Published inMay 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789340723
Edition4th Edition
Languages
Tools
Right arrow
Authors (2):
David Millán Escrivá
David Millán Escrivá
author image
David Millán Escrivá

David Millán Escrivá was 8 years old when he wrote his first program on an 8086 PC in Basic, which enabled the 2D plotting of basic equations. In 2005, he finished his studies in IT with honors, through the Universitat Politécnica de Valencia, in human-computer interaction supported by computer vision with OpenCV (v0.96). He has worked with Blender, an open source, 3D software project, and on its first commercial movie, Plumiferos, as a computer graphics software developer. David has more than 10 years' experience in IT, with experience in computer vision, computer graphics, pattern recognition, and machine learning, working on different projects, and at different start-ups, and companies. He currently works as a researcher in computer vision.
Read more about David Millán Escrivá

Robert Laganiere
Robert Laganiere
author image
Robert Laganiere

Robert Laganiere is a professor at the School of Electrical Engineering and Computer Science of the University of Ottawa, Canada. He is also a faculty member of the VIVA research lab and is the co-author of several scientific publications and patents in content based video analysis, visual surveillance, driver-assistance, object detection, and tracking. Robert authored the OpenCV2 Computer Vision Application Programming Cookbook in 2011 and co-authored Object Oriented Software Development published by McGraw Hill in 2001. He co-founded Visual Cortek in 2006, an Ottawa-based video analytics start-up that was later acquired by iwatchlife.com in 2009. He is also a consultant in computer vision and has assumed the role of Chief Scientist in a number of start-up companies such as Cognivue Corp, iWatchlife, and Tempo Analytics. Robert has a Bachelor of Electrical Engineering degree from Ecole Polytechnique in Montreal (1987) and MSc and PhD degrees from INRS-Telecommunications, Montreal (1996). You can visit the author's website at laganiere.name.
Read more about Robert Laganiere

View More author details
Right arrow

Transforming Images with Morphological Operations

Mathematical morphology is a theory that was developed in the 1960s for the analysis and processing of discrete images. It defines a series of operators that transform an image by probing it with a predefined shape element. The way this shape element intersects the neighborhood of a pixel determines the result of the operation. This chapter presents the most important morphological operators. It also explores the problems of image segmentation and feature detection using algorithms based on morphological operators.

In this chapter, we will cover the following recipes:

  • Eroding and dilating images using morphological filters
  • Opening and closing images using morphological filters
  • Detecting edges and corners using morphological filters
  • Segmenting images using watersheds
  • Extracting distinctive regions using MSER
  • Extracting foreground...

Eroding and dilating images using morphological filters

Erosion and dilation are the most fundamental morphological operators. Therefore, we will present these in the first recipe of this chapter. The fundamental component in mathematical morphology is the structuring element. A structuring element can be simply defined as a configuration of pixels (the square shape in the following diagram) on which an origin is defined (also called an anchor point). Applying a morphological filter consists of probing each pixel of the image using this structuring element. When the origin of the structuring element is aligned with a given pixel, its intersection with the image defines a set of pixels on which a particular morphological operation is applied (the nine shaded pixels in the following figure). In principle, the structuring element can be of any shape, but most often, a simple shape...

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...

Detecting edges and corners using morphological filters

Morphological filters can also be used to detect specific features in an image. In this recipe, we will learn how to detect contours and corners in a gray-level image.

Getting ready

In this recipe, the following image will be used:

Let's start with our recipe.

How to do it...

Let's have a look at the following code:

  1. The edges of an image to be detected can be extracted by using the appropriate filter of the cv::morphologyEx function. Refer to the following code:
// Get the gradient image using a 3x3 structuring...

Segmenting images using watersheds

The watershed transformation is a popular image-processing algorithm that is used to segment an image into homogenous regions quickly. It relies on the idea that when the image is seen as a topological relief, the homogeneous regions correspond to relatively flat basins, delimited by steep edges. As a result of its simplicity, the original version of this algorithm tends to over-segment the image, which produces multiple small regions. This is why OpenCV proposes a variant of this algorithm that uses a set of predefined markers that guide the definition of the image segments.

How to do it...

The watershed segmentation is obtained through the use of the cv::watershed function. The input for...

Extracting distinctive regions using MSER

In the previous recipe, you learned how an image can be segmented into regions by gradually flooding it and creating watersheds. The maximally stable external regions (MSER) algorithm uses the same immersion analogy in order to extract meaningful regions in an image. These regions will also be created by flooding the image level by level, but this time, we will be interested in the basins that remain relatively stable for a period of time during the immersion process. It will be observed that these regions correspond to some distinctive parts of the scene objects that are pictured in the image.

How to do it...

The basic class to compute the MSER of an image is cv::MSER. An instance...

Extracting foreground objects with the GrabCut algorithm

OpenCV proposes the implementation of another popular algorithm for image segmentation—the GrabCut algorithm. This algorithm is not based on mathematical morphology, but we have presented it here since it shows some similarities in its use with the watershed segmentation algorithm we presented earlier in this chapter. GrabCut is computationally more expensive than watershed, but it generally produces more accurate results. It is the best algorithm to use when you want to extract a foreground object in a still image (for example, to cut and paste an object from one picture to another).

How to do it...

The cv::grabCut function is easy to use. You just need to input...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
OpenCV 4 Computer Vision Application Programming Cookbook - Fourth Edition
Published in: May 2019Publisher: PacktISBN-13: 9781789340723
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
David Millán Escrivá

David Millán Escrivá was 8 years old when he wrote his first program on an 8086 PC in Basic, which enabled the 2D plotting of basic equations. In 2005, he finished his studies in IT with honors, through the Universitat Politécnica de Valencia, in human-computer interaction supported by computer vision with OpenCV (v0.96). He has worked with Blender, an open source, 3D software project, and on its first commercial movie, Plumiferos, as a computer graphics software developer. David has more than 10 years' experience in IT, with experience in computer vision, computer graphics, pattern recognition, and machine learning, working on different projects, and at different start-ups, and companies. He currently works as a researcher in computer vision.
Read more about David Millán Escrivá

author image
Robert Laganiere

Robert Laganiere is a professor at the School of Electrical Engineering and Computer Science of the University of Ottawa, Canada. He is also a faculty member of the VIVA research lab and is the co-author of several scientific publications and patents in content based video analysis, visual surveillance, driver-assistance, object detection, and tracking. Robert authored the OpenCV2 Computer Vision Application Programming Cookbook in 2011 and co-authored Object Oriented Software Development published by McGraw Hill in 2001. He co-founded Visual Cortek in 2006, an Ottawa-based video analytics start-up that was later acquired by iwatchlife.com in 2009. He is also a consultant in computer vision and has assumed the role of Chief Scientist in a number of start-up companies such as Cognivue Corp, iWatchlife, and Tempo Analytics. Robert has a Bachelor of Electrical Engineering degree from Ecole Polytechnique in Montreal (1987) and MSc and PhD degrees from INRS-Telecommunications, Montreal (1996). You can visit the author's website at laganiere.name.
Read more about Robert Laganiere