简体   繁体   English

C ++ Opencv:尝试打印Mat值,但得到空打印

[英]C++ Opencv: Try to print Mat value, but get empty prints

The full source code is shown as follows: 完整的源代码如下所示:

#include "opencv2/opencv.hpp"
#include <opencv2/core/core.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char const *argv[])
{
    int n_Channel = 3;
    Mat M(Size(100, 200), CV_32FC(n_Channel));

    cout << M.rows << "," << M.cols << "," << M.channels() << endl;
    for (int i = 0; i < n_Channel; ++i)
    {
        M.at<Vec3b>(4,3)[i] = i+1;
    }
    for (int i = 0; i < n_Channel; ++i)
    {
        cout << "i=" << i << ", " << M.at<Vec3b>(4,3) << endl;
        cout << "i=" << i << ", |" << M.at<Vec3b>(4,3)[i] << "|" << endl;
    }
    return 0;
}

In the last of the print, I expect to see printed lines like "i=0, |2|". 在打印的最后,我希望看到打印的行,例如“ i = 0,| 2 |”。 However, I get "i=0, ||", with nothing enclosed in the pair of "|". 但是,我得到“ i = 0,||”,而在“ |”对中没有任何内容。 What's wrong with my code? 我的代码出了什么问题?

The complete printed messages are shown as follows: 完整的打印消息如下所示:

200,100,3
i=0, [1, 2, 3]
i=0, ||
i=1, [1, 2, 3]
i=1, ||
i=2, [1, 2, 3]
i=2, ||

Thank you all for helping me! 谢谢大家的帮助!

Miki's comment is correct but here is an explanation why the mentioned error occurred: Miki的评论是正确的,但这是为什么发生上述错误的解释:

While initializing Mat M by: 在通过以下方式初始化Mat M

Mat M(Size(100, 200), CV_32FC(n_Channel));

The Matrix type of M is set to 32 bit and each pixel in the 3 channelled Mat M holds float value ranging from 0 to 1.0. M的矩阵类型设置为32位,并且3通道Mat M中的每个像素保持的浮点值介于0到1.0之间。 Now to print the values of the pixels in Mat M it is necessary to use correct specifier , in case of Mat M , it should be Vec3f , that is Vector with 3 float entries instead of Vec3b , that is Vector with 3 byte entries(would have been suitable for CV_8UC3 ). 现在要打印Mat M中的像素值,必须使用正确的说明符 ,对于Mat M ,它应该是Vec3f (即具有3个浮点条目的Vector而不是Vec3b ,即具有3个字节条目的Vector(将适用于CV_8UC3 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM