Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering OpenCV 4 with Python

You're reading from  Mastering OpenCV 4 with Python

Product type Book
Published in Mar 2019
Publisher Packt
ISBN-13 9781789344912
Pages 532 pages
Edition 1st Edition
Languages
Author (1):
Alberto Fernández Villán Alberto Fernández Villán
Profile icon Alberto Fernández Villán

Table of Contents (20) Chapters

Preface 1. Section 1: Introduction to OpenCV 4 and Python
2. Setting Up OpenCV 3. Image Basics in OpenCV 4. Handling Files and Images 5. Constructing Basic Shapes in OpenCV 6. Section 2: Image Processing in OpenCV
7. Image Processing Techniques 8. Constructing and Building Histograms 9. Thresholding Techniques 10. Contour Detection, Filtering, and Drawing 11. Augmented Reality 12. Section 3: Machine Learning and Deep Learning in OpenCV
13. Machine Learning with OpenCV 14. Face Detection, Tracking, and Recognition 15. Introduction to Deep Learning 16. Section 4: Mobile and Web Computer Vision
17. Mobile and Web Computer Vision with Python and OpenCV 18. Assessments 19. Other Books You May Enjoy

Thresholding Techniques

Image segmentation is a key process in many computer vision applications. It is commonly used to partition an image into different regions that, ideally, correspond to real-world objects extracted from the background. Therefore, image segmentation is an important step in image recognition and content analysis. Image thresholding is a simple, yet effective, image segmentation method, where the pixels are partitioned depending on their intensity value, and so, it can be used to partition an image into a foreground and background.

In this chapter, you will learn the importance of thresholding techniques in your computer vision projects. The main thresholding techniques that OpenCV (and also the scikit-image image processing library) provides, which you will need in your computer vision applications as a key part of image segmentation, will be reviewed.

The...

Technical requirements

The technical requirements are listed as follows:

  • Python and OpenCV.
  • A Python-specific IDE.
  • The NumPy and Matplotlib packages.
  • The scikit-image image processing library (optional for the last section of this chapter. See the Thresholding algorithms using scikit-image section to know how to install it for Conda-based distributions). See the following instructions in order to install it using pip.
  • The SciPy library (This is optional for the last section of this chapter) is also required. See the following instructions in order to install it using pip.
  • A Git client.

Further details about how to install these requirements are covered in Chapter 1, Setting Up OpenCV. The GitHub repository for Mastering OpenCV 4 with Python, containing all the supporting project files necessary to work through the book from the first chapter to the last one, can be accessed...

Introducing thresholding techniques

Thresholding is a simple, yet effective method for image partitioning into a foreground and background. The objective of image segmentation is to modify the representation of an image into another representation that is easier to process. For example, image segmentation is commonly used to extract objects from the background based on some properties of the object (for example, color, edges, or histogram). The simplest thresholding methods replace each pixel in the source image with a black pixel if the pixel intensity is less than some predefined constant (the threshold value), or a white pixel, if the pixel intensity is greater than the threshold value.

OpenCV provides the cv2.threshold() function to threshold images. We will see this function in further detail in the next subsections of this chapter.

In the thresholding_introduction.py script...

Simple thresholding

In order to perform simple thresholding, OpenCV provides the cv2.threshold() function that was briefly introduced in the previous section. The signature for this method is as follows:

cv2.threshold(src, thresh, maxval, type, dst=None) -> retval, dst

The cv2.threshold() function applies a fixed-level thresholding to the src input array (multiple-channel, 8-bit or 32-bit floating point). The fixed level is adjusted by the thresh parameter, which sets the threshold value. The type parameter sets the thresholding type, which will be further explained in the next subsection.

Different types are as follows:

  • cv2.THRESH_BINARY
  • cv2.THRESH_BINARY_INV
  • cv2.THRESH_TRUNC
  • cv2.THRESH_TOZERO
  • cv2.THRESH_TOZERO_INV
  • cv2.THRESH_OTSU
  • cv2.THRESH_TRIANGLE

Additionally, the maxval parameter sets the maximum value to use only with the cv2.THRESH_BINARY and cv2.THRESH_BINARY_INV...

Adaptive thresholding

In the previous section, we have applied cv2.threshold() using a global threshold value. As we could see, the obtained results were not very good due to the different illumination conditions in the different areas of the image. In these cases, you can try adaptive thresholding. In OpenCV, the adaptive thresholding is performed by the cv2.adapativeThreshold() function. The signature for this method is as follows:

adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) -> dst

This function applies an adaptive threshold to the src array (8-bit single-channel image). The maxValue parameter sets the value for the pixels in the dst image for which the condition is satisfied. The adaptiveMethod parameter sets the adaptive thresholding algorithm to use:

  • cv2.ADAPTIVE_THRESH_MEAN_C: The T(x, y) threshold value is calculated as the mean...

Otsu's thresholding algorithm

As we saw in previous sections, the simple thresholding algorithm applies an arbitrary global threshold value. In this case, what we need to do is experiment with different thresholding values and look at the thresholded images in order to see if the result satisfies our necessities. However, this approach can be very tedious.

One solution is to use the adaptive thresholding that OpenCV provides by means of the cv2.adapativeThreshold() function. When applying adaptive thresholding in OpenCV, there is no need to set a thresholding value, which is a good thing.

However, two parameters should be established correctly: the blockSize parameter and the C parameter. Another approach is to use Otsu's binarization algorithm, which is a good approach when dealing with bimodal images. A bimodal image can be characterized by its histogram containing...

The triangle binarization algorithm

Another automatic thresholding algorithm is the triangle algorithm, which is considered a shape-based method because it analyzes the structure (or shape) of the histogram (for example, trying to find valleys, peaks, and other shape histogram features). This algorithm works in three steps. In the first step, a line is calculated between the maximum of the histogram at bmax on the gray level axis and the lowest value bmin on the gray level axis. In the second step, the distance from the line (calculated in the first step) to the histogram for all the values of b [bmin-bmax] is calculated. Finally, in the third step, the level where the distance between the histogram and the line is maximal is chosen as the threshold value.

The way to use the triangle binarization algorithm in OpenCV is very similar to Otsu's algorithm. In fact, only one...

Thresholding color images

The cv2.threshold() function can also be applied to multi-channel images. This can be seen in the thresholding_bgr.py script. In this case, the cv2.threshold() function applies the thresholding operation in each of the channels of the BGR image. This produces the same result as applying this function in each channel and merging the thresholded channels:

ret1, thresh1 = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY)

Therefore, the preceding line of code produces the same result as performing the following:

(b, g, r) = cv2.split(image)
ret2, thresh2 = cv2.threshold(b, 150, 255, cv2.THRESH_BINARY)
ret3, thresh3 = cv2.threshold(g, 150, 255, cv2.THRESH_BINARY)
ret4, thresh4 = cv2.threshold(r, 150, 255, cv2.THRESH_BINARY)
bgr_thresh = cv2.merge((thresh2, thresh3, thresh4))

The result can be seen in the following screenshot:

Although you can perform cv2.threshold...

Thresholding algorithms using scikit-image

As we mentioned in Chapter 1, Setting Up OpenCV, there are other packages that can be used for scientific computing, data science, machine learning, deep learning, and computer vision. In connection with computer vision, scikit-image is a collection of algorithms for image processing (https://scikit-image.org/). Images manipulated by scikit-image are NumPy arrays.

In this section, we will make use of scikit-image capabilities in connection with thresholding techniques. So, if you want to replicate the results obtained here, the first step is to install it. See https://scikit-image.org/download.html in order to install scikit-image properly on your operating system. Here, we are going to install it with conda, which is an open source package management system (and also an environment management system). See Chapter 1, Setting Up OpenCV...

Summary

In this chapter, we have reviewed the main thresholding techniques you can use to threshold your images. Thresholding techniques can be used for many computer vision tasks (for example, text recognition and image segmentation, among others). Both simple and adaptive thresholding techniques have been reviewed. Additionally, we have seen how to apply Otsu's binarization algorithm and the triangle algorithm to automatically select a global threshold for thresholding your images. Finally, we have seen how to use different thresholding techniques using scikit-image. In this sense, two global thresholding techniques (Otsu's and triangle algorithms) and two local thresholding techniques (Niblack's and Sauvola's algorithms) have been applied to a test image.

In Chapter 8, Contour Detection, Filtering, and Drawing, we will see how to deal with contours, which...

Questions

  1. Apply a thresholding operation using cv2.threshold() with a threshold value of 100 and using the cv2.THRESH_BINARY thresholding type.
  2. Apply an adaptive thresholding operation using cv2.adapativeThreshold() ,cv2.ADAPTIVE_THRESH_MEAN_C, C=2 and blockSize=9.
  3. Apply Otsu's thresholding using the cv2.THRESH_BINARY thresholding type.
  4. Apply triangle thresholding using the cv2.THRESH_BINARY thresholding type.
  5. Apply Otsu's thresholding using scikit-image.
  6. Apply triangle thresholding using scikit-image.
  7. Apply Niblack's thresholding using scikit-image.
  8. Apply Sauvola's thresholding using scikit-image and a window size of 25.
  9. Modify the thresholding_example.py script in order to make use of np.arange(), with the purpose of defining the threshold values to apply to the cv2.threshold() function. Afterwards, call the cv2.threshold() function with the defined threshold...

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 2019 Publisher: Packt ISBN-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.
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}