Reader small image

You're reading from  Mastering OpenCV 4 with Python

Product typeBook
Published inMar 2019
Reading LevelExpert
PublisherPackt
ISBN-139781789344912
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Alberto Fernández Villán
Alberto Fernández Villán
author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán

Right arrow

Contour Detection, Filtering, and Drawing

A contour can be defined as a sequence of points defining the boundary of an object in an image. Therefore, contours convey key information about the object boundary, encoding the main information about the object shape. This information serves as the basis of image descriptors (for example, SIFT, Fourier descriptors or shape context, among others) and it can be used for shape analysis and object detection and recognition.

In this chapter, you will see how to deal with contours, which are used for shape analysis and object detection and recognition.

In this chapter, key points in connection with contours will be tackled in the following topics:

  • An introduction to contours
  • Compressing contours
  • Image moments
  • More functionality related to contours
  • Filtering contours
  • Recognizing contours
  • Matching contours
...

Technical requirements

The technical requirements are listed as follows:

  • Python and OpenCV
  • Python-specific IDE
  • NumPy and Matplotlib packages
  • Git client

Further details about how to install these requirements are in Chapter 1, Setting Up OpenCV. The GitHub repository for Mastering OpenCV 4 with Python containing all the supporting project files, which are necessary to work through the book from the first chapter to the last one, can be accessed in the next URL: https://github.com/PacktPublishing/Mastering-OpenCV-4-with-Python/.

An introduction to contours

Contours can be seen as a curve joining all the points along the boundary of a certain shape. As they define the boundary of the shape, an analysis of these points can reveal key information for shape analysis and object detection and recognition. OpenCV provides many functions to properly detect and process contours. However, before diving into these functions, we are going to see the structure of a sample contour. For example, the following function simulates detecting a contour in a hypothetical image:

def get_one_contour():
"""Returns a 'fixed' contour"""

cnts = [np.array(
[[[600, 320]], [[563, 460]], [[460, 562]], [[320, 600]], [[180, 563]], [[78, 460]], [[40, 320]], [[77, 180]], [[179, 78]], [[319, 40]], [[459, 77]], [[562, 179]]], dtype=np.int32)]
return cnts

As you can see, a contour is an array composed...

Compressing contours

Detected contours can be compressed to reduce the number of points. In this sense, OpenCV provides several methods to reduce the number of points. This can be set with the parameter method. Additionally, this compression can be disabled by setting the flag to cv2.CHAIN_APPROX_NONE, where all boundary points are stored; hence, no compression is performed.

The cv2.CHAIN_APPROX_SIMPLE method can be used to compress the detected contours because it compresses horizontal, vertical, and diagonal segments of the contour, preserving only endpoints. For example, if we use cv2.CHAIN_APPROX_SIMPLE to compress the contour of a rectangle, it will only be composed of four points.

Finally, OpenCV provides two more flags for compressing contours based on the Teh-Chin algorithm, which is a non-parametric method. The first step of this algorithm determines the region of support...

Image moments

In mathematics, a moment can be seen as a specific quantitative measure of a function shape. An image moment can be seen as a weighted average of image pixel intensities, or a function of such moments, encoding some interesting properties. In this sense, image moments are useful to describe some properties of the detected contours (for example, the center of mass of the object, or the area of the object, among others).

cv2.moments() can be used to calculate all the moments up to the third order of a vector shape or a rasterized shape.

The signature for this method is as follows:

retval = cv.moments(array[, binaryImage])

Therefore, in order to calculate the moments for a detected contour (for example, the first detected contour), perform the following:

M = cv2.moments(contours[0])

If we print M, we get the following information:

{'m00': 235283.0, 'm10...

Filtering contours

In previous sections, we have seen how to calculate the size of a detected contour. The size of a detected contour can be calculated based on image moments or using the OpenCV function cv2.contourArea(). In this example, we are going to sort the detected contours based on the computed size for each one.

Therefore, the sort_contours_size() function is key:

def sort_contours_size(cnts):
""" Sort contours based on the size"""

cnts_sizes = [cv2.contourArea(contour) for contour in cnts]
(cnts_sizes, cnts) = zip(*sorted(zip(cnts_sizes, cnts)))
return cnts_sizes, cnts

Before explaining the code of this function, we are going to introduce some key points. The * operator can be used in conjunction with zip() to unzip the list:

coordinate = ['x', 'y', 'z']
value = [5, 4, 3]
result = zip(coordinate,...

Recognizing contours

We have previously introduced cv2.approxPolyDP(), which can be used to approximate one contour with another with fewer points using the Douglas-Peucker algorithm. A key parameter in this function is epsilon, which sets the approximation accuracy. In contours_shape_recognition.py, we will make use of cv2.approxPolyDP() in order to recognize the contours (for example, triangle, square, rectangle, pentagon, or hexagon, among others) based on the number of detected vertices in the decimated contour (the output of cv2.approxPolyDP()). In order to decimate the number of points, given a certain contour, we first compute the perimeter of the contour. Based on the perimeter, the epsilon parameter is established. This way, the decimated contour is invariant to scale. The epsilon parameter is calculated as follows:

epsilon = 0.03 * perimeter

The constant 0.03 is established...

Matching contours

Hu moment invariants can be used for both object matching and recognition. In this section, we are going to see how to match contours based on Hu moment invariants. OpenCV provides cv2.matchShapes(), which can be used to compare two contours using three comparison methods. All these methods use Hu moment invariants. The three implemented methods are cv2.CONTOURS_MATCH_I1, cv2.CONTOURS_MATCH_I2, and cv2.CONTOURS_MATCH_I3.

If A denotes the first object and B denotes the second object, then the following applies:

are the Hu moments of A and B, respectively.

Finally, see the following:

  • cv2.CONTOURS_MATCH_I1:
  • cv2.CONTOURS_MATCH_I2:
  • cv2.CONTOURS_MATCH_I3:

In contours_matching.py, we are making use of cv2.matchShapes() to match several contours against a perfect circle contour.

First of all, we draw a perfect circle in an image by using the OpenCV function...

Summary

In this chapter, we have reviewed the main functionality OpenCV provides in connection with contours. Additionally, we have also coded some useful functions when comparing and describing contours. Moreover, we have also provided some interesting functionality that can be helpful when debugging your code. In this sense, functions for both creating reduced contours and creating images with simple shapes are provided. With this chapter we have finished four chapters related to image processing techniques—Chapter 5, Image Processing Techniques, reviewed key points in image processing; Chapter 6, Constructing and Building Histograms, introduced histograms; Chapter 7, Thresholding Techniques, covered thresholding techniques; and finally, in this chapter, we have explained how to deal with contours.

In the next chapter, we are going to supply an introduction to augmented...

Questions

  1. What function should you use if you want to detect contours in a binary image?
  2. What four flags does OpenCV provide for compressing contours?
  3. What function does OpenCV provide to compute image moments?
  4. What moment provides the size of the contour?
  5. What function does OpenCV provide to calculate the seven Hu moment invariants?
  6. What function should you use if you want to get a contour approximation of a given contour?
  7. The extreme_points() function defined in the contour_functionality.py script can be rewritten in a more compact way as explained in this chapter. Therefore, rewrite it accordingly.
  8. What function should you use if you want to match contours using Hu moment invariants as features?

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering OpenCV 4 with Python
Published in: Mar 2019Publisher: PacktISBN-13: 9781789344912
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

Author (1)

author image
Alberto Fernández Villán

Alberto Fernndez Villn is a software engineer with more than 12 years of experience in developing innovative solutions. In the last couple of years, he has been working in various projects related to monitoring systems for industrial plants, applying both Internet of Things (IoT) and big data technologies. He has a Ph.D. in computer vision (2017), a deep learning certification (2018), and several publications in connection with computer vision and machine learning in journals such as Machine Vision and Applications, IEEE Transactions on Industrial Informatics, Sensors, IEEE Transactions on Industry Applications, IEEE Latin America Transactions, and more. As of 2013, he is a registered and active user (albertofernandez) on the Q&A OpenCV forum.
Read more about Alberto Fernández Villán