Barry Thomas

Experiments in Machine Vision

In between technical writing contracts I wrote Android apps. My first was a free STL file viewer which was used by thousands of engineers, modellers and designers, all over the world. I wrote mainly in Java, dropping into C/C++ where speed demanded or to use a library like OpenCV.

My next project was in the field of machine vision. This is a dynamic and growing area of work, used in automotive systems, robotics, the military, medicine, manufacturing and a wide range of other sectors.

The app used an open source machine vision library, OpenCV, to process frames from the device's video camera. The app took the video feed frame by frame, processed the data and overlaid the results onto the frame before displaying it on the screen.

The app displayed the results of the following processes:

Source Code

I offer snippets of source code here. The code is based on the sample apps which come with the Android OpenCV 2.4.4 library. The snippets I provide can be dropped into those apps with a bit of work to define some added variables and support methods.

Click on any thumbnail to see the original screen capture. The images were taken from a Nexus 4 (1280 x 768 resolution) using the front camera.

Frame rate: The frame rate is displayed on the screen when viewing the video feed. Some of these processes are very computation-intensive and your Android device is unlikely to be as fast as your desktop computer (unless the latter was built in 1990). However, in all modes the rate improves as the lighting of the scene gets brighter. Use the app in the best lighting available and the frame rate improves significantly.

Canny Edge Detector

Many processes and tools in the OpenCV library use an edge detector process as their first step. When the edges in the video frame have been identified, the features in that image can more readily be identified and used. This mode simply uses the Canny algorithm to process each frame and displays it on the screen.

Hough Circle Detector

The Hough algorithm is used here to detect circular features in the (Canny processed) frame, and then draws a red circle at that location and shows the centre of the circle with a cross. The maximum number of circles outlined is 10 but this can be changed in the code.

Hough Line Detector

Colour Range: Contour Mapper

This process converts the (BGR) frame to HSV, finds all areas of the image where the hue is more or less yellow (values are 'in range'), creates a bit mask of those areas, finds the contours of the areas, creates an approximate polygon for each and overlays them on the original image.

Colour Range: Quadrilaterals

This process carries out the same process as that above, but at the final step checks the contours to see if any are quadrilateral. If so it outlines the contour and draws the diagonals.

Face detection

The Haar cascade used in the app recognises only frontal views, not profile. Having identified a number of faces in the frame the app draws a rectangle over the main features. The largest face is copied to the top left of the screen. If that face is markedly different to the previous largest face it is saved to the SD card of the device. As you scan around a number of faces, you should get one or two saved copies of each face seen.

Good-Features-To-Track

This process is used as first step in a number of other MV procedures. The frame is searched for 'corners', prominent features of the image which are likely to be strong candidates for things to track from one frame to the next, or used when calculating optical flow.

Optical Flow

Uses Optical Flow to compare strong features in each frame with those in the next, then draws a line between matching points in each frame, overlaid on the most recent image.