Detection of 1D linear bar codes

Previous tutorial: Detection of square binary glyphs


The 1.2.7 version of Computer Vision Sandbox comes with a plug-in for detection of 1D linear bar codes. This plug-in does not perform bar code recognition (decoding of lines patterns); instead a new plug-in will be provided for this purpose when ready. Nevertheless, bar code detection on its own can still be used in some applications. Plus we need to start from somewhere anyway.

The Bar Code Detector plug-in can be used from scripting only at this point - it can not be put as a video processing step directly in the sandbox wizard user interface. However, using the plug-in is very simple and requires only a single call to ProcessImage() method of the plug-in.

local table = require "table"
local math  = require "math"

-- Create instances of plug-ins to use
detectorPlugin = Host.CreatePluginInstance( 'BarCodeDetector' )
drawingPlugin  = Host.CreatePluginInstance( 'ImageDrawing' )

-- Colors used to highlight bar code's corners
cornerColors = { 'FF0000', '880000', 'FFAAAA', 'FF8800' }

function Main( )
    image = Host.GetImage( )

    -- Find bar codes in the current image
    detectorPlugin:ProcessImage( image )

    barcodesFound = detectorPlugin:GetProperty( 'barcodesFound' )
    barcodeQuads  = detectorPlugin:GetProperty( 'barcodeQuadrilaterals' )

    -- Tell how many bar codes are detected
    drawingPlugin:CallFunction( 'DrawText', image,
                                'Found bar codes: ' .. tostring( barcodesFound ),
                                { 5, 5 }, 'FF0000', '00000000' )

    -- TODO

The above code just tells the number of detected bar codes. However, lets put some more code and highlight each individual bar code detected, its corners and cross it with a line ( the code below is to replace the 'TODO' comment).

    -- Highlight each detected bar code
    for i = 1, barcodesFound do
        quad = barcodeQuads[i]

        drawingPlugin:CallFunction( 'DrawPolygon', image, quad, 'FF0000' )

        for j = 1, 4 do
            drawingPlugin:CallFunction( 'FillRectangle', image,
                                        { quad[j][1] - 3, quad[j][2] - 3 },
                                        { quad[j][1] + 3, quad[j][2] + 3 },
                                        cornerColors[j] )

        -- Draw line crossing the bar code
        drawingPlugin:CallFunction( 'DrawLine', image,
                  { math.floor( ( quad[1][1] + quad[4][1] ) / 2 ),
                    math.floor( ( quad[1][2] + quad[4][2] ) / 2 ) },
                  { math.floor( ( quad[2][1] + quad[3][1] ) / 2 ),
                    math.floor( ( quad[2][2] + quad[3][2] ) / 2 ) }, 'FF0000' )

Now putting the code into the Lua Scripting plug-in and running it on some video with bar codes may produce result looking similar to the one below.

Regardless of the bar code orientation, coordinates of its corners are provided in such way, that the vector between the first and second points tell the direction of the code (the line connecting light red and dark red corners on the above image). Knowing coordinates of bar code corners, an image of the code itself can be extracted using Extract Quadrilateral plug-in, for example.

This is it with detection of bar codes for now. The next plug-in to develop is to recognize them.

Note: here is complete script for detection of 1D linear bar codes.


Next tutorial: Video source run time properties