Boost logo

Boost Users :

Subject: Re: [Boost-users] Some problems about GIL
From: Christian Henning (chhenning_at_[hidden])
Date: 2008-11-16 15:14:13

Hi there,

> 1. Does any one know how to crop a image using GIL?

You can create a sub-image view of an existing image. Like this for instance:

// original image
gray8_image_t src( 10, 10 );

// cropped image
gray8_image_t dst( 4, 4 );

// coppy pixels
copy_pixels( subimage_view( view( src ), point_t( 2, 2 ), point_t( 4, 4 ))
           , view( dst ));

> 2. The IO extension seems not support to serialize a image binary data to
> memory, how to do that using GIL? (read png -> do some stuffs... -> write
> png to char* buffer not file.)

I'm not sure if I understand you correctly. To get access to the
image's memory buffer you can do the following:

gray8_image_t src( 10, 10 );
unsigned char* buf = &( *view( src ).begin() )[0];

But you have to be careful when it comes to planar and interleaved
images since the data is arranged differently in memory.

> 3. Is there any one good performance image comparison algorithm to compare
> two images by GIL? I am using ImageMagick to do that now. It try to compute
> Mean Absolute Error for two images, how this can be done by GIL?

I would assume the two images are compatible. If yes, you could do this:

template< typename View1, typename View2 >
bool cmp( const View1& v1, const View2& v2 )
    for( int y = 0; y < std::min( v1.height(), v2.height() ); ++y )
        const View1::x_iterator it1 = v1.row_begin( y );
        const View2::x_iterator it2 = v2.row_begin( y );

        for( int x = 0; x < std::min( v1.width(), v2.width() ); ++x )
            if( !static_equal( it1[x], it2[x] ))
                return false;

    return true;

The function has the advantage of not caring about the if an image is
planar or interleaved and also not caring about the layout of the
channels. For instance the following still works:

bgr8_image_t img1( 10, 10 );
rgb8_planar_image_t img2( 10, 10 );

assert( cmp( view( img1 ), view( img2 )));

There are still a lot of drawbacks here and I wouldn't recommend this
function in production code. But I hope it gives you the idea of how
to use gil. For instance, in case the channels are floating point data
you would want to something use something else than the ==operator.

Currently, there is no algorithm available to compute the Mean
Absolute Error. If you want you can take the challenge! ;-) Am right
in assuming that you would accumulate the differences of each channel
and then at the end take the mean?

> Thanks for your answer, I am try to adopt my old ImageMagick's code to GIL,
> I like elegant design of GIL but I am a newbie. Thanks for you patient to
> answer my questions. :)

Not a problem.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at