10 July, 2009

Convert Color Video To GrayScale

I've recently found it useful to convert color video into grayscale video. I found the following to be effective in doing so.


$ ffmpeg -y -i /tmp/video.avi -s 640x480 -pix_fmt gray -vcodec rawvideo /var/tmp/video.avi


I haven't however found a means to re-encode the grayscale into an alternative codec (sigh).

06 July, 2009

Narrowing Your Google Search

I was just surfing for ServiceMix documentation and came across instructions at a website that suggested searching their site for specific info by using Google and constraining the search to their site.

Paint my ass and call me Sally.....I didn't know you could do that.

Seems that you can constrain a search to a given site by specifying a site: directive.

For example,

site:fatslowkid.blogspot.com imagemagick


This'll limit your search to the fatslowkid.blogspot.com site, looking for instances of imagemagick.

Cheers.

03 July, 2009

ImageMagick Frame Differences

ImageMagick has some image processing utilities built right in. I recently found a means to display the differences between 2 PNM images.

By using these image processing utilities you can compare the following images to detect the changes between frames by using the following command:

$ convert frame00000002.bmp frame00000001.bmp -compose difference -composite -threshold 0 -negate diff.bmp




The resultant image is as follows:



Another side note that's worth mentioning, you don't have to redirect the results to a file. You can instead redirect the output to stdout and pipe it into the display utility for immediate rendering to screen; as follows:

$ convert frame00000002.bmp frame00000001.bmp -compose difference -composite -threshold 0 -negate - | display



Gotta love the command pipe!!

30 June, 2009

Power of the Pipe

For quite some time I've it's been a mystery to me why many Unix-flavored toolsuites offer a command line interface supporting inputting or outputting to stdin/stdout respectively.

For example, ffmpeg can utilize stdin in leu of a input file by specifying -; similarly it can utilize stdout in leu of an output file using the same - specifier.

I've recently become aware of how useful this option can be. In particular it allows you to bypass creating unnecessary temporary files.

For example, using ffmpeg you can redirect the output to stdout and into mplayer to review the impacts of varying codecs and video quality specifiers.


$ ffmpeg -i /var/tmp/foo.mpg -f asf - | mplayer -cache 8192 -


Similarly, you can make use of the similar feature with ImageMagick like so:

$ convert Lenna.jpg - | display -


In each case, redirecting to stdout and piping to a consuming display utility eliminates the need for creating a temporary file.

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

19 June, 2009

Cpu Load Monitoring

We've recently had explore collecting and analyzing CPU resource loading metrics for our project. The host OS is Linux 2.6 based and rather than install a performance-specific set of utilities we aimed at utilizing readily available services.

Ask any Linux weenie what CPU loading they have been experiencing and most will respond by starting the top utility. Top is available on most Unix distributions, is a well known utility and has pretty much stood the test of time. For those reasons it seemed obvious to incorporate it into a performance monitoring data collection and analysis tool suite.

The following Tcl script takes two arguments, the first an output file which will be populated with the redirected top log file, the second is a duration (in seconds) of how long to monitor the performance. After the raw collection has completed the script parses the log, extracts the CPU loading metrics and outputs a median and mean CPU processing load expressed in percentage of utilization.

It's worth noting that the script incorporates searching for the regular expression *Cpu* to find the top line item holding the Cpu metrics. As a result, if you have a process running that matches *Cpu* you'll find the script errors out during processing of the log file. I'll leave it as a lesson to the reader how to correct (wink).

We've extended on the same principle to monitor network activities.

Have fun.


#!/usr/bin/tclsh

proc log { msg } {
# puts stderr __$msg
}

proc mean { L } {
set sum 0.0
foreach e $L {
set sum [expr $sum + $e]
}
return [expr $sum / [llength $L].]
}

proc median { L } {
set L1 [lsort -real $L]
return [lindex $L1 [expr [llength $L1] / 2]]
}

proc process { fileName } {
set fp [open $fileName r]
while { [gets $fp line] >= 0 } {
switch -glob -- $line {
*Cpu*
{
log "processing line '$line'"
set el [split $line ,]
set userLoad [string trimleft [lindex $el 0] "Cpu(s):"]
set sysLoad [lindex $el 1]
set niceLoad [lindex $el 2]
set idleLoad [lindex $el 3]
log "parsing $userLoad"
log "parsing $sysLoad"
log "parsing $niceLoad"
log "parsing $idleLoad"
set userLoad [lindex [split $userLoad \%] 0]
set sysLoad [lindex [split $sysLoad \%] 0]
set niceLoad [lindex [split $niceLoad \%] 0]
set idleLoad [lindex [split $idleLoad \%] 0]
log "extracted $userLoad"
log "extracted $sysLoad"
log "extracted $niceLoad"
log "extracted $idleLoad"
# puts [expr $userLoad+$sysLoad+$niceLoad+$idleLoad]
lappend L [expr 100.0-$idleLoad]
}
}
}
close $fp
puts "mean cpu load: [mean $L]"
puts "median cpu load: [median $L]"
}

proc monitorCpuLoad { fileName duration } {
catch { exec top -b -n $duration -d 1 > $fileName }
process $fileName
}

proc help { } {
upvar #0 argv0 argv0
puts stderr "usage: $argv0 \[logFileName\] \[duration\]"
exit
}

#---main---
if { [llength $argv] != 2 } {
help
}
monitorCpuLoad [lindex $argv 0] [lindex $argv 1]



18 June, 2009

Capturing Radio Broadcast Streams with Mplayer

My wife and I took our honeymoon in Australia and I can admit that there are numerous occasions where I hunger for a bit of Aussie. I recently explored how to capture an audio stream to play back on my MP3 player (since our office frowns on streaming).

This is how I've been capturing Triple-J streams for fun;


$ mplayer http://202.6.74.107:8060/triplej.mp3 -dumpstream -dumpfile /tmp/audio.mp3