Page 1 of 1

histogram as array

Posted: 2008-10-17T06:12:32-07:00
by nicolas1
Is it possible to get histogram as array inside program? (magick++ or magickcore)

Thanks

Re: histogram as array

Posted: 2008-10-20T13:28:37-07:00
by strangeelement
I used the following solution to get the histogram information of an image:
convert file.png histogram:- | identify -format %c -
The result is given in the format:
844467: (255,255,255) #FFFFFF white
96333: (255, 0, 0) #FF0000 red
The source program is in PHP so I simply do an explode("\n", $histogram) on the output after calling it with exec() to get an array of the identified colors.

Re: histogram as array

Posted: 2008-10-21T23:28:32-07:00
by anthony
Unfortunately a list of the colors isn't the actual histogram data, IM is using internally.

The actual is not available from the command line, though it may be available from the API interfaces.

However by mapping the color values in the 1024 bins, one set for each RGB channel, you should get what IM is using internally.

Re: histogram as array

Posted: 2008-10-22T21:03:50-07:00
by nicolas1
How to get color histogram data through API interface? There is only "GetImageHistogram" function.

Re: histogram as array

Posted: 2008-10-22T21:11:39-07:00
by anthony
Sorry there I can't help much.

The best idea for that is to look at the function "ContrastStretchImage()" and "EqualizeImage" in magick/enhance.c

This uses the histogram of the image to modify the image basied on that histogram.

I have not gone into these function much myself, though as Fred and I are discussing these aspects of IM, I may end up looking at them myself soon.

Re: histogram as array

Posted: 2008-10-22T21:17:48-07:00
by fmw42
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

Re: histogram as array

Posted: 2008-10-23T04:47:15-07:00
by nicolas1
How can I use this code in C program...