我需要从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和其他库,但我需要在缓冲区中保存帧。