0

我需要从 ip-camera 获取 rtsp 流并将流剪切到帧。我试过了:

int _tmain(int argc, _TCHAR* argv[])
{
    CvCapture *camera = cvCreateFileCapture("rtsp://MYIP:8554/CH001.sdp");

    if (camera == NULL) {
        printf("camera is null, aborting...");
        return -1;
    }
    printf("camera is not null\n");
    fflush(stdout);
    cvNamedWindow("img");
    int i = 0;
    while (cvWaitKey(100) != 27) {
        IplImage *img = cvQueryFrame(camera);
        if (img == NULL) break;
        cvShowImage("img", img);
        // cvReleaseCapture(&camera); 
        // printf("Image: %i\n", ++i);
    }
    cvReleaseCapture(&camera);
    return 0;
}

不起作用,但是:

int _tmain(int argc, _TCHAR* argv[])
{
    {
    VideoCapture cap("C:/720.mp4"); // open the video file for reading
    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the video file" << endl;
         return -1;
    }

    //cap.set(CV_CAP_PROP_POS_MSEC, 90000); //start the video at 300ms
    double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
     cout << "Frame per seconds : " << fps << endl;
    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
    while(1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
                        cout << "Cannot read the frame from video file" << endl;
                       break;
        }
        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
       {
                cout << "esc key is pressed by user" << endl; 
                break; 
       }
    }
    return 0;
}
}

效果很好!如何将rtsp流切割成帧?如何在 opencv 中使用 RTSP?我也可以使用 FFmpeg 和其他库,但我需要在缓冲区中保存帧。

4

0 回答 0