Cv :: add doesn't work in openCV

I'm trying to plot an average of 10 frames, so I tried:

 .....
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 ????? 
     ....
 }

      

}

any idea how i can solve this. thank you in advance

+1


source to share


1 answer


There is no need to call the add function explicitly when you have operators available in the OpenCV C ++ interface. This is how you can average the specified number of frames.

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();

}

      



Always specify the full type when creating matrices, for example CV_32FC3

, not only CV_32F

.

+2


source







All Articles