Search code examples
c++opencvvectorstructurepush-back

Display Values inside Vector<RECT> array


I am using a code that stores points into a Vector data type as follows:

    RECT head;
    head.left = pt1.x;
    head.right = pt2.x;
    head.top = pt1.y;
    head.bottom = pt2.y;

    detectBox->push_back(head);

This falls inside a function that has a for loop that stores multple instances of "head" into the detectBox. This function is delared like this:

void GetHeads(cv::Mat Img, vector<RECT>* detectBox)

Where Img is just a normal black and white image being fed in with the heads that have been extracted through other processes. My question now is how to I see the points that have been stored inside of detectBox? I would like to access them outside of the for loop to use for other things. When i try and print out the variables Ive only been able to have the addresses returned (0000004FA8F6F31821, etc)

Also, detectBox is a shade of grey in the code(not sure what that means).

Full code can be found in my other question related to the same function, here:

C++ CvSeq Accessing arrays that are stored

-----EDIT-----

Methods tried and associated errors/Outputs:

First:

std::cout << "Back :  " << &detectBox->back << std::endl;

'&': illegal operation on bound member function expression

Second:

std::cout << "Back :  " << detectBox->back << std::endl;

'std::vector>::back': non-standard syntax; use '&' to create a pointer to member

Third:(Note, No Error, but no useful information output)

    std::cout << "Detect Box :  " << detectBox << std::endl;

Detect Box : 00000062BF0FF488

Fourth:

std::cout << "Detect Box :  " << &detectBox[1] << std::endl;

Detect Box : 000000CB75CFF108


Solution

  • First, detectBox is a pointer to the array so it needs to be dereferenced before trying to index into it.

    std::cout << (*detectBox)[1];  // Outputs the 2nd RECT in the vector
                                   // assuming an operator<< is implemented
                                   // for RECT.
    

    Including the address of '&' operator before it will print out the address of the 2nd RECT instance at index 1 which you do not need to do because the index operator gives you the reference.

    std::cout << &(*detectBox)[1];
    

    Without an operator<< for the RECT instance to output it to the stream you will need to access the members directly. The following should work:

    std::cout << "Left: " << (*detectBox)[1].left;
    std::cout << "Right: " << (*detectBox)[1].right;
    std::cout << "Top: " << (*detectBox)[1].top;
    std::cout << "Bottom: " << (*detectBox)[1].bottom;
    

    This can be improved by saving off a reference to the RECT you are trying to get first and then using that:

    RECT& secondRect = (*detectBox)[1];
    std::cout << "Left: " << secondRect.left;
    std::cout << "Right: " << secondRect.right;
    std::cout << "Top: " << secondRect.top;
    std::cout << "Bottom: " << secondRect.bottom;
    

    Finally, I notice that you push the new RECT on the back of the Vector, but then you always output the 2nd RECT in the vector. Assuming you want to print the RECT you just added you could either output the head local variable or use the back() method on the vector as so:

    RECT& lastRect = detectBox->back();
    std::cout << "Left: " << lastRect.left;
    std::cout << "Right: " << lastRect.right;
    std::cout << "Top: " << lastRect.top;
    std::cout << "Bottom: " << lastRect.bottom;