OpenCV:将 Mat 转换为 UChar4

OpenCV: Convert Mat into UChar4

本文关键字:UChar4 转换 Mat OpenCV      更新时间:2023-10-16

我刚刚完成了Udacity 并行编程第 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,这样我就可以拆分通道,进行卷积,然后重新组合通道以返回输出图像。 有什么办法可以做到这一点吗?

opencv 通常使用 bgr,3 通道垫,但基本:

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

将生成一个(未使用的(第 4 个通道。 现在你可能必须为你分配 mem 输出图像:

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 );