gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:47:13 28 4
gpt4 key购买 nike

我正在使用 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 毫秒内完成(假设视频为 30fps)。您是否尝试过降低此值,或干脆将其删除?

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com