
C を使用して効率的なビデオ ストリーム処理とビデオ分析を行うにはどうすればよいですか?
要約: ビデオ技術の急速な発展に伴い、ビデオ処理と分析を必要とするアプリケーションがますます増えています。この記事では、ビデオ ストリームの取得、ビデオ デコード、ビデオ エンコード、ビデオ分析など、効率的なビデオ ストリーム処理とビデオ分析に C 言語を使用する方法を紹介し、対応するコード例を示します。
1. ビデオ ストリームの取得
ビデオ ストリームの取得はビデオ処理の最初のステップであり、主にカメラ、ファイル、ネットワークなどのソースからビデオ ストリームを取得します。 C では、ビデオ ストリームの取得に OpenCV ライブラリを使用できます。これは使いやすく強力です。
次は、OpenCV ライブラリを使用してローカル ビデオ ファイルを取得するコード例です:
#include <opencv2/opencv.hpp>
int main() {
cv::VideoCapture cap("test.mp4"); // 打开本地视频文件
if (!cap.isOpened()) { // 检查文件是否成功打开
std::cout << "Failed to open video file!" << std::endl;
return -1;
}
cv::Mat frame;
while (cap.read(frame)) { // 读取每一帧画面
cv::imshow("Video", frame); // 显示视频
cv::waitKey(1);
}
cap.release(); // 释放资源
return 0;
}2. ビデオ デコード
ビデオ デコードとは、圧縮されたビデオ ストリームを元のビデオ フレーム データにデコードすることです。その後の使用の処理と分析。 C では、広範なサポートと効率的なデコード パフォーマンスを備えた FFmpeg ライブラリをビデオ デコードに使用できます。
次は、FFmpeg ライブラリを使用してビデオ ファイルをデコードし、各フレームを出力するコード例です:
extern "C" {
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
int main() {
av_register_all();
AVFormatContext* format_ctx = nullptr;
if (avformat_open_input(&format_ctx, "test.mp4", nullptr, nullptr) != 0) {
std::cout << "Failed to open video file!" << std::endl;
return -1;
}
avformat_find_stream_info(format_ctx, nullptr);
int video_stream_index = -1;
for (int i = 0; i < format_ctx->nb_streams; i++) {
if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_index = i; // 找到视频流索引
break;
}
}
AVCodecParameters* codec_params = format_ctx->streams[video_stream_index]->codecpar;
AVCodec* codec = avcodec_find_decoder(codec_params->codec_id);
if (codec == nullptr) {
std::cout << "Failed to find decoder!" << std::endl;
return -1;
}
AVCodecContext* codec_ctx = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(codec_ctx, codec_params);
avcodec_open2(codec_ctx, codec, nullptr);
AVFrame* frame = av_frame_alloc();
AVPacket packet;
while (av_read_frame(format_ctx, &packet) >= 0) {
if (packet.stream_index == video_stream_index) {
avcodec_send_packet(codec_ctx, &packet);
avcodec_receive_frame(codec_ctx, frame);
// TODO: 处理每一帧画面
}
av_packet_unref(&packet);
}
av_frame_free(&frame);
avcodec_free_context(&codec_ctx);
avformat_close_input(&format_ctx);
return 0;
}3. ビデオ エンコード
ビデオ エンコードとは、処理されたビデオ フレーム データを保存用に圧縮することです。そして送信。 C では、ビデオ エンコードに FFmpeg ライブラリを使用して、効率的なビデオ圧縮とエンコードを実現することもできます。
次は、FFmpeg ライブラリを使用して元のビデオ フレーム データを H.264 形式のビデオ ファイルにエンコードするコード例です:
extern "C" {
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
}
int main() {
av_register_all();
AVFormatContext* format_ctx = nullptr;
if (avformat_alloc_output_context2(&format_ctx, nullptr, nullptr, "output.mp4") != 0) {
std::cout << "Failed to create output format context!" << std::endl;
return -1;
}
AVOutputFormat* output_fmt = format_ctx->oformat;
AVStream* video_stream = avformat_new_stream(format_ctx, nullptr);
if (video_stream == nullptr) {
std::cout << "Failed to create video stream!" << std::endl;
return -1;
}
AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (codec == nullptr) {
std::cout << "Failed to find encoder!" << std::endl;
return -1;
}
AVCodecContext* codec_ctx = avcodec_alloc_context3(codec);
if (codec_ctx == nullptr) {
std::cout << "Failed to allocate codec context!" << std::endl;
return -1;
}
codec_ctx->width = 640;
codec_ctx->height = 480;
codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
codec_ctx->time_base = (AVRational){1, 30};
if (format_ctx->oformat->flags & AVFMT_GLOBALHEADER) {
codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
avcodec_open2(codec_ctx, codec, nullptr);
avcodec_parameters_from_context(video_stream->codecpar, codec_ctx);
avio_open(&format_ctx->pb, "output.mp4", AVIO_FLAG_WRITE);
avformat_write_header(format_ctx, nullptr);
// TODO: 逐帧编码并写入
av_write_trailer(format_ctx);
avio_close(format_ctx->pb);
avcodec_free_context(&codec_ctx);
avformat_free_context(format_ctx);
return 0;
}4. ビデオ分析
ビデオ分析は実行します。ビデオデータに対するさまざまな操作アルゴリズムと処理、ビデオ内の重要な情報と特徴を抽出して、ターゲット検出、アクション認識などのさまざまなタスクを完了します。 C では、ビデオ分析に OpenCV ライブラリを使用し、それを他の画像処理アルゴリズムと組み合わせて、より高度なビデオ分析を行うことができます。
以下は、OpenCV ライブラリを使用してビデオのターゲット検出を実行するコード例です:
#include <opencv2/opencv.hpp>
int main() {
cv::VideoCapture cap("test.mp4");
if (!cap.isOpened()) {
std::cout << "Failed to open video file!" << std::endl;
return -1;
}
cv::CascadeClassifier classifier("haarcascade_frontalface_default.xml");
cv::Mat frame;
while (cap.read(frame)) {
cv::Mat gray;
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
std::vector<cv::Rect> faces;
classifier.detectMultiScale(gray, faces, 1.1, 3);
for (const auto& rect : faces) {
cv::rectangle(frame, rect, cv::Scalar(0, 255, 0), 2);
}
cv::imshow("Video", frame);
cv::waitKey(1);
}
cap.release();
return 0;
}概要: この記事では、C 言語を使用して効率的なビデオ ストリーム処理とビデオ分析を行う方法を紹介します。ビデオストリームの取得とビデオ解析のための OpenCV ライブラリと、ビデオデコードとビデオエンコードのための FFmpeg ライブラリを通じて、さまざまなビデオ処理と分析機能を簡単に実装できます。この記事で提供されているコード例を通じて、読者は開発プロセス中に参照し、実際のプロジェクトに適用することができます。この記事がビデオ処理とビデオ分析の読者にとって役立つことを願っています。
以上がC++ を使用して効率的なビデオ ストリーム処理とビデオ分析を行うにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。