c++ - 从网络摄像机获取图像

标签 c++ opencv libvlc ip-camera

我想从我的网络摄像机拍摄图像。我在 visual studio 2012 工作。 起初,我使用 openCV 连接到它。这是我的代码。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"

int main()
{
cv::VideoCapture vcap;

const std::string videoStreamAddress = "rtsp://admin:admin@192.168.0.120/snl/live/1/1/stream1.cgi";

if (!vcap.open(videoStreamAddress))
{
    printf("camera is null\n");
    return -1;
}
else
{
    cv::Mat image;
    cv::namedWindow("Video", CV_WINDOW_AUTOSIZE);

    while(1)
    {

        vcap >> image;
        imshow("Video", image);
        if(cv::waitKey(10) == 99 ) break;
     }
 }

 cv::waitKey(1000);
 return 0;
}

它显示流。但是它的一些图像有失真。我认为这是因为解码错误。错误是这样的:

[h264 @ 00decfe0] error while decoding MB 37 22, bytestream <td>

为了解决这个错误,我写了另一个代码,灵感来自 http://answers.opencv.org/question/65932/how-to-stream-h264-video-with-rtsp-in-opencv-partially-answered/ .它使用 libVLC 来获取流。这是我的第二个代码。

#include "opencv2/opencv.hpp"
#include "vlc/libvlc.h"
#include "vlc/libvlc_media.h"
#include "vlc/libvlc_media_player.h"

#include "ctime"
#include "Windows.h"
#include "string"

struct ctx
{
   IplImage* image;
   HANDLE mutex;
   uchar*    pixels;
};

void *lock(void *data, void**p_pixels)
{
    struct ctx *ctx = (struct ctx*)data;
    WaitForSingleObject(ctx->mutex, INFINITE);
    *p_pixels = ctx->pixels;   
    return NULL;

}
void display(void *data, void *id){
   (void) data;
   assert(id == NULL);
}
void unlock(void *data, void *id, void *const *p_pixels){
   struct ctx *ctx = (struct ctx*)data;
   /* VLC just rendered the video, but we can also render stuff */
   uchar *pixels = (uchar*)*p_pixels;
   cvShowImage("image", ctx->image);
   ReleaseMutex(ctx->mutex);
   assert(id == NULL); /* picture identifier, not needed here */
}

int main()
{
    // VLC pointers
    libvlc_instance_t *vlcInstance;
    libvlc_media_player_t *mp;
    libvlc_media_t *media;

    const char * const vlc_args[] = {
  "-I", "dummy", // Don't use any interface
  "--ignore-config", // Don't use VLC's config
  "--extraintf=logger", // Log anything
  "--verbose=2", // Be much more verbose then normal for debugging purpose
    };

    vlcInstance = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);
    media = libvlc_media_new_location(vlcInstance, "rtsp://admin:admin@192.168.21.120/snl/live/1/1/stream1.cgi");

    mp = libvlc_media_player_new_from_media(media);
    libvlc_media_release(media);
    context->mutex = CreateMutex(NULL, FALSE, NULL);
    context->image = new Mat(VIDEO_HEIGHT, VIDEO_WIDTH, CV_8UC3);
    context->pixels = (unsigned char *)context->image->data;

    // show blank image
    imshow("test", *context->image);

    libvlc_video_set_callbacks(mp, lock, unlock, display, context);
    libvlc_video_set_format(mp, "RV24", VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * 24 / 8); // pitch = width * BitsPerPixel / 8

    int ii = 0;
    int key = 0;
    while(key != 27)
    {
        ii++;
        if (ii > 5)
        {
            libvlc_media_player_play(mp);
        }
        float fps =  libvlc_media_player_get_fps(mp);
        //printf("fps:%f\r\n",fps);
        key = waitKey(100); // wait 100ms for Esc key
    }

    libvlc_media_player_stop(mp);

    return 0;

}

但它有几乎相同的错误。部分像素丢失,图像失真。

[h264 @ 02a3b660] Frame num gap 6 4
[h264 @ 02a42220] no picture oooo

我该如何解决这个问题?问题根源于我的代码和使用的库还是我的相机?

最佳答案

我和你一样在做一些项目,这个错误是因为openCV无法处理H264 ,我建议您使用 here 中提到的 VLC 库, 因为你使用 VS2012 你可以使用 VLCDotNet在你的项目中。其他选项是 FFmpeg , aforgeH264 兼容.

关于c++ - 从网络摄像机获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39399196/

相关文章:

c - 如何确定特定色度的每个像素的平面数和字节数?

ios - VLCMobileKit iOS 在 iPhone 4 屏幕锁定时崩溃

python - 使用 OpenCV 和 Tkinter 在整个屏幕上显示视频

c++ - 如何创建带有更改编号的文件名

python - 如何可靠地检测条码的 4 个角?

c - 使用 libvlc 播放 FTP 文件

c++ - 为什么 std::remove_const 不删除 const 限定符?

python - 如何在 Raspberry Pi 上使用 TBB 构建 OpenCV?

c++ - 无法将 std::set_intersection 应用于具有公共(public)字段的不同类型的结构

c++ - g++ 不编译我的源代码。也许一些模板错误?