Currently I am looking into register allocation using graph colouring
(also see Chaitin's paper on the subject). The graph colouring problem also occurs when
trying to colour countries in a political world map where adjacent countries must have different colours.
I thought it would be interesting to try a minimal implementation in Scheme.
Feel free to suggest improvements in the comment section below .
A (undirected) graph is usually represented by a list of nodes (vertices) and a list of edges.
If there are no insular nodes, a list of edges is sufficient.
In Scheme (here GNU Guile implementation) one can do this as follows.
Using the argument of the minimum one can determine the node with lowest adjacency count.
Now one can recursively remove the node with lowest adjacency count and then assign colours starting with the last node and working backwards.
If an adjacent node has a colour already, another colour must be used.
And here is an example of coloring a graph with a few more nodes.
Having worked with the Ruby programming language for many years I started to get
interested in Scheme. The Scheme programming language has a small and powerful
core supporting closures, hygienic macros, first-class continuations, and of
course the famous interpreter functions eval and apply.
However one thing I don't like about Scheme is that there are different function
names for each type of arguments. E.g. adding numbers is done with +, adding
lists is done with append, and adding strings is done with string-append.
The same program would be much less verbose if + was extended to work with
strings and lists, too:
GOOPS: The Guile extension for object-oriented programming
It turns out that GNU Guile (the Scheme interpreter of the GNU project) has
a mature extension which facilitates this. GOOPS is inspired by the
Common Lisp Object System (CLOS). GOOPS not only provides polymorphic
methods, it even lets you replace existing functions with polymorphic ones:
The define-class method is the normal way to define a class. GOOPS requires
you to define the instance attributes when defining the class. The following
example defines a class <a> with attribute x and a + method. The
make method is used to create the instance a of the class <a>.
Constructors and the next method
One can define a shorthand for instantiating objects. E.g. one can define a
method which sets the attribute #:x to the argument multiplied by two.
IMHO it is better though to define a modified constructor directly. This is more
involved but also possible.
Note the call to next-method. This essentially calls the
next less specialized method for that generic function. Here is another
example using an inheriting class <b> to illustrate the concept.
Note that GOOPS does not implicitly create a metaclass. The following example
shows how to define a metaclass <m<c>> with a class method.
One can also use GOOPS to change the way how objects get displayed and what the
REPL writes to the terminal. E.g. one can define the method write for
the class <a> to change the way the Guile REPL displays objects of that
Furthermore one can implement the method display to define the way objects
get displayed in formatted output.
I have now used GOOPS for a little while. Coming from Ruby there are a few
gotchas when using GOOPS and Guile's module system. For example one needs to use
a #:re-export statement when using a module to replace the core binding for
the + operator.
All in all GOOPS makes quite a mature impression and I think it makes Scheme much
more amenable to developers who are used to thinking in terms of objects and
To quote Guile's foreign function interface documentation:
The more one hacks in Scheme, the more one realizes that there are
actually two computational worlds: one which is warm and alive, that
land of parentheses, and one cold and dead, the land of C and its ilk.
Any comments and suggestions are welcome
If necessary it is also possible to create objects, classes, and metaclasses
dynamically. The following example instantiates the object v of class
<v> of metaclass <m<v>>. Furthermore the generic test is
implemented for <v>.
When I started doing a PhD in machine vision in 2004 I didn't know what I was in for. I thought I would learn about various object recognition algorithms, implement them in C++, and then try to come up with something new. I was motivated to implement 2D object recognition and tracking algorithms and I was hoping to eventually get into 3D object recognition/tracking and/or Visual SLAM (simultaneous localisation and mapping).
The trouble started when I started to realise how many representations of images there are. I am not even talking about colour spaces or compressed images/videos. It is already sufficient to just consider one-channel grey images. Virtually every C/C++ library for handling images comes with its own data structures for representing images. I.e. when trying to use more than one C/C++ library at a time, one ends up writing a lot of code for converting between different representation of images.
It get's worse. CPUs usually offer arithmetic using 8-bit, 16-bit, 32-bit, and 64-bit integers which can be signed or unsigned. Also there are single-precision and double-precision floating point numbers (i.e. 10 or more different native data types). When implementing a C/C++ library which just wants to support basic binary operations (addition, subtraction, division, multiplication, exponent, comparisons, ...) for array-scalar, scalar-array, and array-array combinations, one quickly ends up with literally thousands of possible combinations. This leads to a combinatorial explosion of methods as one can see in the Framewave library for example.
In the end I wrote a thesis about a different way of implementing machine vision systems. The thesis shows how one can implement machine vision software using a popular dynamically typed programming language (i.e. the Ruby programming language).
The listing below shows an IRB (Interactive Ruby) session to illustrate the result. Comment lines (preceded with '#') show the output of the IRB REPL (read-eval-print loop). The session first opens a video display showing the camera image. After closing the window it shows a video display with the thresholded camera image.
See the picture below for an example of a thresholded image.
The example has just 7 lines of code. The REPL furthermore facilitates experimentation with machine vision software in an unprecedented way. The system achieves real-time by generating C-programs for the required operations, compiling them to Ruby extensions, and linking them on-the-fly.
Current machine vision systems (or at least their performance critical parts) are predominantly implemented using statically typed programming languages such as C, C++, or Java. Statically typed languages however are unsuitable for development and maintenance of large scale systems.
When choosing a programming language, dynamically typed languages are usually not considered due to their lack of support for high-performance array operations. This thesis presents efficient implementations of machine vision algorithms with the (dynamically typed) Ruby programming language. The Ruby programming language was used, because it has the best support for meta-programming among the currently popular programming languages. Although the Ruby programming language was used, the approach presented in this thesis could be applied to any programming language which has equal or stronger support for meta-programming (e.g. Racket (former PLT Scheme)).
A Ruby library for performing I/O and array operations was developed as part of this thesis. It is demonstrated how the library facilitates concise implementations of machine vision algorithms commonly used in industrial automation. That is, this thesis is about a different way of implementing machine vision systems. The work could be applied to prototype and in some cases implement machine vision systems in industrial automation and robotics.
The development of real-time machine vision software is facilitated as follows
A just-in-time compiler is used to achieve real-time performance. It is demonstrated that the Ruby syntax is sufficient to integrate the just-in-time compiler transparently.
Various I/O devices are integrated for seamless acquisition, display, and storage of video and audio data.
In combination these two developments preserve the expressiveness of the Ruby programming language while providing good run-time performance of the resulting implementation.
To validate this approach, the performance of different operations is compared with the performance of equivalent C/C++ programs.
I hope that my work has shown that the choice of programming language plays a fundamental role in the implementation of machine vision systems and that those choices should be revisited.
HornetsEye: Ruby computer vision library (developed as part of this thesis)