简体   繁体   English

STL容器用于简单的图像分割

[英]STL container for simple image segmentation

I need to segment an image, based on a simple rule (if value is in between 2 values). 我需要根据一个简单规则(如果值在2个值之间)对图像进行细分。 I'm using only STL containers (I'm not using opencv or other libraries because I want to keep this free of dependencies while teaching myself c++) 我只使用STL容器(我没有使用opencv或其他库,因为我想在教自己使用c ++时保持不受依赖)

I've stored my images as vector< vector<double> > . 我将图像存储为vector< vector<double> > My brute force approach is to iterate through my image using 2 iterators and check each value, and maybe push the indices of the values that satisfy my condition to another vector<int> . 我的蛮力方法是使用2个迭代器遍历我的图像并检查每个值,然后将满足我条件的值的索引推送到另一个vector<int> I'll have to do this until all segments are found. 在找到所有段之前,我必须这样做。 Every time I want to pick a segment I'll iterate through the stored indices. 每次我要选择一个细分时,我都会遍历存储的索引。

  1. What is the correct way to do this? 正确的方法是什么?

  2. Can this be achieved in one pass? 一口气可以做到吗?

  3. What is a suitable STL container for this process? 什么是适合此过程的STL容器? I'm trying to figure it out through this flowchart . 我正在尝试通过此流程图解决问题 The best I can come up with was an unordered_multimap . 我能想到的最好的是unordered_multimap

If you're moving elements to the end of the vector, use std::stable_partition . 如果要将元素移到向量的末尾,请使用std::stable_partition

std::vector<int> vec(20);
// 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

std::iota(begin(vec), end(vec), 0);
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 

std::stable_partition(begin(vec), end(vec),
                      [](const auto& e){ return (4 <= e && e < 12);});
// 4 5 6 7 8 9 10 11 0 1 2 3 12 13 14 15 16 17 18 19

Online example here . 在线示例在这里

This will also work if you store the data in a single vector - use iterators for the beginning and end of the column/row instead of the entire range. 如果将数据存储在单个向量中,这也将起作用-使用迭代器作为列/行的开始和结束而不是整个范围。 I've got no idea how you read 'across' the data sensibly with either a 1D or 2D vector though! 我不知道您是如何通过1D或2D向量明智地“跨”读取数据的!

(I wrote a very nice post about Sean Parent's gather algorithm before realising it doesn't answer the question. View the edit history if you want to move selected items to places other than the ends.) (我意识到关于Sean Parent的gather算法没有解决问题之前,写了一篇非常不错的文章。如果要将选定项目移动到末端以外的地方,请查看编辑历史记录。)

如果将图像分为两段,则可以将分割存储为vector<vector<bool>> ,其大小与图像相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM