Is it possible to get histogram as array inside program? (magick++ or magickcore)
Thanks
histogram as array
Re: histogram as array
I used the following solution to get the histogram information of an image:
The result is given in the format:convert file.png histogram:- | identify -format %c -
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.844467: (255,255,255) #FFFFFF white
96333: (255, 0, 0) #FF0000 red
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: histogram as array
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.
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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: histogram as array
How to get color histogram data through API interface? There is only "GetImageHistogram" function.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: histogram as array
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.
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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: histogram as array
If you cannot find the needed code, then you can script it as follows:nicolas1 wrote:How to get color histogram data through API interface? There is only "GetImageHistogram" function.
#!/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