Skip to main content

Module 3

Masks and Blob Detection

Overview

In Module 1, we learned how we can use thresholds to have OpenCV “pick out” certain parts of an image based on its pixels’ grayscale values. In general terms, this is creating a mask, which is a purely black and white image used to pick out desired features. In this module, we will learn different ways to create these masks, specifically using HSV values to filter out desired parts of our image.

The second part of this module will make use of OpenCV’s blob detection feature, which allows us to count the number of blobs on the mask. We will also learn OpenCV’s Simple Blob Detector parameters, which allow us to filter blobs based on a variety of attributes like circularity, color, size, etc.

Lecture Slides

Click here for lecture slides

Objectives

By the end of Module 3, you will have worked with color dice and HSV filtering, going from this to this.

filtering1

And finally, you will have counted the number of blobs on both the colored image, and the white dice from Modules 1 and 2.

filtering2

Skeleton Code

Modules 1-3: Dice Detection

Note that for Modules 1-3, we will be using the same Jupyter notebook for coding as some modules depend on previous results.

In the Module 3 section of the notebook, you will begin with a new image, that of two colored dice. After using some different techniques, you will arrive at a thresholded image of the dice, called a mask. Finally, after implementation of OpenCV’s simple blob detector, you have a pipeline that takes in an image of a dice and spits out the number of dots, and which pixels they are located at in the image.

Helpful Tips/FAQ

Click here for FAQ sheet

Checkoff Questions

  • Imagine that a dice was thresholded poorly, and the shadow the dice cast was part of the resulting mask. If your blob detector is counting this shadow, how might you fix this?
  • What bitwise function would you use to perform masking? (applying the mask to the original image)
  • Why was adaptive thresholding not necessary for HSV filtering? (i.e., why were we able to set static values for the hue)