【问题标题】:How can i use a Vector of Struct containing vector<Mat>我如何使用包含 vector<Mat> 的结构向量
【发布时间】:2014-03-04 18:30:10
【问题描述】:

我试图声明一个结构,

struct Sample {
    int classLabel[2];
    vector<Mat> image; 
};

现在我想用它,我把它初始化为

Sample sample1;

并在sample1 中保存了 5 张图片,并创建了另一个 Sample sample2 并保存了另外 5 张图片。 现在我想将这两个样本保存在Sample 的向量中。我声明:

vector<Sample> samples;

现在当我尝试 push_back sample1 和 sample2 时

samples.push_back(sample1);
samples.push_back(sample2);

它什么也没给我。样本显示为samples[2],但不包含classLabel=??? 和image=???? 前面的问号

任何人都可以指导我在哪里做错了。我怎样才能使它可用。我的意思是在samples 中正确保存sample1 和sample2 结构。 会很感激。 问候

【问题讨论】:

    标签: c++ opencv vector struct


    【解决方案1】:
    vector<Sample> samples;
    samples.push_back(sample1);
    samples.push_back(sample2);
    

    在这之后,samples 的大小是 2。所以你只能访问 samples[0] 和 samples[1],而不是 samples[2]。


    正如@sammy 所评论的,Watch 中的变量可能在 VS 中显示不正确的值。

    要使其正常工作,您可能需要执行以下步骤:

    1. 检查应用程序及其依赖项(如果需要)是否在 debug 模式下构建。
    2. 确保禁用优化。编译时使用 /Od 选项。
    3. 结构成员对齐。当有一堆项目一起编译时,项目内部该字段的值不同可能会导致显示值无效。在所有项目上重置为 默认 值。该值位于 Configuration Properties-> C/C++ ->Code Generation -> Struct Member Alignment 下。

    查看here 了解更多信息。


    编辑:对于您在评论中的代码:

    sample1.classlabel[0] = 0; 
    sample1.classlabel[1]=1;
    vector<Mat> temp; 
    temp.push_back(img1); 
    temp.push_back(img2); 
    temp.push_back(img3); 
    sample1.image=temp; 
    
    sample2.classlabel[0] = 0; 
    sample2.classlabel[1]=1; 
    vector<Mat> temp1; 
    temp.push_back(img5);         // problem
    temp.push_back(img6);         // problem
    temp.push_back(img7);         // problem
    sample2.image=temp1; 
    
    samples.push_back(sample1); 
    samples.push_back(sample2);
    

    3 个问题行应改为

    temp1.push_back(img5);
    temp1.push_back(img6);
    temp1.push_back(img7); 
    

    【讨论】:

    • 你没看懂,也可能是我解释有误,尺寸显示为样本[2]。但是,如果您访问 samples[0] 或 samples[1] 它会显示 classlabel=???和图像=????。
    • @khan 处于调试模式还是发布模式?您可能需要在此处分享您的完整代码。
    • 在调试中,sample1.classlabel[0] = 0; sample1.classlabel[1]=1;矢量温度; temp.push_back(img1); temp.push_back(img2); temp.push_back(img3);样品1.图像=温度; sample2.classlabel[0] = 0; sample2.classlabel[1]=1;矢量 temp1; temp.push_back(img5);temp.push_back(img6);temp.push_back(img7); sample2.image=temp1; samples.push_back(sample1); samples.push_back(sample2);
    • @khan sample 是什么?请分享您的完整代码,以便我们重现问题。
    • @khan 它可能工作正常 - 有时调试器无法显示复杂的对象。但它们的值是正确的。那 ????仅表示它无法在调试器中显示,但没有别的
    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多