c++ - 使用 boost::gil 编写 jpeg 文件

标签 c++ boost jpeg libjpeg boost-gil

我正在尝试读取 jpeg 文件,进行一些简单的转换并使用 boost::gil 库将其写出来。
现在我被卡住了,甚至读取一个文件,提取非白点(它只包含红点和白点)并将它们写入另一个文件会产生完全意想不到的结果。我希望图片是相同的,但这就是发生的事情:

这是输入图片。
Original Picture

这是输出图片:
enter image description here

这就是我阅读非白色坐标的方式:

vector<Point2D> points;
//load points from file
rgb8_image_t img;
jpeg_read_image(in_file_path, img );

assert( 2 == img._view.num_dimensions );
const auto dimensions = img._view.dimensions();
assert( 2 == dimensions.num_dimensions );

row_width = dimensions.x;
col_height = dimensions.y;

size_t white_cnt = 0;
size_t non_white_cnt = 0;
for ( uint32_t x = 0; x < dimensions.x; ++x ){
    for ( uint32_t y = 0; y < dimensions.y; ++y ){
        const rgb8_pixel_t& pix = const_view(img)( rgb8_image_t::point_t(x,y) );
        if ( !is_white(pix) ){
            points.push_back( Point2D {x,y} );
        }
    }
}

这就是写相同点的方式
rgb8_image_t img(row_width,col_heigth);

rgb8_pixel_t white(255,255,255);
rgb8_pixel_t red(255,0,0);
rgb8_pixel_t blue(0,0,255);

fill_pixels( view(img), white );

cout << "printing " << points.size() << "points" << std::endl;
for ( Point2D p : points ){
        const auto x = p[0];
        const auto y = p[1];
        cout << x <<"," << y << endl;
        view(img)( rgb8_image_t::point_t( x,y ) ) = red;
}

jpeg_write_view( out_file_path, const_view(img));

为了完整起见,这是 is_white 检查:
bool is_white( const boost::gil::rgb8_pixel_t& pix ){
    const auto r = boost::gil::at_c<0>(pix);
    const auto g = boost::gil::at_c<1>(pix);
    const auto b = boost::gil::at_c<2>(pix);
    constexpr auto MAX_VAL = std::numeric_limits<decltype(r)>::max();
    return ( MAX_VAL == r ) && ( MAX_VAL == g ) && ( MAX_VAL == b );
}

任何想法我做错了什么?

谢谢

最佳答案

由于源数据中的 JPEG 伪影,您有许多实际上不是红色的非白色像素:

enter image description here

您需要将颜色与阈值进行比较。我决定对于您当前的数据,在灰度 View 中检测非白度会更有效,因此整个程序有效地变为:

gil::transform_pixels(
        const_view(input),
        view(output),
        [](auto& pix) { return pix<200? blue : white; });

这是一个修改过的片段:
#include <boost/gil.hpp>
#include <boost/gil/color_convert.hpp>
#include <boost/gil/extension/io/jpeg.hpp>
#include <boost/gil/io/io.hpp>
#include <iostream>
namespace gil = boost::gil;

int main() {
    static const gil::rgb8_pixel_t
        white(255, 255, 255),
        red(255, 0, 0),
        blue(0, 0, 255);

    // load points from file
    gil::gray8_image_t input;
    read_and_convert_image("input.jpg", input,
            gil::image_read_settings<gil::jpeg_tag>{});

    auto dimensions = input._view.dimensions();
    gil::rgb8_image_t output(dimensions.x, dimensions.y, white);

    gil::transform_pixels(
            const_view(input),
            view(output),
            [](auto& pix) { return pix<200? blue : white; });

    write_view("output.jpg", const_view(output), gil::jpeg_tag());
}

笔记
  • 有几个简化
  • 我正在使用新的 IO 接口(interface)
  • 我在灰色 channel (200) 中选择了一个随机阈值来检测白度,这是我的第一个阈值,它似乎工作得很好,但你可能想为你的输入调整它
  • 我将输出像素颜色更改为蓝色以“证明”我不只是显示输入图像:)

  • 这是输入和输出交替:

    enter image description here

    关于c++ - 使用 boost::gil 编写 jpeg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61411952/

    相关文章:

    .net - 使用 .NET 创建带 JPEG 压缩的多页 TIFF

    c++ - 如何将 utf16 ushort 数组转换为 utf8 std::string?

    c++ - 将 unique_ptr 从一个集合移动到另一个集合

    c++ - 在模板化的嵌套和继承类中未检测到变量

    java - 无法在 Java 中读取 PIL 保存的 jpeg 字符串

    python - 如何从 PowerPoint 文件创建一组图像文件?

    c++ - 通过数组绘制对象

    c++ - Boost::Signals 的意义何在?

    c++ - 互斥作为类成员

    c++ - 使用私有(private)数据 boost 类的序列化