View Full Version : ImageMagick GraphicsMagick and compare function

March 2nd, 2015, 01:06 PM
It had previously seemed to me like GraphicsMagick (https://en.wikipedia.org/wiki/GraphicsMagick) was a more freely licensed version of ImageMagick (https://en.wikipedia.org/wiki/ImageMagick).

Mumble, but, consulting wikipedia, it seems like both are pretty freely licensed now. As if sometime after I'd been tangentially involved in carefully installing GraphicsMagick instead of ImageMagick in environments to avoid licensing problems, the concern has gone away.

Bah. Of course, issues still do arise when the programs do or don't manipulate graphics formats which themselves are not free.

Anyway, I'd had a reason to see if simple command-line image recognition

If the source of your images is in-game screenshots, such simple analysis does seem to work relatively well for identifying "the type of screen depicted". So after having gathered lots of shots without classifying them I should be able to relatively easy pick shots of a type I want by using "compare" to evaluate them to a number indicative of their "closeness".

Using "compare" (imagemagick) or "gm compare" (graphicsmagick) I seem to get results corresponding to what I expect. Or at least, after a few samples I can deduce what I should use as a criterion for a "match".

Reading the wikipedia pages seems to suggest I would be better to go with GraphicsMagick (https://en.wikipedia.org/wiki/GraphicsMagick) rather than ImageMagick (https://en.wikipedia.org/wiki/ImageMagick). Because GraphicsMagick seems to me more concerned about a useful command-line/API. Bah. But the most natural shared environment for me to do my processing on seems to have ImageMagick but not GraphicsMagick.

It seems to me going with either "MAE" (Mean Absolute Error) or "RMSE" (Root Mean Squared Error) evaluation (seem to be supported by both packages) give recognizable results, in small test cases. But RMS is so traditional.

Anybody have other comments and suggestions.

This is purely a personal project, nothing to do with work.

March 9th, 2015, 11:36 AM
So the following script will take the first image on the command line, and output the names of subsequent images preceded by the RMSE evaluation. You can then sort that, and images similar to the first one will be at the top.

I need to try changing it to resize images to the base size if necessary. But that will add some complexity, and the comparision of resized images might not work so well? (although logically the ones closest to the original should still sort as earlier, even if the rmse value tends to be higher (because of details of border characteristics of images from different sources, etc).


compare=`which compare`
#echo $compare
if [[ "x$compare" = "x" ]]
echo "Cannot find compare".
exit 2


for i in "$@"
# No, really imagemagick really seems to send the output to stderr
rms=`sh -c "$compare -verbose -metric rmse $base $i /dev/null 2>&1 | awk '/all:/' | tail -1"`
# all: 23510.2 (0.358742)
rms=`echo "$rms" | awk '{print $NF}' | sed 's/[()]//g'`

if [[ "x$rms" = "x" ]]
# this is a size error

echo "$rms" "$i"