【问题标题】:Multi channel Mat display function多通道垫子显示功能
【发布时间】:2012-08-29 13:26:34
【问题描述】:

我想写一个简短的函数来显示 OpenCV Mat 矩阵类型。我关闭完成它。这是我的代码:

template <class eleType>
int MATmtxdisp(const Mat mtx, eleType fk)   
// create function template to avoid type dependent programming i.e. Mat element can be float, Vec2f, Vec3f, Vec4f
// cannot get rid of fk ('fake') variable to identify Mat element type
{   
    uchar chan = mtx.channels();

    for(int i = 0; i < mtx.rows; i++)
        for(int k = 0; k < chan; k++)
        {
            for(int l = 0; l < mtx.cols; l++)
            {
                eleType ele = mtx.at<eleType>(i,l);  
// I even split 2 cases for 1 channel and multi-channel element
// but this is unacceptable by the compiler when element is float type (1 channel)
// i.e. C2109: subscript requires array or pointer type
                if (chan > 1)
                    printf("%8.2f",ele[k]);   
                else 
                    printf("%8.2f",ele);
            }
            printf("\n");
        }

        return 0;
}

有什么办法可以解决这个问题,让我拥有一个紧凑且无通道数的显示功能???

【问题讨论】:

    标签: c++ opencv matrix


    【解决方案1】:

    这个怎么样?

    cv::Mat myMat(2,3, CV_8UC3, cv::Scalar(1, 2, 3));
    std::cout << myMat << std::endl;
    

    不管怎样,如果你使用 cout,你的代码会更容易:

    template <class T>
    int MATmtxdisp(const Mat& mtx)
    {   
        uchar chan = mtx.channels();
        int step = mtx.step();
        T* ptr = (T*)mtx.data;
    
        for(int i = 0; i < mtx.rows; i++)
            for(int k = 0; k < chan; k++)
            {
                for(int l = 0; l < mtx.cols; l++)
                {
                    cout << ptr[ /* calculate here the index */];
                }
                cout << endl;
        }
    }
    

    【讨论】:

    • 谢谢你的好提示!我刚刚学会了。更漂亮的输出格式怎么样??
    • 输出有一些修饰符,但我不记得了。您可以将其格式化为 Matlab 风格、C 风格,我认为是 XML 风格。它在文档中的某个地方...
    • 这也可以应用于 CvMat 和 InputArray 或 OutputArray 吗? OpenCV 中其他版本的矩阵??
    • InputArray 和 outputArrat 有一个 getMat() 方法可以将它们转换为 Mat。 CvMat 也应该很容易转换为 Mat,但直接刷新到 cout 是不可能的
    • 非常感谢!如果我对输出格式有问题,希望你能停下来:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多