In our last class meeting we talked about computer vision – how to make you program evaluate and respond to live images from a camera connected to the computer.
The following is a short recapitulation of what we covered in the meeting.
Computer Vision Experiments
We used the external library OpenCV for these experiments, which you can find here: http://ubaa.net/shared/processing/opencv/
There are two parts that need to be installed, first the Intel OpenCV framework and second the processing library. Please follow the instruction given at the webpage (in case you did not install the libs already in class).
Blob Detection
The first example is a simple implementation of the blob detection function of open cv. In order to facilitate the detection of blobs the camera image is turned into a posterized black and white image. Play with the threshold value in order to adjust it to the lighting situation of your set-up. The best results for image analysis are achieved when you have a uniform and evenly lit background. When you start your application you have to calibrate it by reading the image of the background into memory (command “opencv.remeber()”). The current image of the camera is then compared to the saved image and resulting differences are tracked. In the sketch I am providing for download you can hit the space bar in order to save an image to memory.
The variables minArea and maxArea allow you to specify the minimum and maximum size of the blobs you want to consider. Play with these values to see the effect. The variable maxBlobs determines how many blobs are detected.
Activity Tracking
The second example is a small modification of the first sketch, which you can use to track the activity in front of the camera. The main differences between the first sketch for blob detection and this one is that the camera image is saved to memory after it is analyzed and used for comparison with the next frame. The strategy is that minimal or no movement means minimal or no difference between between the current image and the previous image, resulting in 0 or a low number of blobs detected. A lot of activity in front of the camera creates a lot of difference between the current and the previous frame, resulting in a higher number of blobs. Play with the size and the amount of blobs to find the best result for your purposes.
Color Tracking
The same sketch can be modified to track only blobs of a certain color (such as find the red blob in the image). The code is the same as in the first example, at the beginning save a reference image of the background. After the image has been converted to a posterized black and white image and was analyzed for blobs you have to restore the original color image (command “opencv.restore(RGB)”) and analyze the colors in this image in the areas of all the blobs found in the first part. The sketch is sampling an area of 5 by 5 pixels around the center of each blobs and compares the average colors in this area with the color that is searched for. The color to look for is defined with its red, green, and blue components in the variables redSearched, greenSearched, and blueSearched. If a blob is found that corresponds in color to these values it will be highlighted with a green frame and its area will be determined. By tracking size variations you may be able to infer to the distance of the object from the camera. The precision of these values depends very strongly on the set-up of your background, lighting situation etc. The variable searchTolerance allows to specify a certain tolerance within which the values may differ from the original value you are looking for. Play with all these values to find the right values for your situation.
Face Detection
The last example is a sketch that uses OpenCV’s ability to detect faces in the camera image. Only faces looking into the camera with both eyes visible are recognized by the algorithm. The variable facesAmount will reflect how many faces are currently detected in the camera image. This number allows you to find out how many people are currently facing the camera. Again, play with the values in the sketch to find the best results. For this sketch you do not need to save a reference image to memory. Faces are always detected no matter what background – but sometimes, if you have a very noisy background other patterns in the background that resemble faces might be tracked as well.