What is “Thresholding” in Pattern Recognition?

The benefits of applying advanced technologies to new fields has become increasingly more evident with the development of machine learning and AI. One of the most powerful aspects of these new technologies is pattern recognition. With the power of pattern recognition, technology is now being applied to a wide variety of problems that humans could only tackle in limited ways in the past.

For example, scientists are able to take images from wildlife camera traps all around the world and more quickly analyze them to identify the species present in habitats of interest. This kind of data helps inform biodiversity studies on issues like habitat usage of select species, the presence of vulnerable species in protected or unprotected habitats, the success of mitigation strategies to prevent the loss of species, etc.

Image for post
Image for post
Image source: https://appsilon.com/object-recognition-transfer-learning/

So how does pattern recognition work?

When you think about it, at their core, computers are essentially pattern recognition machines. Everything you do on a computer involves using any number of programming languages to write lines of code according to rules, or patterns, that the computer will recognize to have some kind of meaning. Based on the interpretation of those patterns, the computer will perform some kind of function, from printing “Hello World” to your console to giving you the user ratings of all of the sushi places within a 25-mile radius from your current location.

What if, though, we wanted to go beyond the simplistic pattern matching paradigm of functional programming and employ computers in the recognition of patterns in actual data they are given? This more sophisticated and abstract pattern recognition paradigm is at the heart of technologies like machine learning.

The pattern recognition involved in translating an image into a computer’s decision of what species that image represents starts with a type of pattern recognition called, simply, image recognition. This is the same process that allows computers to recognize faces in photos, fingerprints in a database, to translate written text on paper to digital text. The more complex the image, the more elaborate the code.

Image for post
Image for post
Image source: https://www.quora.com/What-are-some-examples-of-pattern-recognition

Let’s look at one piece of the pattern recognition process in this article, “thresholding.”

Python (https://www.python.org/) is quickly becoming the go-to programming language for most scientific data analyses. In order to fully take advantage of python’s ability to threshold images, the proper packages need to be installed (via command prompt).

Image for post
Image for post

Once everything is installed, an image can be imported using Pillow and then numpy imported as np.

Image for post
Image for post

The image can be opened using an image processor; following that, the numpy array version must be saved to iar and then that can be output to the console.

Image for post
Image for post

What happens is that we get a 3-dimensional array of the data, in which each matrix block is a row of data and each of the elements within that block is the RGB-A pixel values. The numerical representation of the image looks something like this:

Image for post
Image for post

The more complex the image, the more massive that array, or list, of colors. So when we have an elaborate image, the concept of “thresholding” comes into play, which is essentially a way to simplify the image down to its most basic parts. Thresholding allows us to analyze the average color of an image and essentially set that average color as the threshold between white or black. There are certainly cases where it won’t be that simple (some gradient will exist), but for the purposes of this article, we’re still sticking with a simple example.

Ok, so let’s see how we can apply thresholding to images in order to simplify pattern recognition.

Make sure we set up our imports:

Image for post
Image for post

Now, we need to add some simple images.

Image for post
Image for post

Let’s say we’ve added the following images. All images except the bottom right show a simple zero, though it is hard to see them, given the differences in color. This is where thresholding will come into play.

Image for post
Image for post
Image source: https://pythonprogramming.net/image-recognition-python/

We begin a threshold function which is expecting a parameter, an image Array, that is the array containing the pixel values. We then set the balanceAr as an empty array, and the newAr is the imageArray for now. The balanceAr will be averaged at the end to find our threshold.

Image for post
Image for post

Here is the rest of the code:

def threshold(imageArray):

balanceAr = []
newAr = imageArray
from statistics import mean
for eachRow in imageArray:
for eachPix in eachRow:
avgNum = mean(eachPix[:3])
balanceAr.append(avgNum)

balance = mean(balanceAr)
for eachRow in newAr:
for eachPix in eachRow:
if mean(eachPix[:3]) > balance:
eachPix[0] = 255
eachPix[1] = 255
eachPix[2] = 255
eachPix[3] = 255
else:
eachPix[0] = 0
eachPix[1] = 0
eachPix[2] = 0
eachPix[3] = 255
return newAr

We are assessing each pixel and if it is brighter than averages, it’s white. If it is darker than average, it is black. We can now feed the image array into this function and what we should get back is the thresholded version of the image array. Let’s apply that threshold to the images.

Image for post
Image for post

Our images now look like this:

Image for post
Image for post
Image source: https://pythonprogramming.net/image-recognition-python/

Obviously, zeros are NOT kangaroos. But in this simple example, we can see how pattern recognition can be facilitated by using the thresholding technique. This will come in handy when comparing the outputs of different machine learning algorithms to identify images. And scaling up this technology can be a powerful tool in the fight to protect wildlife. Or monitor cows, chickens and pigs.

Image for post
Image for post
Image source: https://nymag.com/developing/2018/10/what-creatures-may-we-place-in-the-panopticon.html

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store