Page 1 of 1

How to get the image brightness?

Posted: 2013-09-06T11:20:40-07:00
by swp
Hello,

I'm trying to determine the brightness of an image using the MagickWand programming interface. I managed to get a brightness value on the command line with the following command:
$ convert image.jpg -colorspace hsb -resize 1x1 txt:-
# ImageMagick pixel enumeration: 1,1,255,hsb
0,0: ( 55, 33,197) #3721C5 hsb(21.413%,12.9335%,77.1542%)
I am interested in the brightness value (i.e. 77.1542%), but I don't know how to get this value with the API. I have come this far:

Code: Select all

/* Use ImageMagick to get the image brightness */
int get_image_brightness(const char *path) {
    MagickBooleanType status;
    MagickWand *magick_wand;

    MagickWandGenesis();
    magick_wand = NewMagickWand();
    status = MagickReadImage(magick_wand, path);
    if (status == MagickFalse)
        return -1;
    MagickSetColorspace(magick_wand, HSBColorspace);
    MagickResizeImage(magick_wand, 1, 1, MitchellFilter, 1);

    /* TODO: Get brightness value here */

    magick_wand = DestroyMagickWand(magick_wand);
    MagickWandTerminus();

    return 0;
}
Which MagickWand function should I use to get that value?

I'm not sure if the above method to get the image brightness is good. If anyone knows a better method to get the brightness value, please tell me.

Re: How to get the image brightness?

Posted: 2013-09-08T08:21:33-07:00
by el_supremo
I haven't tried this and also haven't used HSB colour space, but I think this will extract what you want:

Code: Select all

	PixelWand *pw;
	float percent;
	int r,g,b;
	
	MagickGetImagePixelColor(magick_wand,0,0,pw);
	r = PixelGetRedQuantum(pw);
	g = PixelGetGreenQuantum(pw);
	b = PixelGetBluequantum(pw);
	percent = (float)b/MagickQuantumRange;
Pete

Re: How to get the image brightness?

Posted: 2013-09-08T13:14:43-07:00
by swp
Thank you very much Pete! Your code contained some typos, but after fixing those, I got the wrong value (it returned the blue color of the pixel wand, not the brightness). But your code example did point me to the right section of the documentation where I found the function PixelGetHSL(), which is just what I needed! Here is the result:

Code: Select all

/* Return image info for image `path` */
int get_image_brightness(const char *path) {
    MagickBooleanType status;
    MagickWand *magick_wand;
    PixelWand *pw;
    //size_t qrange = 0;
    //double percent;
    double hue, saturation, lightness;

    MagickWandGenesis();
    magick_wand = NewMagickWand();
    pw = NewPixelWand();
    status = MagickReadImage(magick_wand, path);
    if (status == MagickFalse)
        return -1;
    MagickSetColorspace(magick_wand, HSBColorspace);
    MagickResizeImage(magick_wand, 1, 1, MitchellFilter, 1);

    // Get the brightness value
    status = MagickGetImagePixelColor(magick_wand, 0, 0, pw);
    if (status == MagickFalse) {
        return -1;
    }
    PixelGetHSL(pw, &hue, &saturation, &lightness);
    //MagickGetQuantumRange(&qrange);
    //percent = lightness / (double)qrange;
    printf("brightness: %f\n", lightness);

    magick_wand = DestroyMagickWand(magick_wand);
    MagickWandTerminus();

    return 0;
}
It returns slightly different values than the `convert' command I used, but that's probably a result from using a different resize filter. I don't know which resize filter `convert' uses, but if anyone knows, please let me know. However I read somewhere in the forums that Mitchell is the default filter for shrinking.

Also I commented out the code for calculating the Quantum Range; I'm guessing it's not needed now.

If anyone knows a better way to calculate the brightness, please let me know.

Re: How to get the image brightness?

Posted: 2013-09-08T15:40:42-07:00
by fmw42
I believe that lanczos is used for shrinking and mitchell for enlarging. See http://www.imagemagick.org/Usage/filter/#default_filter