c++ - cv::add 在 openCV 中不起作用

标签 c++ opencv

我尝试建立 10 帧的平均值,所以我尝试了:

 .....
cv::Mat frame,outf,resultframe1, resultframe2;
VideoCapture cap(1);
cap>> frame;
resultframe1 = Mat::zeros(frame.rows,frame.cols,CV_32F);
resultframe2 = Mat::zeros(frame.rows,frame.cols,CV_32F);
while(waitKey(0) != 27}{
cap>> frame;
if ( waitKey(1) = 'm'){
for (  int j = 0 ; j <= 10 ; j++){  
cv::add(frame,resultframe1,resultframe2);// here crashes the program ????? 
     ....
 }

任何想法我该如何解决。 提前致谢

最佳答案

当您在 OpenCV C++ 接口(interface)中有可用的运算符时,无需显式调用 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 而不是仅 CV_32F

关于c++ - cv::add 在 openCV 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14198518/

相关文章:

c++ - Qt 表模型列覆盖

c++ - 无需读取整个图像的 OpenCV 图像尺寸

java - 如何在 java 中旋转缩放图像(使用 opencv)?

c++ - 使用 OpenCV 中的相机校准参数将 2D 点投影到 3D 空间

java - 使用 OpenCV 的 Android 光学字符识别

C++1z(?) 运行时大小的数组作为返回值

c++ - 如何在扩展监视器上显示 QLabel

python - 如何检测圆形腐 eclipse /膨胀

Qt 或 OpenCV : print out the codec of a video file

c++ - 你如何使用 std::not1 和 std::not2?