Skip to content

Instantly share code, notes, and snippets.

@mmitou
Last active November 28, 2015 09:14
Show Gist options
  • Select an option

  • Save mmitou/31e59eee92e83a110f41 to your computer and use it in GitHub Desktop.

Select an option

Save mmitou/31e59eee92e83a110f41 to your computer and use it in GitHub Desktop.
画像の類似度を比較するプログラムとそのユーティリティ
#!/bin/bash
set -eu
if [ $# -ne 2 ]; then
echo "invalid arg" >&2
exit 1
fi
for file in $2/*
do
x=`./similarity.sh $1 $file`
echo $1 $file $x
done
#!/bin/bash
set -eu
if [ $# -ne 1 ]; then
echo "invalid arg" >&2
exit 1
fi
files=(${1}/*)
for (( i = 0; i < ${#files[@]}; ++i ))
do
for (( j = 0; j < ${#files[@]}; ++j ))
do
buf=`./similarity.sh ${files[$i]} ${files[$j]}`
echo ${files[$i]} ${files[$j]} ${buf}
done
done
#!/bin/sh
set -eu
help() {
echo "$0 returns similarity of two given images."
echo "they must be same width and height."
echo ""
echo "Usage:"
echo "$0 image_file0 image_file1"
exit 1
}
if [ $# -ne 2 ]; then
help
fi
# check images to have same width and hight.
w1=`identify -format "%w" $1`
h1=`identify -format "%h" $1`
w2=`identify -format "%w" $2`
h2=`identify -format "%h" $2`
if [ $w1 -ne $w2 -o $h1 -ne $h2 ]; then
help
fi
# generate tmporary file.
tmp1=$(mktemp "/tmp/${1##*/}.XXXXXX")
tmp2=$(mktemp "/tmp/${2##*/}.XXXXXX")
# when process finished, rm tmp files.
rm_tmps() {
[ -e ${tmp1} ] && rm ${tmp1}
[ -e ${tmp2} ] && rm ${tmp2}
}
trap rm_tmps INT TERM EXIT
# convert images to color histograms.
convert $1 -format '%c' histogram:info:- | sed 's/:.*#/ /' | awk '{print $2, $1}' > ${tmp1}
convert $2 -format '%c' histogram:info:- | sed 's/:.*#/ /' | awk '{print $2, $1}' > ${tmp2}
# summate minimums
sumofmins=`join ${tmp1} ${tmp2} | awk 'BEGIN{sum=0} {if($2 < $3) sum=sum+$2; else sum=sum+$3} END {print sum}'`
# normalize sumofmins
echo "scale=3; ${sumofmins} / (${w1} * ${h1})" | bc
#!/bin/sh
set -eu
if [ $# -ne 2 ]; then
echo "Usage:"
echo "${0} num_of_pics dest_dir"
exit 1
fi
[ ! -d $2 ] && mkdir ${2}
for i in `seq 0 ${1}`; do
fswebcam -r 640x480 --jpeg 85 -D 1 -S 35 --no-banner ${2}/`date +%Y-%m-%d-%H%M%S`.jpg
done
# -*- conding:utf-8 -*-
import sys
import re
table = {}
column = []
row = []
sr = re.compile( ' +' )
for line in open( sys.argv[1], 'r' ):
l, r, v = sr.split( line.strip() )
l = l.strip()
r = r.strip()
if not l in table:
table[ l ] = {}
table[ l ][ r ] = float( v )
column.append( l )
row.append( r )
column = sorted( list( set( column ) ) )
row = sorted( list( set( row ) ) )
for r in row:
print( ", ", r, end="")
print()
for c in column:
print( c, end="" )
for r in row:
print( ", ", table[ c ][ r ], end = "" )
print()
@mmitou

mmitou commented Nov 28, 2015

Copy link
Copy Markdown
Author

similarity.sh img1 img2 : img1とimg2の類似度を計算する。
take_pics.sh num destdir : webカメラでnum枚の画像を取り、destdirに保存する。
round_robin.sh dir: dirに保存してある全ての画像に対して総当りで類似度を計算する。
to_csv.py txt: round_robin.shの出力結果を保存したファイルtxtをcsvに変換する。
point_multi.sh img dir : imgとdirにある全てのファイルの類似度を比較する。

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