Vector to ints - ofxCv

#1

Hello!

My brain is a little foggy - I think I’m missing something possibly fundamental to approach my problem.

I have an vector of numbers coming in via contourFinder.size() - that vector is the amount of contours found with my camera.

Currently I have 7 - I now use number.push_back(contourFinder.getContourArea(i)); to get the areas of the of each contour / blob in that vector.

debug mode, I see that I have these values:
{ size=7 }
[capacity]: 9
[allocator]: allocator
[0]: 896
[1]: 1708
[2]: 554
[3]: 369
[4]: 8719
[5]: 918
[6]: 415

Here is where I am stuck - and maybe you can help?!
I would like to capture each value of each space in the vector in its own int, eg, int a = 896 - I’m happy / expecting the area to change - I’m hoping to use the vals to drive other functions…

I anticipate that the size of my initial vector will also update, getting bigger / smaller depending on what’s in the scene.

#2

I don’t think it’s clear what your question is.

std::array works for what you want, and you already seem to be using it. You can add and remove elements.

You can use:
int a = number[3];
if you want, and then pass “a” to functions, or skip a and just pass number[3], or number[i]…

Actually, you might want to check out this refresher on vectors in ofBook:

https://openframeworks.cc/ofBook/chapters/stl_vector.html

#3

Ace, thanks.

I did actually look over that doc and begin to play with int c = numbers[2]… So that’s good hear I was on the right track :slight_smile: .

OK, so now I have another question if I may…

When the program starts, and before I play around with any thresholds (ofxCv, findContour-basic example) I typically get 1x contour, I play with the settings, I may get 9.

However, my understanding is, that if I create 9x int h = numbers[8] etc, then I’ll get an out of bound error on start-up.

Can I dynamically create / delete and add ints from a vector should it get larger / smaller?

#4

Yes, you can dynamically add and remove values to/from a std::vector.

Each time you do, the vector’s length will change.

e.g.
numbers.push_back(5); // will add a new int with value 5 to the end of the vector, which will now contain one more int than it did before.

It will be an error if your code tries to access a value from a vector at an index where there currently is no value. So you just need to write your code so it doesn’t do that, for instance by making sure i < numbers.size() before trying to access numbers[i].