Skip to content

Instantly share code, notes, and snippets.

@yezhiming
Last active January 28, 2019 15:05
Show Gist options
  • Save yezhiming/43653c8303e4e52bc1cac4a23c1e7242 to your computer and use it in GitHub Desktop.
Save yezhiming/43653c8303e4e52bc1cac4a23c1e7242 to your computer and use it in GitHub Desktop.
convert FFmpeg AVFrame to OpenCV Mat
#include <opencv2/opencv.hpp>
extern "C"
{
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/frame.h>
}
// link libswscale in c++
// ref: https://soledadpenades.com/posts/2009/linking-with-ffmpegs-libav/
void convert(struct AVFrame *frame) {
int w = frame->width;
int h = frame->height;
AVFrame *frameBGR = av_frame_alloc(); // opencv use BGR color space
av_image_alloc(frameBGR->data, frameBGR->linesize, frame->width, frame->height, AV_PIX_FMT_BGR24, 1);
struct SwsContext *ctx = sws_getContext(
w, h, (AVPixelFormat)frame->format,
w, h, AV_PIX_FMT_BGR24,
SWS_FAST_BILINEAR, NULL, NULL, NULL);
sws_scale(ctx, frame->data, frame->linesize, 0, h, frameBGR->data, frameBGR->linesize);
// mat type should match pix_fmt param of av_image_alloc, 8 x 3 = 24
cv::Mat mat = cv::Mat(h, w, CV_8UC3, frameBGR->data[0], frameBGR->linesize[0]);
sws_freeContext(ctx);
av_freep(&frameBGR->data);
av_frame_free(&frameBGR);
//do something with mat
}
#ifndef FTM_H
#define FTM_H
//using in pure C
#ifdef __cplusplus
extern "C" {
#endif
// forward declarations
typedef struct AVFrame AVFrame;
convert(struct AVFrame *frame);
#ifdef __cplusplus
}
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment