当您在 OpenCV C++ 接口中有可用的运算符时,无需显式调用 add 函数。这是平均指定帧数的方法。
void main()
{
cv::VideoCapture cap(-1);
if(!cap.isOpened())
{
cout<<"Capture Not Opened"<<endl; return;
}
//Number of frames to take average of
const int count = 10;
const int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
const int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
cv::Mat frame, frame32f;
cv::Mat resultframe = cv::Mat::zeros(height,width,CV_32FC3);
for(int i=0; i<count; i++)
{
cap>>frame;
if(frame.empty())
{
cout<<"Capture Finished"<<endl; break;
}
//Convert the input frame to float, without any scaling
frame.convertTo(frame32f,CV_32FC3);
//Add the captured image to the result.
resultframe += frame32f;
}
//Average the frame values.
resultframe *= (1.0/count);
/*
* Result frame is of float data type
* Scale the values from 0.0 to 1.0 to visualize the image.
*/
resultframe /= 255.0f;
cv::imshow("Average",resultframe);
cv::waitKey();
}
创建矩阵时始终指定完整类型,例如,CV_32FC3
而不是 only CV_32F
。