BOOST::GIL color_converted_view 不编译

BOOST::GIL color_converted_view doesn't compile

下面是麻烦的代码

#include <boost/gil.hpp>
#include <boost/gil/extension/io/jpeg.hpp>
#include <boost/gil/extension/io/png.hpp>

namespace gil = boost::gil;

int main() {
      gil::rgb8_image_t input_image;
      gil::read_image("box.jpg",input_image,gil::jpeg_tag{});
      gil::color_converted_view<gil::rgb8_view_t,gil::gray8_pixel_t>::type input_image_view=
                                   gil::color_converted_view<gil::gray8_pixel_t>(gil::view(input_image));
}

所以,我正在尝试将 rgb 图像转换为灰度图像。但是这段代码给出了以下错误

c++ -I /data/home/sayan/Boost_Library/boost_1_75_0 Fast_testing.cpp -L./ -ljpeg -lpng -ltiff                                                                           
In file included from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/planar_pixel_iterator.hpp:12,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/rgb.hpp:12,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/typedefs.hpp:16,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/channel_algorithm.hpp:13,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/color_convert.hpp:11,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/image_view_factory.hpp:11,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/algorithm.hpp:15,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil.hpp:12,
                 from Fast_testing.cpp:9:
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp: In instantiation of ‘boost::gil::memory_based_step_iterator<I>::memory_based_step_iterator(const boost::gil::memory_based_step_iterator<I2>&) [with I2 = boost::gil::dereference_iterator_adaptor<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*, boost::gil::color_convert_deref_fn<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >&, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >, boost::gil::default_color_converter> >; Iterator = boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*]’:
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/locator.hpp:262:99:   required from ‘boost::gil::memory_based_2d_locator<I>::memory_based_2d_locator(const boost::gil::memory_based_2d_locator<SI>&) [with X = boost::gil::memory_based_step_iterator<boost::gil::dereference_iterator_adaptor<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*, boost::gil::color_convert_deref_fn<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >&, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >, boost::gil::default_color_converter> > >; StepIterator = boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>]’
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/image_view.hpp:100:89:   required from ‘boost::gil::image_view<L>::image_view(const View&) [with View = boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::dereference_iterator_adaptor<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*, boost::gil::color_convert_deref_fn<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >&, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >, boost::gil::default_color_converter> > > > >; Loc = boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >]’
Fast_testing.cpp:137:113:   required from here
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:155:67: error: no matching function for call to ‘boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >::step_iterator_adaptor(const x_iterator&, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>)’
         : parent_t(it.base(), memunit_step_fn<Iterator>(it.step())) {}
                                                                   ^
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:50:5: note: candidate: ‘boost::gil::detail::step_iterator_adaptor<Derived, Iterator, SFn>::step_iterator_adaptor(const Iterator&, SFn) [with Derived = boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>; Iterator = boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*; SFn = boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>]’
     step_iterator_adaptor(const Iterator& it, SFn step_fn=SFn()) : parent_t(it), _step_fn(step_fn) {}
     ^~~~~~~~~~~~~~~~~~~~~
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:50:5: note:   no known conversion for argument 1 from ‘const x_iterator’ {aka ‘const boost::gil::dereference_iterator_adaptor<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*, boost::gil::color_convert_deref_fn<const boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >&, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >, boost::gil::default_color_converter> >’} to ‘boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >* const&’
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:49:5: note: candidate: ‘boost::gil::detail::step_iterator_adaptor<Derived, Iterator, SFn>::step_iterator_adaptor() [with Derived = boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>; Iterator = boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*; SFn = boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>]’
     step_iterator_adaptor() {}
     ^~~~~~~~~~~~~~~~~~~~~
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:49:5: note:   candidate expects 0 arguments, 2 provided
In file included from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/planar_pixel_iterator.hpp:12,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/rgb.hpp:12,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/typedefs.hpp:16,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/channel_algorithm.hpp:13,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/color_convert.hpp:11,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/image_view_factory.hpp:11,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/algorithm.hpp:15,
                 from /data/home/sayan/Boost_Library/boost_1_75_0/boost/gil.hpp:12,
                 from Fast_testing.cpp:9:
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:41:7: note: candidate: ‘constexpr boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >::step_iterator_adaptor(const boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >&)’
 class step_iterator_adaptor : public iterator_adaptor<Derived, Iterator, use_default, use_default, use_default, typename SFn::difference_type>
       ^~~~~~~~~~~~~~~~~~~~~
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:41:7: note:   candidate expects 1 argument, 2 provided
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:41:7: note: candidate: ‘constexpr boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >::step_iterator_adaptor(boost::gil::detail::step_iterator_adaptor<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*>, boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*, boost::gil::memunit_step_fn<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mp11::mp_list<boost::gil::gray_color_t> > >*> >&&)’
/data/home/sayan/Boost_Library/boost_1_75_0/boost/gil/step_iterator.hpp:41:7: note:   candidate expects 1 argument, 2 provided

但是当我将input_image_view的类型指定为auto时,没有出现这样的错误并且程序被正确执行。此外,在这种情况下,我获得了正确的输出。那么,为什么会出现此错误? 我用谷歌搜索了错误并发现了这个 link https://sourceforge.net/p/adobe-source/discussion/648138/thread/b4979028/。我尝试了那里的解决方案(不是复制和转换像素一)但仍然出现相同的错误。

我的编译器 (GCC10) 直接通知我您的类型 ID 错误:

using V = gil::color_converted_view<gil::rgb8_view_t, gil::gray8_pixel_t>::type;

给予

test.cpp|15 col 20| error: function template-id ‘color_converted_view<boost::gil::rgb8_view_t, boost::gil::gray8_pixel_t>’ in nested-name-specifier
||    15 |     using V = gil::color_converted_view<gil::rgb8_view_t, gil::gray8_pixel_t>::type;
||       |                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

所以,这就是你的问题。您可以拼出有效的结果类型:

using boost::mp11::mp_list;
using std::integral_constant;
boost::gil::image_view<gil::memory_based_2d_locator<
    gil::memory_based_step_iterator<gil::dereference_iterator_adaptor<
        gil::pixel<
            unsigned char,
            gil::layout<mp_list<gil::red_t, gil::green_t, gil::blue_t>,
                        mp_list<integral_constant<int, 0>,
                                integral_constant<int, 1>,
                                integral_constant<int, 2>>>>*,
        gil::color_convert_deref_fn<
            const gil::pixel<
                unsigned char,
                gil::layout<mp_list<gil::red_t, gil::green_t, gil::blue_t>,
                            mp_list<integral_constant<int, 0>,
                                    integral_constant<int, 1>,
                                    integral_constant<int, 2>>>>&,
            gil::pixel<unsigned char,
                       gil::layout<mp_list<gil::gray_color_t>,
                                   mp_list<integral_constant<int, 0>>>>,
            gil::default_color_converter>>>>>

WHich 确实有效,但由于显而易见的原因很笨重。所以,检查函数 docs/implementation 告诉我你想说:

gil::color_converted_view_type<
    std::remove_reference_t<decltype(gil::view(input_image))>,
    gil::gray8_pixel_t>::type input_image_view =
    gil::color_converted_view<gil::gray8_pixel_t>(gil::view(input_image));

或者,如果您确实知道视图是 rgb8_view_t,那就是:

gil::color_converted_view_type<gil::rgb8_view_t, gil::gray8_pixel_t>::type
    input_image_view = gil::color_converted_view<gil::gray8_pixel_t>(
        gil::view(input_image));

TL;DR

您打错了 color_converted_view 而不是 color_converted_view_type