c++ - OpenCV 视频处理帧率下降

标签 c++ opencv

我正在使用 OpenCV 2.4.6 处理视频。当我运行视频处理算法时,视频可以正确处理,但是处理后显示每一帧时会出现明显的延迟。例如,10秒的视频在添加处理后变成18秒。我需要实时进行此处理,如何改进该流程?谢谢

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

最佳答案

为什么要执行waitKey(30)? 此调用将阻塞至少 30 毫秒,剩下的处理将在 3 毫秒内完成(假设是 30 fps 的视频)。 您是否尝试过降低该值,或者干脆将其删除?

关于c++ - OpenCV 视频处理帧率下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20559282/

相关文章:

opencv - 使用 arduino、处理和 opencv 进行面部跟踪的 Arduino 伺服

c++ - OpenCV VLFeat Slic 函数调用

c++ - 如何在 C++ 中从我的 Tracker 类创建一个 Tracker 对象?

c++ - 为什么我在 MinGW 中不需要标志 -lm 但在 Linux 中我明确需要它?

c++ - 这是数据对齐崩溃吗? (可能涉及堆栈错位、XNAMath、Visual Studio 2103)

python - 沿y轴逐行旋转(对齐)图像

c++ - 带有 cvfilter2d 的 opencv。我越来越黑了图片

C++ 将 const 存储到临时对象

c++ - if(isspace()) 语句不工作 C++

python - 使用网络摄像头 OpenCV 将多条线连接到屏幕中心