#!/bin/bash
INPUTDIR=$1
OCCLUSIONDIR=$2
TYPE=$3
TRIAL=$4
SEED=$4
NUMTRIALS=$5
case $TYPE in
"motion")
OUTPUTDIR=$MOTIONOUTPUTDIR
MAPDIR=$MOTIONMAPDIR
minOcclusionScale="0"
maxOcclusionScale="0.05"
;;
"occlusion")
OUTPUTDIR=$OCCLOUTPUTDIR
MAPDIR=$OCCLUSIONMAPDIR
minOcclusionScale="0.3"
maxOcclusionScale="1"
;;
*)
echo "Unknown Type $TYPE, exiting"
exit
;;
esac
LENGTH=${#NUMTRIALS}
minToleratedStandardDev=5
# list of animated sequences
SEQUENCES=($(ls $INPUTDIR))
numTimeSteps=4
# dimensions (pixel) of input
width=1024
height=436
# dimensions (pixel) of output (per timestep)
xPatchSize=32
yPatchSize=32
## Help Functions
## random number generation
# Parameters: 1: return variable, 2: lower boundary, 3: upper boundary
function getRandomNumber {
local AWKSCRIPT=" { srand(${SEED}); print rand() } "
local rnd=`echo | awk "${AWKSCRIPT}"`
let SEED+=1
local lower=${2}
local upper=${3}
local ret
ret=$(awk "BEGIN{print ${rnd} * 1000000}")
ret=${ret%.*}
ret=$(awk "BEGIN{print ${ret}%(${upper}-(${lower}))+${lower}}")
eval "$1=${ret}" # return random number
}
getRandomNumber SEED 0 999999
numOcclusionPixels=-1
standardDev=0
minToleratedOcclusionPixels=$(awk "BEGIN{print int(${xPatchSize}*${yPatchSize}*(${numTimeSteps}- 1)*${minOcclusionScale})}")
maxToleratedOcclusionPixels=$(awk "BEGIN{print int(${xPatchSize}*${yPatchSize}*(${numTimeSteps}- 1)*${maxOcclusionScale})}")
# create trial with occlusion/motion happening, check for patches with too low standard Dev
while [ $standardDev -lt $minToleratedStandardDev ]
do
# choose a random animated sequence
getRandomNumber rand 1 ${#SEQUENCES[@]}
let rand=$rand-1
sequence=${SEQUENCES[$rand]}
occlusionDir=${OCCLUSIONDIR}/$sequence
# calculate last possible frame (numframes - numTimeSteps + 2) since occlusionmaps have always 1 frame less than actual movie frames
frames=($(ls $occlusionDir))
numFrames=${#frames[@]}
let lastPossibleFrame=$numFrames-$numTimeSteps+2
# get random starting frame
getRandomNumber startFrameNr 1 $lastPossibleFrame
let startFrameNr=$startFrameNr-1
# get random coordinates
let xMax=$width-$xPatchSize-1
let yMax=$height-$yPatchSize-1
getRandomNumber xOffset 0 $xMax
getRandomNumber yOffset 0 $yMax
usedframes=""
for i in `seq 0 $(awk "BEGIN{print $numTimeSteps - 2}")`; do
let frameNr=$i+$startFrameNr
usedframes="${usedframes} $occlusionDir/${frames[$frameNr]}"
done
trialNrLz="$(printf "%0${LENGTH}d" ${TRIAL})"
fileName="$trialNrLz.png"
outputFile="$MAPDIR/${fileName}"
convert $usedframes -crop "${xPatchSize}x${yPatchSize}+${xOffset}+${yOffset}" +append $outputFile
numOcclusionPixels=$(convert $outputFile txt:- | grep "#FFFFFF" | wc -l)
convert $outputFile -resize 48x16 ${outputFile}
# check if occluding pixels are in bounds for occlusion/motion trial
if [ $numOcclusionPixels -lt $minToleratedOcclusionPixels ] || [ $numOcclusionPixels -gt $maxToleratedOcclusionPixels ]
then
continue;
fi
# create trial to that occlusion
usedframes=""
sequenceDir=$INPUTDIR/$sequence
for i in `seq 0 $(awk "BEGIN{print $numTimeSteps - 1}")`; do
let frameNr=$i+$startFrameNr
usedframes="${usedframes} $sequenceDir/${frames[$frameNr]}"
done
outputFile="$OUTPUTDIR/${fileName}"
convert $usedframes -crop "${xPatchSize}x${yPatchSize}+${xOffset}+${yOffset}" +append $outputFile
convert $outputFile -resize 64x16 ${outputFile}
standardDev=$(identify -verbose $outputFile | grep "standard deviation" | cut -d":" -f2 | cut -d" " -f2)
standardDev=$(awk "BEGIN{print int($standardDev)}")
done
printf "%s, %d, %d, %s, %d\n" $trialNrLz $xOffset $yOffset $sequence $startFrameNr > "parallelres/stdout$trialNrLz"