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

Positioning an image within an image using GIL from Boost in C++

我正在尝试弄清楚如何使用 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

您可以只使用 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));

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

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 显示错误。

顺便说一句,程序目录中必须有img1.png和img2.png,否则会出现内存错误。