vlc接收rtsp视频流然后存到opencv mat中

最近做一个项目是要用opencv来加工处理rtsp流,直接用opencv 中自带的VideoCapture video(rtsp://192.168.1.221:554)这种方式能够拉到rtsp流,但是很容易解码错误,毕竟opencv也不是做视频解码的,后来决定用vlc来处理,代码如下。

#include "libvlc.h"  
#include "libvlc_media.h"
#include "libvlc_media_player.h"

#include
#include
#include "opencv2/imgproc.hpp"
#include
#include
#include


Mat videobuf;

void* lock(void *data, void**p_pixels)  
{  
    *p_pixels = videobuf.data;  
    return NULL;  


void VideoShow::rtspstream()
{
videobuf.create(height,width,CV_8UC3);
    libvlc_media_t* media = NULL;  
    libvlc_media_player_t* mediaPlayer = NULL;  
    char const* vlc_args[] =  
    {  
        "-I",  
        "dummy",  
        "--ignore-config",  
    };  
      
    libvlc_instance_t* instance = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);   
      
    media = libvlc_media_new_location(instance, "rtsp://192.168.1.221:554");  
    mediaPlayer = libvlc_media_player_new_from_media(media);  
  
    //libvlc_media_player_set_media(mediaPlayer, media);  
    libvlc_video_set_callbacks(mediaPlayer, lock, NULL, NULL, NULL);  
    libvlc_video_set_format(mediaPlayer, "RV24", width, height, width*24/8); 
libvlc_media_release(media);


for(int i=0;i<5;i++)
    {
libvlc_media_player_play(mediaPlayer);  
               Sleep(500);
}
}

int main()

{

    Mat show ;

    while(true){

     show = videobuf;

     imshow("rtsp",show);

      waitKey(30);

}

}



你可能感兴趣的:(vlc接收rtsp视频流然后存到opencv mat中)