gpt4 book ai didi

c++ - 如何使用Boost::GIL垂直翻转图像

转载 作者:行者123 更新时间:2023-12-02 10:15:49 25 4
gpt4 key购买 nike

我正在尝试生成图像图标。基于一些限制,我需要按{r,g,b,a,r1,g1,b1,a1,...}的顺序获取int32_t像素值的一维数组。在这种情况下,行的顺序应与OpenGL UV相同(从下到上)。为此,我使用了以下经过验证的方法:

const int image_width = image.width();
const int image_height = image.height();
if (!(image_width && image_height)) { return 1; }

int preview_width, preview_height;
if (image_width > image_height) {
preview_width = PREVIEW_SIZE;
preview_height = PREVIEW_SIZE * (float)image_height / (float)image_width;
}
else {
preview_width = PREVIEW_SIZE * (float)image_width / (float)image_height;
preview_height = PREVIEW_SIZE;
}

bg::rgba8_image_t preview_square(preview_width, preview_height);
const bg::rgba8_view_t& ps_viewer = bg::view(preview_square);
bg::resize_view(bg::const_view(image), ps_viewer, bg::bilinear_sampler());

std::vector<int32_t> arr(preview_width * preview_height);
memcpy(&arr[0], &ps_viewer[0], sizeof(int32_t) * arr.size());

根据GIL中像素的存储顺序,我需要沿y轴翻转图像。请告诉我如何实现?

enter image description here

最佳答案

您可以使用翻转 View 进行转换:

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

namespace bg = boost::gil;

int main() {
bg::rgba8_image_t v;
bg::read_and_convert_image("/tmp/VIplm.jpg", v, bg::jpeg_tag{});

using Pixel = bg::rgba8_pixel_t;
std::vector<Pixel> vec(v.width() * v.height());

auto dest = bg::interleaved_view(v.width(), v.height(), vec.data(), v.width() * sizeof(Pixel));
bg::copy_and_convert_pixels(
bg::flipped_left_right_view(bg::const_view(v)),
dest);

bg::write_view("/tmp/pixels.png", dest, bg::png_tag{});
}

png在这里:

enter image description here

我省去了调整大小的操作。

关于c++ - 如何使用Boost::GIL垂直翻转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61991470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com