原来Qt从视频中获取每一帧数据如此简单

有时候需要在视频上画图,所以需要能获取到每一帧视频数据。

以前从视频文件或视频流中得到帧,一般都是使用qt + ffmpeg或qt + vlc。

qt对显示处理视频大体有以下方法:

1. QMediaPlayer + QVideoWidget

这种方法只适合简单的显示视频功能,不适合对视频进行处理(比如画图)

2. QMediaPlayer + QGraphicsVideoItem + QGraphicsScene + QGraphicsView

这种方法功能强大,除了显示视频功能,还可以做复杂的图形处理(具体可以查看QGraphicsScene的使用)

3. QMediaPlayer + QAbstractVideoSurface

这种方法比较简单,是我下面要介给的。可以获取到每一帧视频数据,基本可以实现与qt + ffmpeg或qt + vlc相同的效果。

 

自定义VideoSurface:

class VideoSurface : public QAbstractVideoSurface
{
	Q_OBJECT

public:
	VideoSurface(QObject *parent = Q_NULLPTR);
	~VideoSurface();

	QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const;
	bool present(const QVideoFrame &frame);

signals:
	void frameAvailable(QVideoFrame &frame);

};

实现如下:

VideoSurface::VideoSurface(QObject *parent)
	: QAbstractVideoSurface(parent)
{
}

VideoSurface::~VideoSurface()
{
}

QList<QVideoFrame::PixelFormat> VideoSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
	QList<QVideoFrame::PixelFormat> listPixelFormats;

	listPixelFormats << QVideoFrame::Format_ARGB32
		<< QVideoFrame::Format_ARGB32_Premultiplied
		<< QVideoFrame::Format_RGB32
		<< QVideoFrame::Format_RGB24
		<< QVideoFrame::Format_RGB565
		<< QVideoFrame::Format_RGB555
		<< QVideoFrame::Format_ARGB8565_Premultiplied
		<< QVideoFrame::Format_BGRA32
		<< QVideoFrame::Format_BGRA32_Premultiplied
		<< QVideoFrame::Format_BGR32
		<< QVideoFrame::Format_BGR24
		<< QVideoFrame::Format_BGR565
		<< QVideoFrame::Format_BGR555
		<< QVideoFrame::Format_BGRA5658_Premultiplied
		<< QVideoFrame::Format_AYUV444
		<< QVideoFrame::Format_AYUV444_Premultiplied
		<< QVideoFrame::Format_YUV444
		<< QVideoFrame::Format_YUV420P
		<< QVideoFrame::Format_YV12
		<< QVideoFrame::Format_UYVY
		<< QVideoFrame::Format_YUYV
		<< QVideoFrame::Format_NV12
		<< QVideoFrame::Format_NV21
		<< QVideoFrame::Format_IMC1
		<< QVideoFrame::Format_IMC2
		<< QVideoFrame::Format_IMC3
		<< QVideoFrame::Format_IMC4
		<< QVideoFrame::Format_Y8
		<< QVideoFrame::Format_Y16
		<< QVideoFrame::Format_Jpeg
		<< QVideoFrame::Format_CameraRaw
		<< QVideoFrame::Format_AdobeDng;

	//qDebug() << listPixelFormats;

	// Return the formats you will support
	return listPixelFormats;
}

bool  VideoSurface::present(const QVideoFrame &frame)
{
	// Handle the frame and do your processing
	if (frame.isValid())
	{
		QVideoFrame cloneFrame(frame);
		emit frameAvailable(cloneFrame);

		return true;
	}

	return false;
}

看了上面的代码就知道,只需要外部连接frameAvailable信号就可以获取到每一帧数据。

具体使用:

QMediaPlayer *mediaPlayer = new QMediaPlayer;
VideoSurface *videoSurface = new VideoSurface;
mediaPlayer->setVideoOutput(videoSurface);
mediaPlayer->setMedia(QUrl("rtsp://admin:admin@192.168.1.112"));
mediaPlayer->play();

把frameAvailable信号与显示窗口的槽连接,比如:

connect(videoSurface, SIGNAL(frameAvailable(QVideoFrame &)), this, SLOT(ProcessFrame(QVideoFrame &)));
void QtVideoTest::ProcessFrame(QVideoFrame &frame)
{
	qDebug() << "=============ProcessFrame===============";
	qDebug() << "width : " << frame.width() << " height : " << frame.height();
	qDebug() << "start time : " << frame.startTime()/1000 << "ms";
	qDebug() << "end time : " << frame.endTime()/1000 << "ms";
	qDebug() << "pixelFormat :" << frame.pixelFormat();

	frame.map(QAbstractVideoBuffer::ReadOnly);
	QImage recvImage(frame.bits(), frame.width(), frame.height(), QVideoFrame::imageFormatFromPixelFormat(frame.pixelFormat()));

	qDebug() << "frame data size :" << frame.mappedBytes();
	frame.unmap();
}

如上,QVideoFrame转QImage,拿QImage进行画图操作就简单了。

 

 

 

 

 

 

  • 13
    点赞
  • 130
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
您可以使用OpenCV的VideoCapture类来从多个视频文件获取一帧数据。以下是一个简单的示例代码,它从16个视频文件获取一帧数据并将其保存到一个Mat对象: ```cpp #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { // 打开16个视频文件 vector<VideoCapture> captures; for (int i = 0; i < 16; ++i) { VideoCapture cap("video" + to_string(i) + ".mp4"); if (!cap.isOpened()) { cerr << "Cannot open video file " << i << endl; return -1; } captures.push_back(cap); } // 获取每个视频的第一帧 vector<Mat> frames; for (int i = 0; i < 16; ++i) { Mat frame; captures[i] >> frame; if (!frame.empty()) { frames.push_back(frame); } else { cerr << "Cannot read frame from video file " << i << endl; return -1; } } // 将所有拼接成一个大图像 Mat combined(frames.size() * frames[0].rows, frames[0].cols, CV_8UC3); for (int i = 0; i < frames.size(); ++i) { Mat roi(combined, Rect(0, i * frames[0].rows, frames[i].cols, frames[i].rows)); frames[i].copyTo(roi); } // 显示结果 namedWindow("First Frames", WINDOW_NORMAL); imshow("First Frames", combined); waitKey(0); destroyAllWindows(); return 0; } ``` 在这个示例,我们首先打开16个视频文件并将它们存储在一个vector。然后,我们循环遍历每个视频文件,使用VideoCapture类来获取一帧数据,并将其存储在一个Mat对象。最后,我们将所有拼接成一个大图像,并将其显示在一个窗口

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值