Monday, October 29, 2012

batch processing using Imagemagick

Imagemagick is a wonderful tool if you want to do batch image processing.

There are few operations which are very useful, but I tend to forget them all the time. :P

Image resizing:

$mogrify -resize HxW  *.jpg

where, HxW is image size (Height x Width). We can take any image format in place of jpg.

If we want to force the desired size,

$mogrify -resize HxW! *.jpg


I prefer using the second command to the first one.

Remember, this replaces the original image file. So you need to take a backup of the current image directory before applying this command.


Format Converstion:

For conversion of all images in current directory to another format,

$mogrify -format <newformat>  *.<currentformat>


for example,
$mogrify -format ppm *.bmp

This converts all bmp images to ppm format and saves a copy.

We can apply this command without worrying at all, since a new copy is created without losing original image format.

Autocrop Images: 

Usually when we save matlab figures, we get large undesirable white boundaries.  We may want to crop those images to fit to actual image dimensions.

$convert -trim image.jpg image.jpg

For processing images in a batch, you can use the following script:
$for i in `ls *.jpg`; do convert $i -trim `basename $i`; done

If you do not want to replace the current image, and rather make a new copy of it, do as follows:
$for i in `ls *.jpg`; do convert $i -trim cropped_`basename $i`; done
 

Refer this link to go through more complicated settings for the same purpose.

confusion in VL_SLIC

The input image for segmentation:

Way1: Read the img as im2double , so the intensity ranges from 0 to 1. Here, the output superpixels are more regular and spacious.

%Input
img = im2double(imread('Images/2_14_s.bmp'));
regionSize = 30 ;
regularizer = 0.1;

%get the SLIC superpixels
vl_setup
segments1 = vl_slic(single(img), regionSize, regularizer) ;












Way2: Read the img as uint8 , so the intensity ranges from 0 to 255.
Here, the output superpixels are more irregular but tightly fit to the edges.

%Input
img = imread('Images/2_14_s.bmp');
regionSize = 30 ;
regularizer = 0.1;

%get the SLIC superpixels
vl_setup
segments1 = vl_slic(single(img), regionSize, regularizer) ;
I want to know which is the right way? and why this change is affecting the performance?