I'm trying to use Qt Camera from QML.
I'm developing a custom VideoFilter:
QVideoFrame MyFilterRunnable::run(QVideoFrame* input, const QVideoSurfaceFormat&, RunFlags)
I started deploying the application on Windows and I have that:
QAbstractVideoBuffer::ReadWrite
PixelFormat::Format_BGR32
When I moved to Linux, unfortunately, everything changed, without changing the camera I have:
QAbstractVideoBuffer::ReadOnly
PixelFormat::Format_YUYV
And now I really don't know how to convert this frame to an OpenCV Mat
.
Is there any way to choose which will be the pixel format of the Camera?
Right on Steven,
if (input->pixelFormat() == QVideoFrame::Format_YUYV)
{
auto input_w = input->width ();
auto input_h = input->height();
auto cam_data = input->bits();
cv::Mat yuyv(input_h, input_w,CV_8UC2, cam_data);
cv::Mat rgb (input_h, input_w,CV_8UC3);
cvtColor(yuyv, rgb, CV_YUV2BGR_YUYV);
m_mat = rgb;
}
else
if (input->pixelFormat() == QVideoFrame::Format_YUV420P || input->pixelFormat() == QVideoFrame::Format_NV12) {
m_yuv = true;
m_mat = yuvFrameToMat8(*input);
} else {
m_yuv = false;
QImage wrapper = imageWrapper(*input);
if (wrapper.isNull()) {
if (input->handleType() == QAbstractVideoBuffer::NoHandle)
input->unmap();
return *input;
}
m_mat = imageToMat8(wrapper);
}
ensureC3(&m_mat);
I am facing the same problem between my Linux machine and a Raspberry : I am using the same camera and the the pixel formats gven by QVideoFrame are different. It has probably something to do with v4l2
About the conversion from YUYV to OpenCV Mat, this code worked for me :
QVideoFrame *input ;
...
auto input_w = input->width ();
auto input_h = input->height();
auto cam_data = input->bits();
cv::Mat yuyv(input_h, input_w,CV_8UC2, cam_data);
cv::Mat rgb (input_h, input_w,CV_8UC3);
cvtColor(yuyv, rgb, CV_YUV2BGR_YUYV);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.