accumweight throws cv:异常错误

时间:2014-06-04 05:31:39

标签: windows opencv visual-studio-2012

我是OpenCV的新手并且试图找到轮廓并在它们上绘制矩形,这里是我的代码,但是当涉及到cumulativeweighted()时它会抛出cv :: Exception。 我尝试通过转换为CV_32FC3然后使用累计加权来查找平均值来制作src(原始图像)和dst(背景)。

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
cout << "\nThis is a Example to implement CAMSHIFT to detect multiple motion objects.\n";
}

Rect rect;
VideoCapture capture;
Mat currentFrame, currentFrame_grey, differenceImg, oldFrame_grey,background;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

bool first = true;

int main(int argc, char* argv[])
{
//Create a new movie capture object.
capture.open(0);

if(!capture.isOpened())
{
    //error in opening the video input
    cerr << "Unable to open video file: " /*<< videoFilename*/ << endl;
    exit(EXIT_FAILURE);
}   

//capture current frame from webcam
capture >> currentFrame;

//Size of the image.
CvSize imgSize;
imgSize.width = currentFrame.size().width; //img.size().width
imgSize.height = currentFrame.size().height; ////img.size().height

//Images to use in the program.
currentFrame_grey.create( imgSize, IPL_DEPTH_8U);//image.create().  

while(1)
{
    capture >> currentFrame;//VideoCapture& VideoCapture::operator>>(Mat& image)                

    //Convert the image to grayscale.
    cvtColor(currentFrame,currentFrame_grey,CV_RGB2GRAY);//cvtColor()

    currentFrame.convertTo(currentFrame,CV_32FC3);
    background = Mat::zeros(currentFrame.size(), CV_32FC3);
    accumulateWeighted(currentFrame,background,1.0,NULL);

    imshow("Background",background);

    if(first) //Capturing Background for the first time
    {
        differenceImg = currentFrame_grey.clone();//img1 = img.clone()
        oldFrame_grey = currentFrame_grey.clone();//img2 = img.clone()
        convertScaleAbs(currentFrame_grey, oldFrame_grey, 1.0, 0.0);//convertscaleabs()
        first = false;
        continue;
    }

    //Minus the current frame from the moving average.
    absdiff(oldFrame_grey,currentFrame_grey,differenceImg);//absDiff()

    //bluring the differnece image
    blur(differenceImg, differenceImg, imgSize);//blur()

    //apply threshold to discard small unwanted movements
    threshold(differenceImg, differenceImg, 25, 255, CV_THRESH_BINARY);//threshold()

    //find contours
    findContours(differenceImg,contours,hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); //findcontours()

    //draw bounding box around each contour
    //for(; contours! = 0; contours = contours->h_next)
    for(int i = 0; i < contours.size(); i++)
    {
        rect = boundingRect(contours[i]); //extract bounding box for current contour
        //drawing rectangle
        rectangle(currentFrame, cvPoint(rect.x, rect.y), cvPoint(rect.x+rect.width, rect.y+rect.height), cvScalar(0, 0, 255, 0), 2, 8, 0);                 
    }       

    //New Background
    convertScaleAbs(currentFrame_grey, oldFrame_grey, 1.0, 0.0);

    //display colour image with bounding box
    imshow("Output Image", currentFrame);//imshow()

    //display threshold image
    imshow("Difference image", differenceImg);//imshow()

    //clear memory and contours
    //cvClearMemStorage( storage );
    //contours = 0;
    contours.clear();

    //background = currentFrame;

    //press Esc to exit
    char c = cvWaitKey(33);
    if( c == 27 ) break;

}

// Destroy All Windows.
destroyAllWindows();

return 0;
}

请帮助解决此问题。

1 个答案:

答案 0 :(得分:0)

你可能想要问RTFM之前。

所以,你在调用addWeighted

时错过了alpha param和dst Mat
Mat dst; 
accumulateWeighted(currentFrame, 0.5 background, 0.5, 0, dst);

另外,不知道,整件事应该实现什么。在对它进行区分之前加上当前帧对我没有任何意义。

如果您计划进行背景分离,请将其全部丢弃,并使用内置backgroundsubtractors之一