显示 Vector<RECT> 数组中的值

Display Values inside Vector<RECT> array

我正在使用将点存储到 Vector 数据类型的代码,如下所示:

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

    detectBox->push_back(head);

这属于一个函数,该函数具有一个 for 循环,该循环将 "head" 的多个实例存储到 detectBox 中。这个函数是这样声明的:

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

其中 Img 只是一张普通的黑白图像,输入的是通过其他过程提取的头像。我现在的问题是如何查看存储在 detectBox 中的点?我想在 for 循环之外访问它们以用于其他用途。当我尝试打印出变量时,我只能返回地址(0000004FA8F6F31821 等)

此外,detectBox 在代码中是灰色阴影(不确定那是什么意思)。

完整代码可以在我与同一功能相关的其他问题中找到,此处:

C++ CvSeq Accessing arrays that are stored

-----编辑-----

尝试并关联的方法errors/Outputs:

第一个:

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

'&': illegal operation on bound member function expression

第二个:

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

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

第三:(注意,没有错误,但没有有用的信息输出)

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

Detect Box : 00000062BF0FF488

第四个:

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

Detect Box : 000000CB75CFF108

detectBox->push_back(head);
std::cout << &detectBox[1];

除非你有一个用于 RECT 的重载运算符“<<”(你可能没有),否则它会尝试将它匹配到最好的东西,这显然不会是什么你想要的。

试试更像

std::cout << "L: " << &detectBox[1].left << 
             "R: " << &detectBox[1].right << std::endl

我也担心你在使用 [1] 而你可能想要的是 ->back()

首先,detectBox 是指向数组的指针,因此在尝试对其进行索引之前需要取消引用它。

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

在打印出索引 1 处的第二个 RECT 实例的地址之前包括“&”运算符的地址,您不需要这样做,因为索引运算符为您提供了参考。

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

如果 RECT 实例没有 operator<< 将其输出到流,您将需要直接访问成员。以下应该有效:

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

这可以通过保存对您首先尝试获取的 RECT 的引用然后使用它来改进:

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

最后,我注意到你把新的 RECT 推到 Vector 的后面,但是你总是输出 vector 中的第二个 RECT。假设你想打印刚刚添加的 RECT,你可以输出 head 局部变量或在向量上使用 back() 方法,如下所示:

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