Press "Enter" to skip to content

Example

Optical Object Recognition Example

This simple example demonstates how easily object regognition and image processing applications can be implemented in Inlab-Scheme. Given is the following input bitmap, an excerpt of a high quality scan at a resolution of 300DPI:

input for OCR example
input for OCR example

The code below looks for the following search pattern, an “e”:

pattern to look for
lookup pattern

This is the Inlab-Scheme source that analyzes each object in the input bitmap returning the number of actually found matching objects. The number of found objects is displayed and a output bitmap is generated that contains the objects that match the search criteria (of being at least 85% equal in the sense of “bitmap-equality” after resizing to 100×100) unchanged and all other objects in grey:

#!/usr/local/bin/scheme

(define cbitmap (bitmap-readxbm "pattern.xbm")) ;; pattern to search 
 (define bitmap (bitmap-readxbm "input.xbm")) ;; input to analyze 
 (define objects (bitmap-explode bitmap)) ;; explode input 
 (define mcount 0) ;; number of matching objects 
 (define object-list '()) ;; initialize list of objects(writeln 
 (length objects) 
 " objects in input.xbm")

(for-each (lambda (object) 
 (define 
 sobject 
 (bitmap-scale-absolute object 100 100)) 
 (if (> (bitmap-equality sobject cbitmap) 0.85) 
 (begin 
 (set! mcount (+ mcount 1)) 
 (set! object-list 
 (cons object object-list))) 
 (begin 
 (bitmap-grey! object) 
 (set! object-list 
 (cons object object-list))) 
 )) objects)

(writeln mcount " matching objects found")

;; generate output-bitmap and write it to output.xbm

(bitmap-writexbm 
 (bitmap-implode object-list 
 (bitmap-height bitmap) 
 (bitmap-width bitmap)) 
 "output.xbm")

(exit)

A run of the program leads to the following output:

57 objects in input.xbm
7 matching objects found

output.xbm looks now like the following and finally indicates the found objects in black:

output with recognised patterns
output with recognised patterns

The running time was 1.1 seconds on an old Pentium90 running FreeBSD 2.1.5 including initialization of Inlab-Scheme which took about 0.2 seconds on this platform (A pentium pro 200 running FreeBSD 2.1.5 needed about 0.44 seconds including 0.05 seconds to initialize).