使用C ++中Boost的GIL在图像中定位图像

时间:2017-03-10 16:11:02

标签: c++ boost png gil boost-gil

我试图弄清楚如何使用Boost库中的GIL在C ++中新建图像中定位图像。

#define png_infopp_NULL (png_infopp)NULL
#define int_p_NULL (int*)NULL
#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/png_dynamic_io.hpp>
using namespace boost::gil;
int main()
{
    rgb8_image_t img(512, 512);
    rgb8_image_t img1;
    rgb8_image_t img2;
    png_read_image("img1.png", img1);//Code for loading an image
    png_read_image("img2.png", img2); //Code for loading 2nd image "img2.png" 
    //loading position of the images to an array or some kind of variable
    //passing in images and postions to the function to apply changes on newly created image with the  size of 512, 512 
    png_write_view("output.png", const_view(img)); //saving changes as "output.png"
}

image of what i want to do

2 个答案:

答案 0 :(得分:2)

您可以使用subimage_view定位图片,然后使用copy_pixels复制图片 您需要注意输入图像和输出子视图的大小是否匹配。如果它们不匹配,您也可以使用resize_view 这样的事情:

rgb8_image_t img1;
jpeg_read_image("img1.jpg", img1);
rgb8_image_t img2;
jpeg_read_image("img2.jpg", img2);

rgb8_image_t out_img(512, 512);
copy_pixels (view(img1), subimage_view(view(out_img), x, y, width, height));
copy_pixels (view(img2), subimage_view(view(out_img), x, y, width, height));
png_write_view("output.png", const_view(out_img));

答案 1 :(得分:0)

如果有人好奇,这是解决方案。

How to install Boost

How to install LibPng (需要加载png)

#define _CRT_SECURE_NO_DEPRECATE
#define _SCL_SECURE_NO_WARNINGS
#define png_infopp_NULL (png_infopp)NULL
#define int_p_NULL (int*)NULL
#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/png_dynamic_io.hpp>
using namespace boost::gil;
int main()
{
    rgb8_image_t out_img(512, 512);
    rgb8_image_t img1;
    rgb8_image_t img2;
    png_read_image("img1.png", img1);//Code for loading img1
    png_read_image("img2.png", img2);//Code for loading img2
    copy_pixels(view(img1), subimage_view(view(out_img), 0, 0, 50, 50)); 
    copy_pixels(view(img2), subimage_view(view(out_img), 462, 462, 50, 50));
    png_write_view("output.png", const_view(out_img));

}

所有这些#define都需要阻止visual studio出现错误。

Btw在程序目录中必须有img1.png和img2.png,否则会出现内存错误。