Showing posts with label ImageMagick. Show all posts
Showing posts with label ImageMagick. Show all posts

24 October, 2013

Create Counting Video


On a couple of occasions, I've had the need to create a source video for video processing and testing video processing commands.  A common need is to create a counting video by generating a series of frames and stitching them together at a pre-defined frame rate.

Below is a mechanism for doing so;


$ cat createVideo 
#!/bin/bash

ws=/var/tmp/cache
rm -rf $ws
mkdir -p $ws

for i in `seq 300`; do
  N=$(printf %03d $i)
  convert -size 640x480 -background white -fill black -pointsize 120 -gravity center label:$i $ws/frame-$N.jpg
done

ffmpeg -y -r 2/1 -i $ws/frame-%03d.jpg -r 30/1 $ws/video.avi
mplayer $ws/video.avi


Cheers.

05 October, 2011

ImageMagick -- Send Directly To Display

'til recently I've always manipulated images, stored them to a temporary file, then displayed them to confirm the desired results.

I've recently found that this is an unnecessary step, as the output of any command can be sent directly to the display using the 'win:' command directive.

E.g.

$ convert main/trunk/Media/Photos/Cats/Dempsey/DSCF0002.JPG -resize "100x100^" -gravity center -crop 100x100 win:


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

22 May, 2009

ImageMagick - Montage

I love FFMpeg and ImageMagick. Armed with both, you can create some pretty interesting things. For example, you can create a frame consisting of 4 images by issuing the following command:


montage -geometry 720x480+0+0 -tile 2x2 /var/tmp/00000001.jpg /var/tmp/00000002.jpg /var/tmp/00000003.jpg /var/tmp/00000004.jpg /tmp/output.jpg


This will take these 4 images:





To create the following resultant image:

A framed image like the resultant isn't particularly interesting, unless you incorporate a series of them into a video. For example, at work we wished to determine what frame rate would be necessary to drive a vehicle exclusively using a camera. We investigated this by creating a montage video stream with varying frame rates in each frame.

Another fine use would be comparison of file size using alternative video comparisions. For example, imagine a montage frame with each frame demonstrating a different JPEG image, file size and compression ratio. Heck, you could even generate your very own composite video with each of Charlies Angels in each of their own frames. The uses are endless :)

30 January, 2009

Imagemagick Kung-Fu

ImageMagick is a suite of image manipulation utilities used to create, edit and convert virtually every popular image file format in existence.

We've recently found ImageMagick useful in generating slide and video presentations for our current program. I'll identify some of the features we've found useful in our trials.

Resizing Images
For presentations it's often useful to resize various original images to a consistent image dimension. To convert the Black Ice album cover image with dimensions of 1007x918, to a dimension of 640x480 use the following command.


$ convert -resize 640x480 ./blackIce-1007x918.jpg /tmp/image.jpg

It's worth noting that during the resizing of the image the aspect ratio of the source image is preserved. As a result, the dimensions of the destination image may differ from (but will be no larger than) the specified 640x480 dimension. In this instance to achieve a 640x480 image, the width reduction ratio of 1007/640 = 1.5734375 and height reduction ratio of 918/480 = 1.9125 the resizing uses the larger reduction ratio of 1.9125. The dimensions of the destination image results in 527x480 (1007/1.9125)x(918/1.9125).

Rotating Images

$ convert -rotate 45 ./blackIce-1007x918.jpg /tmp/image.jpg


Overlaying Images
Overlaying images to create a composite image is useful in presenting two perspectives simultaneously. The ordering of source images is relevant, specified in a bottom-to-top ordering. For instance, we can overlay an image of Angus and Brian over the Black Ice cover by specifying:


$ convert -composite ./blackIce-1007x918.jpg ./acdc-560x395.jpg /tmp/image.jpg

The resultant image consists of a composite image achieved by overlaying Brian and Angus over the Black Ice cover. It's important to specify the larger of the images as the bottom image, otherwise no overlay will result and you'll get a copy of the top image cropped to the size of the bottom image.


The gravity feature defines where the image will be overlayed on the background image. NorthWest, North, NorthEast, West, Center, East, SouthWest and SouthEast are the available gravity types. Unless specified the default gravity used is NorthWest. Examples of North gravity and SouthWest follow.

$ convert -gravity North -composite ./blackIce-1007x918.jpg ./acdc-560x395.jpg /tmp/image.jpg



$ convert -gravity SouthWest -composite ./blackIce-1007x918.jpg ./acdc-560x395.jpg /tmp/image.jpg


You have even greater control specifying an exact overlay position with the -geometry feature. For example, you can position the overlayed image at x position 200, y position 40 by specifying:

$ convert ./blackIce-1007x918.jpg ./acdc-560x395.jpg -geometry +200+40 -composite /tmp/image.jpg



Blending Images
You can blend two images together, giving an almost transparent overlay by using the composite command. For instance, you can blend Angus and Brian with the Black Ice label using the command:


$ composite -blend 50 ./acdc-560x395.jpg ./blackIce-1007x918.jpg -geometry +10+0 /tmp/image.jpg





$ composite -blend 50 ./blackIce-1007x918.jpg ./acdc-560x395.jpg -geometry +10+0 /tmp/image.jpg




Stretching both images to a known dimension of 1007x918 gives a more uniform overlay of both images.


$ composite -blend 50 ./acdc-560x395.jpg ./blackIce-1007x918.jpg -geometry +10+0 -resize 1007x918 /tmp/image.jpg



The top image is resized to the underlying image, so if the underlying image is larger the top image is stretched to fit. If, however, the top image is larger than the underlying image the top image is cropped to the dimensions of the underlying image.

Now, young warrior, take these weapons to the masses.