how can i read all frames?

asked 2015-01-14 04:29:39 -0600

h_mahmoodian gravatar image

I have written some code to read a video and do some processing on it.

int totalframes=(int) cap.get(CV_CAP_PROP_FRAME_COUNT);

total frames=1707

int fps=(int) cap.get(CV_CAP_PROP_FPS);

fps=30

I do processing on each frame but my code does it for only 804 frames. How can I do the processing for all frames? After processing 804 frames ,i see this text:

Unhandled exception at at 0x75299617 in opencv learning.exe: Microsoft C++ exception: cv::Exception at memory location 0x0041B778.

this is a .avi video format.

Can you please help me to solve this problem? I use windows 7,visual studio 2012,opencv 2.4.6 for processing this video file. my code:

#include "opencv2/opencv.hpp"
#include <stdio.h>
#include <conio.h>

using namespace cv;
using namespace std;

int main(int, char**)
{
int k=0;
VideoCapture cap("f:/video for test/hassan_1.avi"); // open the default camera
if(!cap.isOpened())  // check if we succeeded
  return -1;
bool stop(false);
Mat edges;
namedWindow("edges",1);
while(!stop)
{
    Mat frame;
    cap >> frame; // get a new frame from camera
    int fps=(int) cap.get(CV_CAP_PROP_FPS);
    int totalframes=(int) cap.get(CV_CAP_PROP_FRAME_COUNT);
    double delay=1000/fps;
    cvtColor(frame, edges, CV_BGR2GRAY);
    GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
    Canny(edges, edges, 0, 30, 3);
    imshow("edges", edges);


    k++;
    if (waitKey(delay)>=0)
    stop= true;
   }
cap.release();
cout<<"number of frames that do processing on them= "<<k<<endl;
getch();
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
edit retag flag offensive close merge delete

Comments

1
  1. IMHO fps, totalframes and delay are constant, so no need to put them in the loop;
  2. your error is linked to learning.exe, I do not think that this is all your code... Give us the code linked to the error
thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-01-14 05:02:50 -0600 )edit

i create project in vs2012 with opencv learning name.

h_mahmoodian gravatar imageh_mahmoodian ( 2015-01-14 06:08:09 -0600 )edit

Are you pressing a key? it may be linked to the waitKey... just do a if (waitKey(delay)>=0) { cout << "stop\n"; stop = true; }

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-01-14 07:03:16 -0600 )edit

I do not press any key ,but when run my code i see error at 804 th frame

h_mahmoodian gravatar imageh_mahmoodian ( 2015-01-14 10:34:58 -0600 )edit

I just ran your code on a video with total frames 2224 but only 2221 were processed. I tried a few things to reproduce or fix the error rate however I always got the same result. An alternative to make sure you are getting the right frame rather than cap >> frame, use cap.set(CV_CAP_PROP_POS_FRAMES, k); then use cap.retrieve(frame); this way you are telling the video to go to position k and retrieve the frame.

Messina Vision Systems gravatar imageMessina Vision Systems ( 2015-01-14 17:48:40 -0600 )edit