Monday 31 October 2016

Detecting bright spots in an image using Python and OpenCV

Detecting multiple bright spots in an image with Python and OpenCV


Figure 7: Detecting multiple bright regions in an image with Python and OpenCV.

Detecting multiple bright spots in an image with Python and OpenCV

Normally when I do code-based tutorials on the PyImageSearch blog I follow a pretty standard template of:
  1. Explaining what the problem is and how we are going to solve it.
  2. Providing code to solve the project.
  3. Demonstrating the results of executing the code.
This template tends to work well for 95% of the PyImageSearch blog posts, but for this one, I’m going to squash the template together into a single step.
I feel that the problem of detecting the brightest regions of an image is pretty self-explanatory so I don’t need to dedicate an entire section to detailing the problem.
I also think that explaining each block of code followed by immediately showing the output of executing that respective block of code will help you better understand what’s going on.
So, with that said, take a look at the following image:
Figure 1: The example image that we are detecting multiple bright objects in using computer vision and image processing techniques.
Figure 1: The example image that we are detecting multiple bright objects in using computer vision and image processing techniques (source image).
In this image we have five lightbulbs.
Our goal is to detect these five lightbulbs in the image and uniquely label them.
To get started, open up a new file and name it detect_bright_spots.py . From there, insert the following code:
Lines 2-7 import our required Python packages. We’ll be using scikit-image in this tutorial, so if you don’t already have it installed on your system be sure to follow these install instructions.
We’ll also be using imutils, my set of convenience functions used to make applying image processing operations easier.
If you don’t already have imutils  installed on your system, you can use pip  to install it for you:
From there, Lines 10-13 parse our command line arguments. We only need a single switch here, --image , which is the path to our input image.
To start detecting the brightest regions in an image, we first need to load our image from disk followed by converting it to grayscale and smoothing (i.e., blurring) it to reduce high frequency noise:
The output of these operations can be seen below:
Figure 2: Converting our image to grayscale and blurring it.
Figure 2: Converting our image to grayscale and blurring it.
Notice how our image  is now (1) grayscale and (2) blurred.
To reveal the brightest regions in the blurred image we need to apply thresholding:
This operation takes any pixel value p >= 200 and sets it to 255 (white). Pixel values < 200 are set to 0 (black).
After thresholding we are left with the following image:
Figure 3: Applying thresholding to reveal the brighter regions of the image.
Figure 3: Applying thresholding to reveal the brighter regions of the image.
Note how the bright areas of the image are now all white while the rest of the image is set to black.
However, there is a bit of noise in this image (i.e., small blobs), so let’s clean it up by performing a series of erosions and dilations:
After applying these operations you can see that our thresh  image is much “cleaner”, although we do still have a few left over blobs that we’d like to exclude (we’ll handle that in our next step):
Figure 4: Utilizing a series of erosions and dilations to help "clean up" the thresholded image by removing small blobs and then regrowing the remaining regions.
Figure 4: Utilizing a series of erosions and dilations to help “clean up” the thresholded image by removing small blobs and then regrowing the remaining regions.
The critical step in this project is to label each of the regions in the above figure; however, even after applying our erosions and dilations we’d still like to filter out any leftover “noisy” regions.
An excellent way to do this is to perform a connected-component analysis:
Line 32 performs the actual connected-component analysis using the scikit-image library. The labels  variable returned from measure.label  has the exact same dimensions as our thresh  image — the only difference is that labels  stores a unique integer for each blob in thresh .
We then initialize a mask  on Line 33 to store only the large blobs.
On Line 36 we start looping over each of the unique labels . If the label  is zero then we know we are examining the background region and can safely ignore it (Lines 38 and 39).
Otherwise, we construct a mask for just the current label  on Lines 43 and 44.
I have provided a GIF animation below that visualizes the construction of the labelMask  for each label . Use this animation to help yourself understand how each of the individual components are accessed and displayed:
Figure 5: A visual animation of applying a connected-component analysis to our thresholded image.
Figure 5: A visual animation of applying a connected-component analysis to our thresholded image.
Line 45 then counts the number of non-zero pixels in the labelMask . If numPixels  exceeds a pre-defined threshold (in this case, a total of 300 pixels), then we consider the blob “large enough” and add it to our mask .
The output mask  can be seen below:
Figure 6: After applying a connected-component analysis we are left with only the larger blobs in the image (which are also bright).
Figure 6: After applying a connected-component analysis we are left with only the larger blobs in the image (which are also bright).
Notice how any small blobs have been filtered out and only the large blobs have been retained.
The last step is to draw the labeled blobs on our image:
First, we need to detect the contours in the mask  image and then sort them from left-to-right (Lines 54-57).
Once our contours have been sorted we can loop over them individually (Line 60).
For each of these contours we’ll compute the minimum enclosing circle (Line 63) which represents the area that the bright region encompasses.
We then uniquely label the region and draw it on our image  (Lines 64-67).
Finally, Lines 70 and 71 display our output results.
To visualize the output for the lightbulb image be sure to download the source code + example images to this blog post using the “Downloads” section found at the bottom of this tutorial.
From there, just execute the following command:
You should then see the following output image:
Figure 7: Detecting multiple bright regions in an image with Python and OpenCV.
Figure 7: Detecting multiple bright regions in an image with Python and OpenCV.
Notice how each of the lightbulbs has been uniquely labeled with a circle drawn to encompass each of the individual bright regions.
You can visualize a a second example by executing this command:
Figure 8: A second example of detecting multiple bright regions using computer vision and image processing techniques (source image).
Figure 8: A second example of detecting multiple bright regions using computer vision and image processing techniques (source image).
This time there are many lightbulbs in the input image! However, even with many bright regions in the image our method is still able to correctly (and uniquely) label each of them.

No comments:

Post a Comment