1

I am having some problems related to using boost::gil library when I try to resize an image view. I would like to access the pixels content of a image view after resizing it. However, I always obtain a segmentation fault when doing this. Here is my code:

boost::gil::rgb8_image_t rgb_image;

//Try to convert image file to raw binary data
try {     
    boost::gil::read_and_convert_image("/tmp/image.jpg", rgb_image, boost::gil::jpeg_tag());  
} catch (...) {
    return;
}

boost::gil::rgb8_view_t rgb_view;
boost::gil::rgb8_image_t rgb_image_resize(150, 200);
boost::gil::resize_view(boost::gil::const_view(rgb_image), boost::gil::view(rgb_image_resize), boost::gil::bilinear_sampler());
rgb_view = boost::gil::view(rgb_image_resize);

for (std::uint32_t i = 0; i < 150; i++) {
    boost::gil::rgb8_view_t::x_iterator it_image = rgb_view.row_begin(i);
    for (size_t width = 0; width < 200; width++) {
        if (it_image->at_c_dynamic(0) * 0.299 + it_image->at_c_dynamic(1) * 0.114 + it_image->at_c_dynamic(2) * 0.587 < 80) {//color filter. Trying to execute this line gives me a seg fault
            //do something
        } else { //white pixel
            //do something
        }
        it_image++;
    }
}

Could you please indicate me what is my problem? If I delete the resize_view function and create the view directly from the rgb_image, it works perfectly.

mloskot
  • 37,086
  • 11
  • 109
  • 136
Jasthro
  • 11
  • 1

1 Answers1

0

The line rgb_view.row_begin(i); gives you X-navigation iterator of i-th row which you can use to iterate over the i-th row pixels. But, you increment this iterator it_image++; in the nested loop, that is 150 * 200 times, what makes it go far beyond the end of the last pixel of the i-th row.

Here is example that shows one of possible ways to achieve the correct iteration:

#include <boost/gil.hpp>
#include <boost/gil/extension/io/jpeg.hpp>
#include <boost/gil/extension/numeric/resample.hpp>
#include <boost/gil/extension/numeric/sampler.hpp>

int main()
{
    namespace bg = boost::gil;

    bg::rgb8_image_t rgb_image;
    bg::read_and_convert_image("/tmp/example/test.jpg", rgb_image, bg::jpeg_tag());
    bg::rgb8_image_t rgb_image_resize(68, 49); // rgb_image is 136x98
    bg::resize_view(bg::const_view(rgb_image), bg::view(rgb_image_resize), bg::bilinear_sampler());
    auto rgb_view = bg::view(rgb_image_resize);

    for (int y = 0; y < rgb_view.height(); ++y)
    {
        bg::rgb8_view_t::x_iterator it_row = rgb_view.row_begin(y);
        for (int x = 0; x < rgb_view.width(); ++x)
        {
            auto p = it_row[x];
            if (p.at_c_dynamic(0) * 0.299 + p.at_c_dynamic(1) * 0.114 + p.at_c_dynamic(2) * 0.587 < 80)
                ; //do something
            else
                ; //do something
        }
    }
}

For more options, check the Boost.GIL tutorial with example computing the image gradient.

NOTE: The I/O extensions have been entirely rewritten in Boost.GIL released in Boost 1.68. The example above is based on GIL from Boost 1.68.

mloskot
  • 37,086
  • 11
  • 109
  • 136