简体   繁体   中英

STL container for simple image segmentation

I need to segment an image, based on a simple rule (if value is in between 2 values). 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++)

I've stored my images as 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> . 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? I'm trying to figure it out through this flowchart . The best I can come up with was an unordered_multimap .

If you're moving elements to the end of the vector, use 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!

(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.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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