如何在 C++ BOOST 中加载像图形一样的 TIFF 图像
How to load a TIFF image like a graph in C++ BOOST
我想在 boost C++ 中加载一个 tiff 图像(带有带浮点值的像素的 GEOTIFF)(我是 C++ 的新手)。我的目标是使用从源 A 到目标 B 的双向 Dijkstra 以获得更高的性能。
Boost:GIL load tiif images:
std::string filename( "raster_clip.tif" );
rgb8_image_t img;
read_image( filename, img, tiff_tag() );
但是如何转换为Boost图呢?我正在阅读文档并寻找示例,但我还没有能够实现它。
我发现的类似问题和例子:
Shortest path graph algorithm help Boost;
http://www.geeksforgeeks.org/shortest-path-for-directed-acyclic-graphs/
我目前正在使用 scikit-image 库并使用 skimage.graph.route_through_array 函数在 python 中加载带有数组的图形。我使用 GDAL 通过加载图像获取数组,如本例中@ustroetz 所建议的 Here:
raster = gdal.Open("raster.tiff")
band = raster.GetRasterBand(1)
array = band.ReadAsArray()
TIFF 示例(上传后已转换为 PNG)为:
好的,请阅读 PNG:
I've cropped the whitespace border since it wasn't consistent anyways
读取和采样图像
using Img = boost::gil::rgb8_image_t; // gray8_image_t;
using Px = Img::value_type;
Img img;
//boost::gil::png_read_image("graph.png", img);
boost::gil::png_read_and_convert_image("graph.png", img);
auto vw = view(img);
接下来,确保我们知道尺寸以及如何处理每个单元格的中心像素:
double constexpr cell_w = 30.409;
double constexpr cell_h = 30.375;
auto pixel_sample = [=](boost::array<size_t, 2> xy) -> auto& {
return vw((xy[0]+.5)*cell_w, (xy[1]+.5)*cell_h);
};
auto const w= static_cast<size_t>(img.dimensions()[0] / cell_w);
auto const h= static_cast<size_t>(img.dimensions()[1] / cell_h);
构建图表
现在让我们制作图表。对于此任务,网格图似乎是有序的。它应该是 w×h
而不是在边缘环绕(如果应该,将 false
更改为 true
):
using Graph = boost::grid_graph<2>;
Graph graph({{w,h}}, false);
我们想在每条边上附加权重。我们可以使用预先调整大小的老式外部 属性 地图:
std::vector<double> weight_v(num_edges(graph));
auto weights = boost::make_safe_iterator_property_map(weight_v.begin(), weight_v.size(), get(boost::edge_index, graph));
或者,我们可以使用动态分配和增长的 属性-map:
auto weights = boost::make_vector_property_map<float>(get(boost::edge_index, graph));
作为奖励,下面是使用关联 属性-map 的等效方法:
std::map<Graph::edge_descriptor, double> weight_m;
auto weights = boost::make_assoc_property_map(weight_m);
其中每一个都是直接兼容的,选择权在您手中。
填充图表
我们简单地迭代所有边,根据颜色差异设置成本:
BGL_FORALL_EDGES(e, graph, Graph) {
auto& from = pixel_sample(e.first);
auto& to = pixel_sample(e.second);
// compare RED channels only
auto cost = std::abs(from[0] - to[0]);
put(weights, e, cost);
}
Note Consider normalizing weight to e.g. [0.0, 1.0)
using the actual bit-depth of the source image
让我们创建一个验证 TIF,这样我们就可以实际看到样本在图像中的拍摄位置:
{
BGL_FORALL_VERTICES(v, graph, Graph) {
pixel_sample(v) = Px(255, 0, 123); // mark the center pixels so we can verify the sampling
}
boost::gil::tiff_write_view("/tmp/verification.tif", const_view(img));
}
verification.tif
最终像(注意每个单元格的中心像素):
奖励:可视化网格图
让我们将其写入 Graphviz 文件:
{
auto calc_color = [&](size_t v) {
std::ostringstream oss;
oss << std::hex << std::noshowbase << std::setfill('0');
auto const& from = pixel_sample(vertex(v, graph));
oss << "#" << std::setw(2) << static_cast<int>(from[0])
<< std::setw(2) << static_cast<int>(from[1])
<< std::setw(2) << static_cast<int>(from[2]);
return oss.str();
};
write_dot_file(graph, weights, calc_color);
}
这从相同的样本像素计算颜色并使用一些 Graphviz 特定的魔法写入文件:
template <typename Graph, typename Weights, typename ColorFunction>
void write_dot_file(Graph const& graph, Weights const& weights, ColorFunction calc_color) {
boost::dynamic_properties dp;
dp.property("node_id", get(boost::vertex_index, graph));
dp.property("fillcolor", boost::make_transform_value_property_map(calc_color, get(boost::vertex_index, graph)));
dp.property("style", boost::make_static_property_map<typename Graph::vertex_descriptor>(std::string("filled")));
std::ofstream ofs("grid.dot");
auto vpw = boost::dynamic_vertex_properties_writer { dp, "node_id" };
auto epw = boost::make_label_writer(weights);
auto gpw = boost::make_graph_attributes_writer(
std::map<std::string, std::string> { },
std::map<std::string, std::string> { {"shape", "rect"} },
std::map<std::string, std::string> { }
);
boost::write_graphviz(ofs, graph, vpw, epw, gpw);
}
这导致 a grid.dot
file like this。
接下来,让我们布局using neato
:
neato -T png grid.dot -o grid.png
结果是:
完整代码清单
#include <boost/gil/extension/io/png_dynamic_io.hpp>
#include <boost/gil/extension/io/tiff_dynamic_io.hpp>
#include <boost/graph/grid_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/graphviz.hpp>
#include <iostream>
template <typename Graph, typename Weights, typename ColorFunction>
void write_dot_file(Graph const& graph, Weights const& weights, ColorFunction);
int main() try {
using Img = boost::gil::rgb8_image_t; // gray8_image_t;
using Px = Img::value_type;
Img img;
//boost::gil::png_read_image("/home/sehe/graph.png", img);
boost::gil::png_read_and_convert_image("/home/sehe/graph.png", img);
auto vw = view(img);
double constexpr cell_w = 30.409;
double constexpr cell_h = 30.375;
auto pixel_sample = [=](boost::array<size_t, 2> xy) -> auto& {
return vw((xy[0]+.5)*cell_w, (xy[1]+.5)*cell_h);
};
auto const w= static_cast<size_t>(img.dimensions()[0] / cell_w);
auto const h= static_cast<size_t>(img.dimensions()[1] / cell_h);
using Graph = boost::grid_graph<2>;
Graph graph({{w,h}}, false);
#if 0 // dynamic weight map
auto weights = boost::make_vector_property_map<float>(get(boost::edge_index, graph));
std::cout << "Edges: " << (weights.storage_end() - weights.storage_begin()) << "\n";
#elif 1 // fixed vector weight map
std::vector<double> weight_v(num_edges(graph));
auto weights = boost::make_safe_iterator_property_map(weight_v.begin(), weight_v.size(), get(boost::edge_index, graph));
#else // associative weight map
std::map<Graph::edge_descriptor, double> weight_m;
auto weights = boost::make_assoc_property_map(weight_m);
#endif
auto debug_vertex = [] (auto& v) -> auto& { return std::cout << "{" << v[0] << "," << v[1] << "}"; };
auto debug_edge = [&](auto& e) -> auto& { debug_vertex(e.first) << " -> "; return debug_vertex(e.second); };
BGL_FORALL_EDGES(e, graph, Graph) {
//debug_edge(e) << "\n";
auto& from = pixel_sample(e.first);
auto& to = pixel_sample(e.second);
// compare RED channels only
auto cost = std::abs(from[0] - to[0]);
put(weights, e, cost);
}
{
auto calc_color = [&](size_t v) {
std::ostringstream oss;
oss << std::hex << std::noshowbase << std::setfill('0');
auto const& from = pixel_sample(vertex(v, graph));
oss << "#" << std::setw(2) << static_cast<int>(from[0])
<< std::setw(2) << static_cast<int>(from[1])
<< std::setw(2) << static_cast<int>(from[2]);
return oss.str();
};
write_dot_file(graph, weights, calc_color);
}
{
BGL_FORALL_VERTICES(v, graph, Graph) {
pixel_sample(v) = Px(255, 0, 123); // mark the center pixels so we can verify the sampling
}
boost::gil::tiff_write_view("/tmp/verification.tif", const_view(img));
}
} catch(std::exception const& e) {
std::cout << "Exception occured: " << e.what() << "\n";
}
template <typename Graph, typename Weights, typename ColorFunction>
void write_dot_file(Graph const& graph, Weights const& weights, ColorFunction calc_color) {
boost::dynamic_properties dp;
dp.property("node_id", get(boost::vertex_index, graph));
dp.property("fillcolor", boost::make_transform_value_property_map(calc_color, get(boost::vertex_index, graph)));
dp.property("style", boost::make_static_property_map<typename Graph::vertex_descriptor>(std::string("filled")));
std::ofstream ofs("grid.dot");
auto vpw = boost::dynamic_vertex_properties_writer { dp, "node_id" };
auto epw = boost::make_label_writer(weights);
auto gpw = boost::make_graph_attributes_writer(
std::map<std::string, std::string> { },
std::map<std::string, std::string> { {"shape", "rect"} },
std::map<std::string, std::string> { }
);
boost::write_graphviz(ofs, graph, vpw, epw, gpw);
}
我想在 boost C++ 中加载一个 tiff 图像(带有带浮点值的像素的 GEOTIFF)(我是 C++ 的新手)。我的目标是使用从源 A 到目标 B 的双向 Dijkstra 以获得更高的性能。
Boost:GIL load tiif images:
std::string filename( "raster_clip.tif" );
rgb8_image_t img;
read_image( filename, img, tiff_tag() );
但是如何转换为Boost图呢?我正在阅读文档并寻找示例,但我还没有能够实现它。
我发现的类似问题和例子:
Shortest path graph algorithm help Boost;
http://www.geeksforgeeks.org/shortest-path-for-directed-acyclic-graphs/
我目前正在使用 scikit-image 库并使用 skimage.graph.route_through_array 函数在 python 中加载带有数组的图形。我使用 GDAL 通过加载图像获取数组,如本例中@ustroetz 所建议的 Here:
raster = gdal.Open("raster.tiff")
band = raster.GetRasterBand(1)
array = band.ReadAsArray()
TIFF 示例(上传后已转换为 PNG)为:
好的,请阅读 PNG:
I've cropped the whitespace border since it wasn't consistent anyways
读取和采样图像
using Img = boost::gil::rgb8_image_t; // gray8_image_t;
using Px = Img::value_type;
Img img;
//boost::gil::png_read_image("graph.png", img);
boost::gil::png_read_and_convert_image("graph.png", img);
auto vw = view(img);
接下来,确保我们知道尺寸以及如何处理每个单元格的中心像素:
double constexpr cell_w = 30.409;
double constexpr cell_h = 30.375;
auto pixel_sample = [=](boost::array<size_t, 2> xy) -> auto& {
return vw((xy[0]+.5)*cell_w, (xy[1]+.5)*cell_h);
};
auto const w= static_cast<size_t>(img.dimensions()[0] / cell_w);
auto const h= static_cast<size_t>(img.dimensions()[1] / cell_h);
构建图表
现在让我们制作图表。对于此任务,网格图似乎是有序的。它应该是 w×h
而不是在边缘环绕(如果应该,将 false
更改为 true
):
using Graph = boost::grid_graph<2>;
Graph graph({{w,h}}, false);
我们想在每条边上附加权重。我们可以使用预先调整大小的老式外部 属性 地图:
std::vector<double> weight_v(num_edges(graph));
auto weights = boost::make_safe_iterator_property_map(weight_v.begin(), weight_v.size(), get(boost::edge_index, graph));
或者,我们可以使用动态分配和增长的 属性-map:
auto weights = boost::make_vector_property_map<float>(get(boost::edge_index, graph));
作为奖励,下面是使用关联 属性-map 的等效方法:
std::map<Graph::edge_descriptor, double> weight_m;
auto weights = boost::make_assoc_property_map(weight_m);
其中每一个都是直接兼容的,选择权在您手中。
填充图表
我们简单地迭代所有边,根据颜色差异设置成本:
BGL_FORALL_EDGES(e, graph, Graph) {
auto& from = pixel_sample(e.first);
auto& to = pixel_sample(e.second);
// compare RED channels only
auto cost = std::abs(from[0] - to[0]);
put(weights, e, cost);
}
Note Consider normalizing weight to e.g.
[0.0, 1.0)
using the actual bit-depth of the source image
让我们创建一个验证 TIF,这样我们就可以实际看到样本在图像中的拍摄位置:
{
BGL_FORALL_VERTICES(v, graph, Graph) {
pixel_sample(v) = Px(255, 0, 123); // mark the center pixels so we can verify the sampling
}
boost::gil::tiff_write_view("/tmp/verification.tif", const_view(img));
}
verification.tif
最终像(注意每个单元格的中心像素):
奖励:可视化网格图
让我们将其写入 Graphviz 文件:
{
auto calc_color = [&](size_t v) {
std::ostringstream oss;
oss << std::hex << std::noshowbase << std::setfill('0');
auto const& from = pixel_sample(vertex(v, graph));
oss << "#" << std::setw(2) << static_cast<int>(from[0])
<< std::setw(2) << static_cast<int>(from[1])
<< std::setw(2) << static_cast<int>(from[2]);
return oss.str();
};
write_dot_file(graph, weights, calc_color);
}
这从相同的样本像素计算颜色并使用一些 Graphviz 特定的魔法写入文件:
template <typename Graph, typename Weights, typename ColorFunction>
void write_dot_file(Graph const& graph, Weights const& weights, ColorFunction calc_color) {
boost::dynamic_properties dp;
dp.property("node_id", get(boost::vertex_index, graph));
dp.property("fillcolor", boost::make_transform_value_property_map(calc_color, get(boost::vertex_index, graph)));
dp.property("style", boost::make_static_property_map<typename Graph::vertex_descriptor>(std::string("filled")));
std::ofstream ofs("grid.dot");
auto vpw = boost::dynamic_vertex_properties_writer { dp, "node_id" };
auto epw = boost::make_label_writer(weights);
auto gpw = boost::make_graph_attributes_writer(
std::map<std::string, std::string> { },
std::map<std::string, std::string> { {"shape", "rect"} },
std::map<std::string, std::string> { }
);
boost::write_graphviz(ofs, graph, vpw, epw, gpw);
}
这导致 a grid.dot
file like this。
接下来,让我们布局using neato
:
neato -T png grid.dot -o grid.png
结果是:
完整代码清单
#include <boost/gil/extension/io/png_dynamic_io.hpp>
#include <boost/gil/extension/io/tiff_dynamic_io.hpp>
#include <boost/graph/grid_graph.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/graphviz.hpp>
#include <iostream>
template <typename Graph, typename Weights, typename ColorFunction>
void write_dot_file(Graph const& graph, Weights const& weights, ColorFunction);
int main() try {
using Img = boost::gil::rgb8_image_t; // gray8_image_t;
using Px = Img::value_type;
Img img;
//boost::gil::png_read_image("/home/sehe/graph.png", img);
boost::gil::png_read_and_convert_image("/home/sehe/graph.png", img);
auto vw = view(img);
double constexpr cell_w = 30.409;
double constexpr cell_h = 30.375;
auto pixel_sample = [=](boost::array<size_t, 2> xy) -> auto& {
return vw((xy[0]+.5)*cell_w, (xy[1]+.5)*cell_h);
};
auto const w= static_cast<size_t>(img.dimensions()[0] / cell_w);
auto const h= static_cast<size_t>(img.dimensions()[1] / cell_h);
using Graph = boost::grid_graph<2>;
Graph graph({{w,h}}, false);
#if 0 // dynamic weight map
auto weights = boost::make_vector_property_map<float>(get(boost::edge_index, graph));
std::cout << "Edges: " << (weights.storage_end() - weights.storage_begin()) << "\n";
#elif 1 // fixed vector weight map
std::vector<double> weight_v(num_edges(graph));
auto weights = boost::make_safe_iterator_property_map(weight_v.begin(), weight_v.size(), get(boost::edge_index, graph));
#else // associative weight map
std::map<Graph::edge_descriptor, double> weight_m;
auto weights = boost::make_assoc_property_map(weight_m);
#endif
auto debug_vertex = [] (auto& v) -> auto& { return std::cout << "{" << v[0] << "," << v[1] << "}"; };
auto debug_edge = [&](auto& e) -> auto& { debug_vertex(e.first) << " -> "; return debug_vertex(e.second); };
BGL_FORALL_EDGES(e, graph, Graph) {
//debug_edge(e) << "\n";
auto& from = pixel_sample(e.first);
auto& to = pixel_sample(e.second);
// compare RED channels only
auto cost = std::abs(from[0] - to[0]);
put(weights, e, cost);
}
{
auto calc_color = [&](size_t v) {
std::ostringstream oss;
oss << std::hex << std::noshowbase << std::setfill('0');
auto const& from = pixel_sample(vertex(v, graph));
oss << "#" << std::setw(2) << static_cast<int>(from[0])
<< std::setw(2) << static_cast<int>(from[1])
<< std::setw(2) << static_cast<int>(from[2]);
return oss.str();
};
write_dot_file(graph, weights, calc_color);
}
{
BGL_FORALL_VERTICES(v, graph, Graph) {
pixel_sample(v) = Px(255, 0, 123); // mark the center pixels so we can verify the sampling
}
boost::gil::tiff_write_view("/tmp/verification.tif", const_view(img));
}
} catch(std::exception const& e) {
std::cout << "Exception occured: " << e.what() << "\n";
}
template <typename Graph, typename Weights, typename ColorFunction>
void write_dot_file(Graph const& graph, Weights const& weights, ColorFunction calc_color) {
boost::dynamic_properties dp;
dp.property("node_id", get(boost::vertex_index, graph));
dp.property("fillcolor", boost::make_transform_value_property_map(calc_color, get(boost::vertex_index, graph)));
dp.property("style", boost::make_static_property_map<typename Graph::vertex_descriptor>(std::string("filled")));
std::ofstream ofs("grid.dot");
auto vpw = boost::dynamic_vertex_properties_writer { dp, "node_id" };
auto epw = boost::make_label_writer(weights);
auto gpw = boost::make_graph_attributes_writer(
std::map<std::string, std::string> { },
std::map<std::string, std::string> { {"shape", "rect"} },
std::map<std::string, std::string> { }
);
boost::write_graphviz(ofs, graph, vpw, epw, gpw);
}