繁体   English   中英

OpenCV:将Mat转换为UChar4

[英]OpenCV: Convert Mat into UChar4

我刚刚完成了Udacity Parallel编程第二阶段的课程,现在我正在用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通道Mats,但是基本:

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

会生成一个(未使用的)第四通道。 现在您可能必须为您的输出图像分配内存:

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

暂无
暂无

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

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