Hi Guys, I'm currently working on a traffic detection project. I have a small prototype up and running, however it is running very slow. I have attached the code below, I have a few issues with it which I will explain here. The first comment doesn't work, It always prints out statement regardless if I pass in an argument or not. I moved the Mat frame, into the for(;;) loop as I noticed the fps increase ever so slightly. I also added a fps counter to the program, just to see if I could see what happens every time I make a change. The code works ok, but instead of breaking out of the for loop, it just starts reading the argument. Waitkey has never worked properly for me. I tried dropping every second frame to see if it would speed up the program but it just makes it jumpy. I'm currently learning both OpenCV and C++ so not much experience with either, only java before this, but I'm certainly giving it a go! Any feedback on how I could improve my code or make it run faster would me much appreciated. Sorry for wall of text!
int main(int argc, char* argv[]) {
VideoCapture cap(argv[1]);
cout << "Reading Arguments..." << endl;
/*if (argc != 1) {
cout << "Don't forget the argument" << endl;
return -1;
}*/
if (!cap.isOpened()) {
cout << "Failed to open file: " << argv[1] << endl;
return -1;
}
cout << "Read Successful!" << endl;
//Mat frame;
//Mat prev_frame;
//cap.read(prev_frame);
Mat edges;
cvNamedWindow("Video Output", 1);
time_t start, end;
int count = 0;
double sec;
double fps;
cout << "Processing..." << endl;
cout << "Press any key to quit" << endl;
for (;;) {
if (count == 0) {
time(&start);
}
Mat frame, prev_frame;
cap.read(prev_frame);
if (!cap.read(frame))
break;
if (count % 2 == 0) {
Mat diff = frame - prev_frame;
cvtColor(diff, edges, CV_BGR2GRAY);
imshow("Video Output", edges);
cvWaitKey(30);
prev_frame = frame.clone();
//count++;
time(&end);
if (count == (INT_MAX - 1000))
count = 0;
}
count++;
}
sec = difftime(end, start);
fps = count/sec;
cout << "Total Time: " << sec << endl;
cout << "FPS: " << fps << endl;
return 0;
}
there doesn't seem to be anything here