Raspberry Pi Object Counting
Raspberry Pi Object Counting
by BhaskarP6
Computer vision, doubtless, is a fantastic thing! Using Considering how advanced and accessible are
this, a computer gains the capability to "see" and computer vision frameworks and tools, the application
sensing better the environment around, what allows described in this article fits well: using a simple
the development of complex, useful and cool Raspberry PI and a cost-free and open-source
applications. Applications such as face detecting and computer vision framework called OpenCV to count
recognising, object tracking and object detection are objects in movement, more precisely how much
more and more present in our day-to-day activities, objects go in and out of a certain monitored zone.
thanks to computer vision advances.
Download
https://www.instructables.com/ORIG/FEE/T1PD/JV77IE8Z/FEET1PDJV77IE8Z.mp4
…
necessary to infeer that something is moving or if it's reference frame are converted to an gray scale
standing still. Here, to determine is something has image. But.. why? It's a computing efficience issue: a
moved, it's pretty much the same: every single image that presents multiple colors (color image) has
webcam stream captured frame will be compared to a three informations per pixel: Red, Blue and Green
reference frame. If something is different, something color components (the old but gold RGB standard).
has been moved. It's simple as it sounds. So, matematically, each pixel can be defined as a
three-value array, each one representing a color
This reference frame must be captured in the most component. Ttherefore, extending it to the whole
perfect conditions (nothing moving, for example). In image, the final image will be actually the mix of three
the image processing's world, this comparison image components: Red, Blue and Green image
between a captured frame and a reference frame components.
consists in a technic called background subtraction.
Background subtration consists on literaly subtract To Process it, a lot of work is required! However, in
pixel-to-pixel color information from the captured gray-scale images, each pixel has only one color
frame and the reference frame. So, the resulting information. So, the processing of an color image is
image from this proccess will highlight / show with three times slower than in gray-scale image case (at
more details only what is different between these two least three times, depending on what technique is
frames (or, what have moved / got movement) and involved). And there's more: for some purposes (like
everything else will be black in image (the color of this project), process all the colors isn't necessary or
zero value on a gray-scale pixel). Important: lighting important at all. Therefore, we came to the
condictions and quality of webcam image captured conclusion: gray-scale images usage is highly
(due to capture sensors quality) can slightely vary recommended for image processing purpose. After
from frame to frame. It implies that the "equal parts" background subtration, it's necessary to apply
from reference frame and another frames won't be Gaussian Blur filter.
total black after background subtraction. Despite of
this behavior, there's no serious consequences in the The Gaussian Blur filter applied over background
next steps image processing in this project. subtracted image smoothes all contours of the
moving detected object. For sure, it'll be helpul in the
In order to minimize image processing time, before next steps of image processing.
doing a background subtration, captured frame and
In most cases of image processing, binarization is (0x00). Unfortunatelly, threshold value's choice isn't
almost a mandatory step after highlight objects / so easy to make. It depends on environment factors,
characteristcs in a image. Reason: in a binary image, such as lighting conditions. A wrong choice of a
each pixel color can assume two values only: 0x00 threshold value can ruin all the steps further. So, I
(black) or 0xFF (white). This helps a lot the image strongly recommend you adjust manually a threshold
processing in order to require even less "computing in the project for your case before any further actions.
power" to apply image processing techniques in the This threshold value must ensure that the moving
next steps. Binarization can be done comparing each object shows in binary image. In my case, after a
pixel color of the gray-scale image to a certain threshold's adequate choice, results in what you see
threshold. If the value of the pixel color is greater than in figure 5.
threshold,this pixel color will assume white value
(0xFF), and if the value of the pixel color is lower than Figure 5 - binary image
threshold,this pixel color will assume black value
Until now, it was possible to detect moving objects, from particular lighting conditions to some part of the
highlight them and apply binarization, what results in object shape. Once holes can "produce" false objects
a pretty clear image of moving object ( = pretty clear inside real objects (depending on how big and where
image of the object for image processing purposes). they're located), the consequences of holes presence
The preparation for object counting is ALMOST done. in a image can be catastrophic to objects' counting. A
The "ALMOST" here means that there're some fine way to eliminate these holes is using an image
adjusts to make before moving on. At this point, processing Technic called Dilate. Use this and holes
there're real chances of presence of "holes" in the go away.
objects (black masses of pixels into the white
highlighted object). These holes can be anything,
At this point, we have the highlighted objects, no sohard to find / adjust. Once all the objects in the
holes inside it and ready for what's next: the search image are picked, the next step is to draw a retangle
for the contours (and its centroids). There're on it (this retangle must contain an entire detected
resources in OpenCV to detect automatically object inside it). And the center of this rectangle is....
contours, but the detected countours must be wisely the object centroid! You are maybe thinking "What's
chosen (to pick the real object or objects only). So, the big deal with this centroid?", right? Here's your
the criteria to detect the contours is the area of the answer: doesn't matter how big or how is the shape of
object, measured in pixels². If a contour has a higher the object, its movement is the same of the centroid.
area than a limit (configured in software),so it must be In another words: this simple point called centroid
considered as a real object to be counted. The choice represents all the movement of the object. It does
of this area limit/criteria is very important, and a bad makes the counting very simple now, doesn't it? See
choice here means wrong countings. You must try the image below (figure 6), where the object's
some area value limits values and check what fits centroid is represented as a black point.
better to your usage. Don't worry, these limit isn't
The grand finale: compare object's centroid coordinates to entrance and exit lines coordinates and apply the
counting algorythm described before. And there'll be counting of moving objects!
Final result
As shown in the very beginning of this post, here is the project in action:
Download
https://www.instructables.com/ORIG/FNC/84LL/JV77IEFY/FNC84LLJV77IEFY.mp4
…
Download
https://www.instructables.com/ORIG/F79/V2UC/JV77IELJ/F79V2UCJV77IELJ.py
…