biais.org

Sunday 9 September 2007

My favorite pieces of art revisited

I wrote a small Processing script that take a picture apply a kind of pointillize filter and save a PDF file. Here sample output I generated from some of my favorites pieces of art:

  • Wassily Kandinsky

Download 1600x1200 Wassily Kandinsky Revisited

  • Paul Klee

Download 1600x1200 Paul Klee Revisited

  • Bernard Buffet

Download 1600x1200 Bernard Buffet Revisited

  • Alfred Gockel

Download 1600x1200 Alfred Gockel Revisited

  • Joan Miro

Download 1600x1200 Joan Miro Revisited

The code:

PImage a;
import processing.pdf.*;
 
void setup()
{
  a = loadImage("INPUT.jpg");
  size(1600, 1200, PDF, "OUTPUT.pdf");
  background(255);
  noStroke();
  smooth();
  noLoop();
}
 
float drawOne(int x, int y, color pix, float size, int alpha)
{
  fill(pix, random(50)+alpha);
  size = random(size);
  ellipse(x, y, size, size);
  return size; 
}
 
void drawAll(int loops, float isize)
{
  float size;
  int x, y;
  color pix;
 
  for (int i = 0; i < loops; i++) {
     size = isize;
     x = int(random(a.width));
     y = int(random(a.height));
     pix = a.get(x, y);
     size = drawOne(x, y, pix, size, 100);
     size = drawOne(x, y, color(255, 255,255), size, 170);
     size = drawOne(x, y, pix, size, 100);
  }
}
 
void draw()
{ 
  int l = 10;
  for (int i = l; i > 0; i--){
    drawAll(200 * (l - i), 50*i);
  }
  exit();
}

Wednesday 5 September 2007

Viterbi algorithm variant in Python

A variant of the Viterbi Algorithm returning the less energy path of a matrix. Example:

 
M = [[4, 1, 3],
     [3, 4, 2],
     [2, 1, 4]]

This matrix M is projected to the following virtual trellis:

In this example, the path' energy from top to bottom [1, 1, 1] (passing the node 1 then 4 then 1) is 6 ( = abs(1-4) + abs(4-1)).

def viterbi(matrix):
    """Viterbi algorithm : find the best 8-connected path in a matrix.
    
    Example, here the best path from top to bottom is to follow the "4"s 
    (diagonal), the energy of the path is 0
    >>> viterbi([[4, 1, 3], \
                 [3, 4, 2], \
                 [2, 1, 4]])
    [0, 1, 2]
    
    On the next example, 2 best paths, only the first found is returned
    >>> viterbi([[1, 2, 2], \
                 [1, 1, 2], \
                 [1, 2, 1]])
    [0, 0, 0]
    
    A basic example. Best path energy: 1
    >>> viterbi([[1, 0], \
                 [4, 2]])
    [0, 1]
    """
    # init
    h = len(matrix) - 1
    w = len(matrix[0])
    k = 0
    S = {}      #Survivors
    L = {}      #Accumulate Length
    for i in range(w):
        L[(k, i)] = 0
 
    # recursion
    for k in range(h):
        for x in range(w):
            for dx in range(-1, 2):
                if (x + dx) < 0 or (x + dx) >= w:
                    continue
                tmpl = L[k, x] + abs(matrix[k][x] - matrix[k+1][x+dx])
                if (not (k+1, x+dx) in L) or (tmpl < L[(k+1, x+dx)]):
                    L[(k+1, x+dx)] = tmpl
                    S[(k+1, x+dx)] = x
 
    # look for the smallest
    tmp, idx = -1, -1
    for i in range(w):
        if L[(h, i)] < tmp or tmp == -1:
            tmp = L[(h, i)]
            idx = i
 
    # create the inversed path
    res = [idx]
    for k in range(h, 0, -1):
        idx = S[(k, idx)]
        res.append(idx)
 
    # reverse the path
    res.reverse()
    return res

Notes: