#! /bin/csh -f # # Bring the sample into focus # # set tempfile = /tmp/focus_$$ if(-w /tmp/$USER) set tempfile = /tmp/${USER}/focus_$$ set path = ( /programs/kuim/bin/ $path ) set step = 0.1 set edge = 100 set focusbox = "" foreach arg ( $* ) if ("$arg" == "beam") set around_beam set int = `echo $arg | awk '{print int($1)}'` if ("$arg" == "$int") then set edge = $int endif end if($?around_beam) then set corner = `beam.com | awk -v edge=$edge '{printf "+%d+%d", 352*$7-edge/2,240*$8-edge/2}'` set focusbox = ${edge}x${edge}$corner echo "selecting $edge x $edge area around $corner" endif echo -n >! ${tempfile}.scores snap.com ${tempfile}.jpg 352x240 -nodisplay -fast >& /dev/null if($?around_beam) then convert -crop $focusbox ${tempfile}.jpg ${tempfile}cropped.jpg mv ${tempfile}cropped.jpg ${tempfile}.jpg endif rm -f ${tempfile}.im >& /dev/null type -f ${tempfile}.jpg ${tempfile}.im >& /dev/null rm -f ${tempfile}.jpg ${tempfile}out.im >& /dev/null median -s 3 3 ${tempfile}.im ${tempfile}out.im >& /dev/null #laplacian ${tempfile}.im ${tempfile}out.im >& /dev/null mv ${tempfile}out.im ${tempfile}.im set score = `header ${tempfile}.im | awk '/^Var/{print $NF}'` rm -f ${tempfile}.im set bestscore = ( $score ) set pos = 0 set bestpos = ( $pos ) echo "$score $bestpos" | tee -a ${tempfile}.scores set steps = 0 while ($step != 0) @ steps = ( $steps + 1 ) sample.com ${step} 0 0 > /dev/null set pos = `echo $pos $step | awk '{print $1+$2}'` snap.com ${tempfile}.jpg 352x240 -nodisplay -fast >& /dev/null if($?around_beam) then convert -crop $focusbox ${tempfile}.jpg ${tempfile}cropped.jpg mv ${tempfile}cropped.jpg ${tempfile}.jpg endif rm -f ${tempfile}.im >& /dev/null type -f ${tempfile}.jpg ${tempfile}.im >& /dev/null rm -f ${tempfile}.jpg ${tempfile}out.im >& /dev/null median -s 3 3 ${tempfile}.im ${tempfile}out.im >& /dev/null #laplacian ${tempfile}.im ${tempfile}out.im >& /dev/null mv ${tempfile}out.im ${tempfile}.im set newscore = `header ${tempfile}.im | awk '/^Var/{print $NF}'` rm -f ${tempfile}.im set rightdirection = `echo $score $newscore | awk '{print ($2-$1)/(2*sqrt(($2-$1)^2))+0.5}'` set rightdirection = `echo $score $newscore | awk '{diff=sqrt(($2-$1)^2);dir=($2-$1)/diff} {print (diff )}'` set rightdirection = `echo $score $newscore $bestscore | awk '{print (($2-$1) > -0.02*$3)}'` if(! $rightdirection && $steps > 1) then # reverse what we just did? set step = `echo $step | awk '$1*$1<0.01{print 0;exit} {printf "%.2f", -0.61*$1}'` endif echo "$newscore $pos" | tee -a ${tempfile}.scores set score = $newscore if( `echo $score $bestscore | awk '{print ($1+0>$2+0)}'` ) then set bestscore = ( $score ) set bestpos = ( $pos ) endif end sort -n ${tempfile}.scores | tail -7 >! ${tempfile} mv ${tempfile} ${tempfile}.scores gnuplot -persist << EOF >&! ${tempfile}.fitlog c=$bestscore[1] b=$bestpos[1] a=-10000 fit a*(x-b)**2+c '${tempfile}.scores' using 2:1 via a fit a*(x-b)**2+c '${tempfile}.scores' using 2:1 via a,c fit a*(x-b)**2+c '${tempfile}.scores' using 2:1 via a,b,c plot '${tempfile}.scores' using 2:1 , a*(x-b)**2+c EOF rm -f fit.log set fitpos = `awk '$1=="b" && $2=="="{print $3}' ${tempfile}.fitlog | tail -1` set dist = `echo $fitpos $bestpos | awk '{printf "%d", 1000*sqrt(($1-$2)^2)}'` if ($dist < 100) then echo "using fitted position" set bestpos[1] = $fitpos endif # make sure we don't do any wild moves set dist = `echo $bestpos | awk '{printf "%d", 1000*sqrt(($1-$4)^2)}'` if($dist < 500) then set delta = `echo $bestpos $pos | awk '{print $1-$2}'` sample.com $delta endif rm -f ${tempfile}.scores rm -f ${tempfile}.fitlog