User:Rama/focus stacking

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
align_image_stack -m -a AIS_ IMG_????.JPG
enfuse -o result.tif --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask AIS_????.tif

My perl script[edit]

Usage

Put images to stack in "targetDirectory" and use

$ focusstacking targetDirectory/

The script will output a "result.tiff" in "targetDirectory".

Requirements
  • Perl
  • rename
  • convert (part of ImageMagik)
  • enfuse and enblend (part of hugin)
Installation
  • copy-paste the following text into a file ("focusstack.pl" for instance)
  • autorise excecution : "chmod +x focusstack.pl"
  • as root, copy file into /usr/bin (there are more elegant ways to do this).
#!/usr/bin/perl
use strict; 

my $numArgs = $#ARGV + 1;
if ($numArgs != 1)
{
  print "Please enter one argument (path to directory containing photographs to stack)\n";
  exit(1);
}

my $targetPath=$ARGV[0];
unless (-d $targetPath)
{
  print "Directory does not exist. Please check the path.\n";
  exit(1);
}

chdir($targetPath) or die "Cannot chdir to $targetPath.\n";
print "Stacking photographs in $targetPath\n";
 
system("rename JPG jpg *"); 
# system("rename s/JPG/jpg/ *");    # Depending on what "rename" you have in your distribution

opendir(DIR, ".");
my @files = grep(/\.jpg$/,readdir(DIR));
closedir(DIR);

print "Converting files to TIFF\n";
my $file="";
foreach $file (@files) {
   #print "Converting $file to TIFF\n";
   system "convert $file z_$file.tif";
}

print "Creating masks\n";
system "align_image_stack -m -a mask z_*.tif";
system "rm -f z_*.tif"; 

my $exposureOption       ="wExposure=0"          ;
my $saturationOption     = "wSaturation=0"       ;
my $contrastOption       = "wContrast=1"         ;
my $maskOption           = "HardMask"            ;
my $contrastWindowOption = "ContrastWindowSize=9";
# my $exposureOption       ="exposure-weight=0"      ;  # Depending on your version of enfuse.
# my $saturationOption     = "saturation-weight=0"   ;  # The above should work with all versions.
# my $contrastOption       = "contrast-weight=1"     ;
# my $maskOption           = "hard-mask"             ;
# my $contrastWindowOption = "contrast-window-size=9";

system "enfuse -o result.tiff --$exposureOption --$saturationOption --$contrastOption --$maskOption --$contrastWindowOption mask*.tif";

system "rm -f mask*.tif";