The following tutorial describes one way to use a vision system to count the values of
dice after they are rolled into view of a camera.
Our first example starts with an image of some colored dice.
Our first impluse is to extract out the white dots by simply detecting the red
color of our dice and then removing that from the image. But first we add in
Mean Module with value of 2 to help smooth the color within the image. You will
not notice much of a change to the unmagnified eye but this filter helps
to soften the color. Then we apply the RGB Filter module
to extract out just the red within the image. The Mean with Red extraction then
looks like
We then select the Black Mask result of the RGB Filter to produce
which shows the white dots plus the background white. We can now
count the number of blobs using the Blob Count module.
Finally using the Display Variables module we
can display that count into the image. The result appears like
which we can clearly see is not the right count. We would expect the
count to be +1 given the background white blob but 17 is incorrect (+2).
The reason for this is in the middle of the dice a white blob is
created due to the dice touching each other. We could remove this
incorrect blob using size but that would restrict the camera movement
to the same distance from the dice otherwise the actual spots may be
removed if they become larger with camera movement.
Instead, we can use the Blob Colorize
followed by the
Blob Filter to
remove all blobs whose original average intensity is below a certain amount.
As the center blob is darker than the white spots we can use intensity
to remove this erroneous blob.
You can see the effect of the Blob Colorize
module in that the background blob approximates the color of the surface
we are using (plastic table). Now the count is correct (minus 1 for the background).
When viewed on a live camera this count can flicker at times since the spots may
momentarily become connected. This is normal camera noise which we can reduce
by using an Average module which will average
together serveral images (10 in our case) to produce a much more stable image.
You do get some blurring when motion happens but not enought to cause us problems if the number
of frames used is kept low.
We can then try a couple more combinations.
which all seem to work fine. The following robofile can
be used to replicate these results.
The problem now is that regular black and white dice will not work! Given the image
there is no red color and thus no dice are extracted. In this case
we need to use a thresholding technique to isolate the spots (now black instead of white)
against the rest of the image. We can do this using the Auto_Threshold
module and the Two Peaks setting. The thresholded image set again to Black Mask appears as
but now our previous trick of removing the inner part of a connected
set of dice will fail since the spots are now also dark!
In this case instead of using intensity to remove the erronious inner blob
we use the Blob Filter to remove blobs
that are not very circular in shape. A setting of 0.60 will remove the
inner blob and the outer background blob (its not a circle either) to
leave us with just our circles that we wish to count.
The final count is then displayed correctly for the Black and White dice.
The following robofile can
be used to replicate these results.
However, when viewed with a camera the thresholding technique used (Two Peaks) can
cause the image to momentarily jump into an incorrect state. As pixel values will
contain noise any global thresholding technique can cause flickering in the image. A
more stable technique to use is the Adaptive Threshold module
which determines threshold values more locally and is more stable overall. Substituting this
module results in final stable dice counter script.
You can download that robofile that
is used in the movie below.