mohikhsan
mohikhsan

Reputation: 1538

OpenCV Video Processing Frame Rate Drop

I'm processing a video using OpenCV 2.4.6. When I run my video processing algorithms the video is processed properly, however there is significant delay when displaying each frame after the processing. For example, a 10 second video becomes 18 seconds after the processing is added. I need to have this processing for real-time, how can I improve the process? Thanks

Mat preProcess(Mat source){
    Mat grad_x, grad_y, grad_dif;
    Mat abs_grad_x, abs_grad_y;
    Mat src_gray, float_gray;
    Mat temp;

    GaussianBlur(source, grad_x, Size(1,1),0,0,BORDER_DEFAULT);
    GaussianBlur(source, grad_y, Size(3,3),0,0,BORDER_DEFAULT);
    grad_dif = grad_x - grad_y;
    cvtColor( grad_dif, src_gray, CV_RGB2GRAY );

    src_gray.convertTo(float_gray, CV_32F, 1.0/255.0);

    GaussianBlur(float_gray, grad_x, Size(0,0), 2, 2);
    abs_grad_x = float_gray - grad_x;

    GaussianBlur(abs_grad_x.mul(abs_grad_x), grad_x, Size(0,0), 10, 10);
    pow(grad_x, 0.5, grad_y);

    float_gray = grad_x / grad_y;
    normalize(float_gray, temp, 0, 255, NORM_MINMAX, CV_8UC1);
    threshold(temp,temp,13,255,3);

    return temp;
}

/** @function main */
int main( int argc, char** argv )
{
    VideoCapture cap("ijv4.mp4"); // open the video file for reading
    Rect myROI(160,20,350,380);

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video file" << endl;
        return -1;
    }

    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"
    Mat frame;
    bool bSuccess;

    while(1)
    {

        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;
        }


        grad = preProcess(frame(myROI));
        imshow("MyVideo", frame); //show the frame in "MyVideo" window
        imshow("processed",grad);

        if(waitKey(30) == 27) 
        {
            cout << "esc key is pressed by user" << endl; 
            break; 
        }
   }

   return 0;
}

Upvotes: 2

Views: 1857

Answers (3)

CAta.RAy
CAta.RAy

Reputation: 514

We can always divided it into threads. One for the visualization and one for the preprocess you are doing. I will recommend BOOST library but you can also use pthreads. Since you process the frame in a different thread you can continue with the next while the opencv GUI shows the processed frame in the window. This way you avoid "loosing" time with the waitKey().

Upvotes: 0

AstDev
AstDev

Reputation: 11

try to take 10 to 15 frames per second for processing it will increase your processing speed

Upvotes: -1

Hannes Ovr&#233;n
Hannes Ovr&#233;n

Reputation: 21851

Why are you doing waitKey(30)? This call will block for at least 30 ms, leaving the rest of your processing to be done in 3ms (assuming a 30fps video). Have you tried lowering this value, or simply remove it?

Upvotes: 3

Related Questions