【问题标题】:OpenCV: Convert Mat into UChar4OpenCV:将 Mat 转换为 UChar4
【发布时间】:2015-02-16 07:42:54
【问题描述】:

我刚刚完成了 Udacity Parallel 编程第 2 阶段的课程,现在我正在使用 OpenCV 将我学到的知识应用到一个基本应用程序中,该应用程序将高斯模糊应用于来自网络摄像头的恒定图像流。

我正在将帧加载到Mat 对象中,而在我的循环中我想调用一个方法gaussian_cpu,唯一的问题是它需要将uchar4 传递给输入和输出参数。如何将Mat 对象转换为uchar4

// Keep processing frames - Do CPU First
while(cpu_frames > 0)
{
    cout << cpu_frames << "\n";
    camera >> frameIn;

    gaussian_cpu(frameIn, frameOut, numRows(), numCols(), h_filter__, 9);

    imshow("Source", frameIn);
    imshow("Dest", frameOut);

    // 2ms delay to prevent system from being interrupted whilst drawing the new frame
    waitKey(2);
    cpu_frames--;
}

我的方法签名如下所示:

void gaussian_cpu(
                const uchar4* const rgbaImage,       // input image from the camera
                uchar4* const outputImage,           // The image we are writing back for display
                size_t numRows, size_t numCols,      // Width and Height of the input image (rows/cols)
                const float* const filter,           // The value of sigma
                const int filterWidth                // The size of the stencil (3x3) 9
             )

我需要使用 uchar4 来拆分通道,进行卷积,然后重新组合通道以返回输出图像。有没有办法做到这一点?

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    opencv一般用bgr,3通道Mats,不过一个基本的:

    Mat bgra;
    cvtColor( frameIn, bgra, CV_BGR2BGRA );
    

    将生成一个(未使用的)第 4 个通道。现在你可能需要为你的 outputImage 分配内存:

    Mat frameOut( bgra.size(), bgra.type() );
    

    然后你可以将它们输入你的 gaussian_cpu():

    int filterWidth=5;
    float *filter = ... // your job, not mine ;)
    gaussian_cpu( (uchar4*)(bgra.data), (uchar4*)(frameOut.data), bgra.rows, bgra.cols, filter, filterWidth );
    

    【讨论】:

    • 这看起来很有希望 - .data 在方法 sig 中看起来像什么?会是 uchar 还是不同的东西:)
    • uchar* Mat::data (这就是它保存任何类型数据的方式)
    • 第四通道是可选的 alpha ;)
    • 请注意,来自网络摄像头的图像中没有 alpha!但是如果你使用 imread("my.png", -1);那么是的!
    • 非常有趣 - 这是我第一次真正深入了解底层世界,不得不说我很喜欢它,尽管在与编译器错误搏斗时偶尔会头疼;)再次感谢你的帮手:)
    猜你喜欢
    • 2018-09-04
    • 2015-12-13
    • 2017-02-19
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多