Skip to content

Instantly share code, notes, and snippets.

@levhita
Last active October 30, 2015 01:29
Show Gist options
  • Save levhita/2d70f836b9da0a438c5e to your computer and use it in GitHub Desktop.
Save levhita/2d70f836b9da0a438c5e to your computer and use it in GitHub Desktop.
Takes all jpgs from the source folder and copies only the jpgs that present a significant difference, used to remove repeated frames from webcams output.
#/bin/bash
#We only allow empty destination folders.
if [ -d "$2" ]; then
echo "Directory already exists"
exit
fi
#If it doesn't exists create it.
if [ ! -d "$2" ]; then
mkdir $2
fi
directory="$2/"
#You can change this to react to smaller changes, but i have found this number to be very good.
threshold=5000
images=($(ls -1 $1/*.jpg))
i=1
last_image=${images[0]}
images_count=${#images[@]}
diffe=0
percent=0
while [ $i -lt $images_count ]
do
#We use compare from imagemagick with a little fuzz, 10% on fuzz and 5000 on threshold seems to work very good.
diffe=$(compare -metric AE -fuzz 10% $last_image ${images[i]} null: 2>&1)
percent=$(echo "scale=2; $i*100/$images_count" | bc)
echo "$i/$images_count($percent%)"
#We only process images that are significally different from the previous one.
if [ "$diffe" -gt "$threshold" ]; then
echo ${images[i]}
echo $diffe
#We use the last processed image as the comparition point
last_image=${images[i]}
cp $last_image $2
fi
(( i++ ))
done
@levhita
Copy link
Author

levhita commented Oct 30, 2015

Usage:
./movement.sh source destination

Created using examples from ImageMagick documentation here: http://www.imagemagick.org/Usage/compare/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment