nicolas1 wrote:How to get color histogram data through API interface? There is only "GetImageHistogram" function.
If you cannot find the needed code, then you can script it as follows:
#!/bin/bash
# test image is IM built-in rose:
infile="rose:"
# set directory for temporary files
dir="." # suggestions are dir="." or dir="/tmp"
tmpA1="$dir/histbins_1_$$.mpc"
tmpA2="$dir/histbins_1_$$.cache"
tmpR1="$dir/histbins_R_$$.mpc"
tmpR2="$dir/histbins_R_$$.cache"
tmpG1="$dir/histbins_G_$$.mpc"
tmpG2="$dir/histbins_G_$$.cache"
tmpB1="$dir/histbins_B_$$.mpc"
tmpB2="$dir/histbins_B_$$.cache"
trap "rm -f $tmpA1 $tmpA2 $tmpR1 $tmpR2 $tmpG1 $tmpG2 $tmpB1 $tmpB2; exit 0" 0
trap "rm -f $tmpA1 $tmpA2 $tmpR1 $tmpR2 $tmpG1 $tmpG2 $tmpB1 $tmpB2; exit 1" 1 2 3 15
if convert -quiet -regard-warnings "$infile" +repage "$tmpA1"
then
: ' do nothing '
else
errMsg "--- FILE $infile DOES NOT EXIST OR IS NOT AN ORDINARY FILE, NOT READABLE OR HAG ZERO SIZE ---"
fi
# convert image to RGB
convert $tmpA1 -colorspace RGB -channel R -separate $tmpR1
convert $tmpA1 -colorspace RGB -channel G -separate $tmpG1
convert $tmpA1 -colorspace RGB -channel B -separate $tmpB1
getHistog()
{
img="$1"
# get lists of values and counts from 8-bit histogram of a single channel
# note that IM histograms are not well sorted
value=`convert $img -format %c -depth 8 histogram:info: | sort -k 2 -b | sed -n 's/^ *[0-9]*: [(]\([0-9 ]*\).*$/\1/ p'`
count=`convert $img -format %c -depth 8 histogram:info: | sort -k 2 -b | sed -n 's/^ *\([0-9]*\): [(].*$/\1/ p'`
# put value and count into arrays
valueArr=($value)
countArr=($count)
# check if both arrays are the same size
if [ ${#valueArr[*]} -ne ${#countArr[*]} ]
then
errMsg "--- ARRAY SIZES DO NOT MATCH ---"
exit 1
fi
echo "numbins=${#valueArr[*]}"
echo "value=${valueArr[*]}"
echo "count=${countArr[*]}"
}
echo ""
getHistog "$tmpR1"
echo ""
getHistog "$tmpG1"
echo ""
getHistog "$tmpB1"
echo ""
exit 0