26 June, 2009

Drawing Primitives with ImageMagick

ImageMagick offers far more than simple image conversion utilities. It also extends to you the ability to create new images using drawing primitives. I've recently found this useful in creating artificially created videos use to test image processing and tracking techniques. The following script supports creating a series of circular targets in a newly created frame. By creating a series of frames, stitching them together with FFMpeg and you can create a video of moving targets of know positions. Useful for testing algorithms.

Later.


#!/usr/bin/tclsh

proc createFrame { fileName imageL targets } {
array set image $imageL
set tL ""

foreach e $targets {
switch [lindex $e 0] {
circle
{
set x0 [lindex [split [lindex $e 1] ,] 0]
set y0 [lindex [split [lindex $e 1] ,] 1]
set r [lindex $e 2]
set x1 [expr $x0 + $r]
set y1 $y0
set tL [concat $tL "-fill red -draw 'circle $x0,$y0 $x1,$y1'"]
}
}
}

set cmd "/usr/bin/convert -size $image(width)x$image(height) xc:white $tL $fileName"
puts $cmd
exec bash -c $cmd
}

#---main---
set image(width) 640
set image(height) 480
set fileName /tmp/frame00000001.pnm
lappend targets [list circle 320,240 10]
lappend targets [list circle 100,100 10]
createFrame $fileName [array get image] $targets

No comments: