从ip camera拍摄图像

时间:2016-09-08 19:55:55

标签: c++ opencv libvlc ip-camera

我想从我的IP摄像头拍摄图像。我在2012年的visual studio工作。 起初,我使用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

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

1 个答案:

答案 0 :(得分:1)

我正在处理像你这样的项目,这个错误是因为openCV无法处理H264,我建议您使用here中提到的VLC库,并导致您使用VS2012可以在你的项目中使用VLCDotNet。其他选项FFmpegaforgeH264兼容,更多来自你的名字我知道你是伊朗人你好锁定你的代码,