我正在尝试读取 jpeg 文件,进行一些简单的转换并使用 boost::gil 库将其写出来。现在我被卡住了,甚至读取一个文件,提取非白点(它只包含红点和白点)并将它们写入另一个文件会产生完全意想不到的结果。我希望图片是相同的,但这就是发生的事情:
这就是我阅读非白色坐标的方式:
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 );
}
任何想法我做错了什么?
谢谢