我想编写一个简单的程序以使用boost :: gil读取和编辑图像。读取图像功能总是发生错误。我会继续在Boost网站上找到教程。
#include <iostream>
#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/jpeg_io.hpp>
#include <boost/gil/extension/dynamic_image/any_image.hpp>
#include <boost/gil/channel.hpp>
#include <boost/mpl/vector.hpp>
int main(int argc, char *argv[])
{
namespace bg = boost::gil;
static const std::string resources_dir = "../resources";
static const std::string filename = "face.jpg";
typedef boost::mpl::vector<bg::gray8_image_t, bg::gray16_image_t, bg::rgb8_image_t, bg::rgb16_image_t> my_img_types;
bg::any_image<my_img_types> runtime_image;
try{
bg::jpeg_read_image(resources_dir + filename, runtime_image);
} catch (const std::ios_base::failure &e) {
std::cout << e.what() << std::endl;
}
return 0;
}
错误很长,不知道出什么问题了。似乎将不支持的图像类型传递到图像模板中,但是无论参数如何,它都会显示-我也尝试传递一种类型。
[ 50%] Building CXX object src/CMakeFiles/edges.dir/main.cpp.o
In file included from /usr/include/boost/config.hpp:57:0,
from /usr/include/boost/gil/gil_config.hpp:24,
from /usr/include/boost/gil/gil_all.hpp:25,
from /home/mateusz/projects/edges1/src/main.cpp:2:
/usr/include/boost/gil/extension/io/jpeg_io.hpp: In instantiation of ‘const bool boost::gil::jpeg_read_support<boost::gil::any_image_view<boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*> > >, boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*> > >, boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >*> > >, boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >*> > >, boost::mpl::vector0<mpl_::na>, 0>, 0>, 0>, 0> > >::is_supported’:
/usr/include/boost/gil/extension/io/jpeg_io.hpp:91:5: required from ‘void boost::gil::jpeg_read_image(const char*, Image&) [with Image = boost::gil::any_image<boost::mpl::vector<boost::gil::image<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >, false, std::allocator<unsigned char> >, boost::gil::image<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >, false, std::allocator<unsigned char> >, boost::gil::image<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >, false, std::allocator<unsigned char> >, boost::gil::image<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >, false, std::allocator<unsigned char> > > >]’
/usr/include/boost/gil/extension/io/jpeg_io.hpp:101:40: required from ‘void boost::gil::jpeg_read_image(const string&, Image&) [with Image = boost::gil::any_image<boost::mpl::vector<boost::gil::image<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >, false, std::allocator<unsigned char> >, boost::gil::image<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >, false, std::allocator<unsigned char> >, boost::gil::image<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >, false, std::allocator<unsigned char> >, boost::gil::image<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >, false, std::allocator<unsigned char> > > >; std::string = std::basic_string<char>]’
/home/mateusz/projects/edges1/src/main.cpp:18:64: required from here
/usr/include/boost/gil/extension/io/jpeg_io.hpp:40:5: error: invalid use of incomplete type ‘struct boost::gil::channel_type<boost::gil::any_image_view<boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*> > >, boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*> > >, boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >*> > >, boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >*> > >, boost::mpl::vector0<mpl_::na>, 0>, 0>, 0>, 0> > >’
BOOST_STATIC_CONSTANT(bool,is_supported=
^
In file included from /usr/include/boost/gil/algorithm.hpp:25:0,
from /usr/include/boost/gil/gil_all.hpp:27,
from /home/mateusz/projects/edges1/src/main.cpp:2:
/usr/include/boost/gil/gil_concept.hpp:49:30: error: declaration of ‘struct boost::gil::channel_type<boost::gil::any_image_view<boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*> > >, boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t> > >*> > >, boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<short unsigned int, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >*> > >, boost::mpl::v_item<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t> > >*> > >, boost::mpl::vector0<mpl_::na>, 0>, 0>, 0>, 0> > >’
template <typename T> struct channel_type;
我也是自己编写CMakeLists.txt的新手,所以我包括了这些文件。主文件夹中的CMakeLists:
cmake_minimum_required(VERSION 2.8)
project(edges1)
set(CMAKE_BUILD_TYPE "Debug")
add_subdirectory(src)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR})
src文件夹中的CMakeLists.txt:
set(SOURCES main.cpp)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR})
include_directories(${CMAKE_SOURCE_DIR}/include)
find_package(Boost REQUIRED COMPONENTS)
include_directories(${BOOST_INCLUDE_DIRS})
#link_directoires(${CMAKE_SOURCE_ROOT}/lib)
#link_directoires(${LIBRARY_OUTPUT_PATH})
add_executable(edges ${SOURCES})
使用read_and_convert_image会产生其他错误...
在src目录中安装了libjpeg,CMakeLists:
set(SOURCES main.cpp)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR})
include_directories(${CMAKE_SOURCE_DIR}/include)
add_executable(edges ${SOURCES})
find_package(Boost REQUIRED COMPONENTS)
IF(BOOST_FOUND)
target_link_libraries(edges Boost::boost)
ENDIF()
find_package(JPEG REQUIRED)
IF(JPEG_FOUND)
include_directories(${JPEG_INCLUDE_DIR})
target_link_libraries(edges ${JPEG_INCLUDE_DIR})
ENDIF()
cmake命令:
cmake -DJPEG_LIBRARY:PATH=/usr/lib/x86_64-linux-gnu/libjpeg.so --libdir=/usr/lib64 ..
制造后:
[ 50%] Linking CXX executable ../edges
CMakeFiles/edges.dir/main.cpp.o: In function `boost::gil::detail::jpeg_reader::init()':
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:89: undefined reference to `jpeg_std_error'
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:90: undefined reference to `jpeg_CreateDecompress'
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:91: undefined reference to `jpeg_stdio_src'
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:92: undefined reference to `jpeg_read_header'
CMakeFiles/edges.dir/main.cpp.o: In function `boost::gil::detail::jpeg_reader::~jpeg_reader()':
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:98: undefined reference to `jpeg_destroy_decompress'
CMakeFiles/edges.dir/main.cpp.o: In function `void boost::gil::detail::jpeg_reader::apply<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t>, boost::mpl::range_c<int, 0, 1> > >*> > > >(boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector1<boost::gil::gray_color_t>, boost::mpl::range_c<int, 0, 1> > >*> > > const&)':
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:102: undefined reference to `jpeg_start_decompress'
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:111: undefined reference to `jpeg_read_scanlines'
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:115: undefined reference to `jpeg_finish_decompress'
CMakeFiles/edges.dir/main.cpp.o: In function `void boost::gil::detail::jpeg_reader::apply<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t>, boost::mpl::range_c<int, 0, 3> > >*> > > >(boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t>, boost::mpl::range_c<int, 0, 3> > >*> > > const&)':
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:102: undefined reference to `jpeg_start_decompress'
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:111: undefined reference to `jpeg_read_scanlines'
/usr/include/boost/gil/extension/io/jpeg_io_private.hpp:115: undefined reference to `jpeg_finish_decompress'
jpeg仍然存在问题,但包括扩展名/ io的所有三个文件都不起作用:(
答案 0 :(得分:1)
您缺少附件
#include <boost/gil/extension/io/dynamic_io.hpp>
#include <boost/gil/extension/io/jpeg_dynamic_io.hpp>
然后编译并链接:
g++ -Wall -Wextra -pedantic -pthread -o sotest test.cpp -ljpeg
答案 1 :(得分:0)
@sehe提供的答案与Boost 1.67 或更早版本中发布的GIL完全正确。
由于 I / O扩展已在Boost 1.68中发布的Boost.GIL中被完全重写,因此您的示例将需要进行调整。在这里,它已更新为最低版本:
#include <boost/gil.hpp>
#include <boost/gil/extension/dynamic_image/any_image.hpp>
#include <boost/gil/extension/io/jpeg.hpp>
#include <boost/mpl/vector.hpp>
int main()
{
namespace bg = boost::gil;
using my_img_types = boost::mpl::vector<bg::gray8_image_t, bg::gray16_image_t, bg::rgb8_image_t, bg::rgb16_image_t>;
bg::any_image<my_img_types> runtime_image;
bg::read_image("file.jpg", runtime_image, bg::jpeg_tag());
}
值得注意的是,最新的Boost.GIL 现在需要兼容C ++ 11的编译器。