0

I'm trying to teach myself opencv and c++ and this example program for face and eye detection includes the line:

for(size_t i = 0; i < faces.size(); i++)

I don't understand what faces.size() means, and following from that at what point i can be greater than faces.size().

How does it acquire a numerical value?

I see plenty of instances of faces throughout the rest of the program, but the only time I see size is as a parameter for face_cascade.detectMultiScale. It is capitalized though, which makes me think that it has nothing to do with faces.size().

Community
  • 1
  • 1
  • `faces` is certainly an instance of some type of container (i.e. `std::vector` or `std::map`) and `size` simply returns the number of elements it holds at that moment. If you're confused about containers I suggest you take a look @ http://en.cppreference.com/w/cpp/container – Captain Obvlious May 13 '16 at 22:48
  • `faces` is declared as `std::vector faces;`. I'm not sure how that could be anymore clear than it already is. – WhozCraig May 13 '16 at 22:54
  • 2
    OpenCV is not a great place to start learning C++. Start with C++ without any third party libraries and [a good book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Get the basics of C++ down and then start unraveling OpenCV – user4581301 May 13 '16 at 22:55
  • @sashoalm, then edit it! – Lamar Latrell May 13 '16 at 22:56
  • 1
    @WhozCraig, To someone learning that is as clear as mud - what are the colons for? what is std? what are the <> things for? Was that a declaration? What *is* a declaration? etc... – Lamar Latrell May 13 '16 at 22:58
  • 1
    That's a tricky one, @LamarLatrell . The underlying assumption we have to make here is that the questioner has a certain minimum knowledge; otherwise all answers would have to start with, "In the beginning..." As a result, the first few chapters of a C++ text needs to be off-limits except for specifically targeted questions that can be answered without having to first cover the first few chapters of a C++ text. – user4581301 May 13 '16 at 23:03
  • @user4581301, While I do agree it is (unfortunately for beginners) as clear as it'll get, it could be made *clearer* with assistance from stackoverflow users in a Q&A context. If they don't get loops, then vectors (as simple as they are) are aren't going to be easier (jargon overload at the least: 'containers', 'passed by reference', 'object' etc...). I think the real issue at heart here is exactly what you've pointed out (I was the first to +1 your comment) - *that openCV isn't a good place to learn C++*, it's a pity though as image-processing is such a good learning & project motivator! – Lamar Latrell May 13 '16 at 23:16
  • @LamarLatrell Those are part of the language syntax and have nothing explicitly to do with OpenCV. If you're having trouble understanding the language you need to put effort into learning it, not just OpenCV. I suggest you pick up a [great book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start learning. – Captain Obvlious May 13 '16 at 23:17
  • @CaptainObvlious, Right there with you... See my answer, see my comments above. – Lamar Latrell May 13 '16 at 23:18
  • 1
    I agree. I don't think throwing in _any_ framework into the mix is a good approach to learning C++. Too often your focus shifts from understanding and learning the language to solely learning the framework. This is bad as you end up missing **a lot** of stuff and your proficiency in the language suffers. If you are serious about learning the language drop the use of OpenCV, pick up a good book, and start learning. Otherwise any effort in educating you about the language is futile at best. – Captain Obvlious May 13 '16 at 23:19
  • Why the -1 on this question? So much worse out there! – Lamar Latrell May 13 '16 at 23:20
  • Not a downvoter, but the future use of this question is minimal. Part of the goal of SO is to create a repository for those that follow. Between the non-representative title making thiws hard to search for and the answer resolving to "Brush up on your C++ basics, then approach this problem again", there isn't much future use here. As for crap living on without being downvote-bombed into the underworld, what can I say? Stuff happens. – user4581301 May 13 '16 at 23:33
  • @LamarLatrell Good edit. Hope it gets the rest of the votes it needs for approval. – user4581301 May 13 '16 at 23:55

2 Answers2

2

faces is being populated here :

//-- Detect faces
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );

According to OpenCV documentation :

void cv::CascadeClassifier::detectMultiScale    (   InputArray      image,
        std::vector< Rect > &   objects,
        double      scaleFactor = 1.1,
        int     minNeighbors = 3,
        int     flags = 0,
        Size    minSize = Size(),
        Size    maxSize = Size() 
    )   

where std::vector< Rect > & objects (faces in your case) is a

Vector of rectangles where each rectangle contains the detected object, the rectangles may be partially outside the original image.

As you can see, objects is passed by reference to allow its modification inside the function.

Also std::vector<Type>::size() will give you the size of your vector, so, i<faces.size() is necessary to get the index i inside the bounds of the vector.

Vtik
  • 3,073
  • 2
  • 23
  • 38
2
faces.size()

Returns the size of 'faces', i.e. how many faces there are in 'faces'.

In general a basic for loop is structured like so:

for ( init; condition; increment )
{
   //your code...
}

It will run as long as the condition is true, i.e. as long as 'i' is less than faces.size() (which might be '10' or some other integer value).

'i' will get bigger as for each loop iteration 1 is added to it. This is managed by the i++ instruction.

I'd suggest if you're struggling with loop syntax that openCV might not be the best place to start learning C++ as a lot of the examples expect a level of competence higher than 'beginner' (intentionally and unintentionally via simple bad coding/lack of commenting etc.)

Lamar Latrell
  • 1,669
  • 13
  • 28