IP摄像机访问使用rtsp, visual studio OpenCv 2.4.5

IP camera acessing using rtsp, visual studio OpenCv 2.4.5?

本文关键字:OpenCv studio visual 访问 摄像机 rtsp IP      更新时间:2023-10-16

这是使用lan端口访问我的IP摄像机的代码。(第一个代码工作正常)。我需要的是得到图像与Mat(c++)结构。代码2显示了我使用Mat结构所做的事情,但是当我调试程序时,执行cv::namedWindow("Frame");然后中断代码,给出如下所示的未处理异常。我的最后一个要求是使用Mat而不是iplimage完成这项工作。提示或适当的代码将是伟大的,因为我正在做一个项目在人类检测使用HOG。谢谢。

#include "stdafx.h"
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include "opencv.hpp"
int main(){
CvCapture *camera=cvCaptureFromFile("rtsp://192.168.1.19:554/0/1:1/main");
if (camera==NULL)
printf("camera is nulln");
else
printf("camera is not null");
cvNamedWindow("img");
while (cvWaitKey(10)!=atoi("q")){
IplImage *img=cvQueryFrame(camera);
cvShowImage("img",img);
}
cvReleaseCapture(&camera);
}

代码2:

int main(int argc, char* argv[])
{
        cv::Ptr<CvCapture> capture = cvCaptureFromFile("rtsp://192.168.1.19:554/0/1:1/main");
        cv::namedWindow("Frame");
        for (;;)
        {
            cv::Mat frame = cvQueryFrame(capture);
            cv::imshow("Frame", frame);
            if (cv::waitKey(1) >= 0)
            break;
        }
    return 0;
}

例外:在Hog与Web cam.exe中的0x00660598未处理的异常:0xC0000005:访问违反读取位置0xcccc0065。

是啊,扔掉那该死的c-api !

int main(int argc, char* argv[])
{
        cv::namedWindow("Frame");
        cv::VideoCapture capture("rtsp://192.168.1.19:554/0/1:1/main");
        while ( capture.isOpened() )     // check !!
        {
            cv::Mat frame;
            if ( ! capture.read(frame) ) // another check !!
                break;
            cv::imshow("Frame", frame);
            if (cv::waitKey(1) >= 0)
                break;
        }
    return 0;
}