Ask Your Question
0

computing average image of N webcam images crashes sometimes

asked 2015-08-11 03:28:40 -0500

interference gravatar image

Hey guys, I wanted to write a program which takes N live webcam images, calculates an averaged-image out of it and saves the average image as a jpg. I'm passing the filename and the number N of images to be averaged as parameters. First I thought everything is working fine with my code, but sometimes the program crashes during runtime (I'm calling the exe via LabVIEW). I'm not changing N, only the filename-number is incremented by 1 for each call, so I don't think that's the issue. It crashes about 1 time in 25 calls but it's nor really reproducible when. With crashing I mean the window freezes, and is greyed out by Windows and I get the promt that the program isn't responding.

Here is my code:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    string outputfilename;
    int NMean; // number of images to take mean values of

    if (argc == 2)
    {
        outputfilename = argv[1];
        NMean = 100;
    }

    else if(argc > 2)
    {
        outputfilename = argv[1];
        istringstream ss(argv[2]);
        ss >> NMean;
    }

    else
    {
        outputfilename = "newframe.jpg";
        NMean = 100;

    }

    VideoCapture cap(0);

    if (!cap.isOpened())
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    }

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1024);

    namedWindow("WebcamImage",CV_WINDOW_AUTOSIZE);

    Mat frame;
    Mat meanImage(1024, 1280, CV_32FC3, cv::Scalar(0));

    for (int i=0; i<NMean; i++)
    {

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

        imshow("WebcamImage", frame);
        accumulate(frame, meanImage);

       if (waitKey(30) == 27)
       {
            cout << "esc key is pressed by user" << endl;
            break;
       }
    }

    meanImage = meanImage / NMean;
    meanImage.convertTo(meanImage,CV_8UC3);
    imwrite(outputfilename.c_str(),meanImage);

    cap.release();
    destroyWindow("WebcamImage");

    return 0;

}

I noticed that during a crash the averaged image is still saved. So the error must occur after the last imwrite (I also tried to put destroyWindow in front of break; but it didn't help).

What could be the issue? Thanks a lot and best regards!

edit retag flag offensive close merge delete

Comments

1

is there any chance, your frame is empty() ? imshow() will break then.

berak gravatar imageberak ( 2015-08-11 04:26:02 -0500 )edit

like @berak said, add enough checks to ensure every step is performed on valid data. This can be done by adding a line like if(frame.empty()){ continue; } which will just skipp the misread frame. This can happen due to encoding errors in the video capture.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-08-11 04:58:28 -0500 )edit

I have tried your program and I have changed some line code. I have test it using a function and there is no problem :

double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);

cout << "Frame size : " << dWidth << " x " << dHeight << endl;

cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1024);
dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);

namedWindow("WebcamImage",CV_WINDOW_AUTOSIZE);

Mat frame;
Mat meanImage(dHeight, dWidth, CV_32FC3, cv::Scalar(0));

I must say that first and second frame are wrong with my webcam

LBerger gravatar imageLBerger ( 2015-08-11 05:05:33 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-08-25 02:12:04 -0500

interference gravatar image

Hey, thanks for your answers and sorry for my late reply! I added your hint if(frame.empty()){ continue; } but it didnt't help. I noticed that the program always crashes on cap.release(); Recently I found out that there was running another service on my PC called "arcsoft connect daemon" which came somehow installed with my webcam. After turning it off there are no problems anymore. I was able to call the program about 16.000 times without an error ;)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-08-11 03:28:40 -0500

Seen: 191 times

Last updated: Aug 25 '15