0

我正在尝试使用 cv 命名空间(Qt4.7)在 openCV 2.2 中实现背景减法。我有以下代码可以很好地编译,但是在运行程序时会中断,因为一个垫子不等于另一个垫子,但我找不到它在哪里,我目前正在通过 API 参考来尝试找到它。

           cvtColor( mcolImage, mcolImage, CV_BGR2RGB);
           cvtColor( mcolImage, gscaleImage, CV_RGB2GRAY);

           acc = Mat(Size(440,320), CV_32FC3);
           accSQ = Mat(Size(440,320), CV_32FC3);

           //we accumulate into a Mat to get an frames average
           Mat avg;
           accumulateWeighted(gscaleImage, acc, 3.0, Mat());
           accumulateSquare(gscaleImage, accSQ, Mat());
           multiply(acc, acc, avg, 1);

           Mat sigma, sigmaSQRT;
           subtract(accSQ, avg, sigmaSQRT, Mat());
           sqrt(sigmaSQRT, sigma); //Holds the standard deviation

           Mat fgImage; //hold the foreground image
           cv::absdiff(avg,gscaleImage, fgImage);

           //GaussianBlur(gscaleImage, gscaleImage, Size(7,7), 2, 2 );

           Mat buff ;
           //convert to black and white
           threshold(fgImage, buff, 75, THRESH_BINARY, 100);

           dilate(buff, buff, Mat(3, 3, CV_8UC1), Point(-1, -1), 1, BORDER_CONSTANT, Scalar(1.0, 1.0, 1.0, 0));
           erode(buff, buff, Mat(3, 3, CV_8UC1), Point(-1, -1), 1, BORDER_CONSTANT, Scalar(1.0, 1.0, 1.0, 0));

           //rectangle(gscaleImage, cvPoint(100, 300), cvPoint(200, 100), cvScalar(255, 255, 255, 0), 1);

           QImage colImagetmp((uchar*)mcolImage.data, mcolImage.cols, mcolImage.rows, mcolImage.step,
                              QImage::Format_RGB888 ); //Colour

           QImage gscaleImagetmp ((uchar*)gscaleImage.data, gscaleImage.cols, gscaleImage.rows, gscaleImage.step,
                              QImage::Format_Indexed8); //Greyscale. I hope

           QImage bwImagetmp((uchar*)buff.data, buff.cols, buff.rows, buff.step,
                            QImage::Format_Indexed8);

           //Setup a colour table for the greyscale image
           QVector<QRgb> colorTable;
           for (int i = 0; i < 256; i++) colorTable.push_back(qRgb(i, i, i));
           bwImagetmp.setColorTable(colorTable);
           gscaleImagetmp.setColorTable(colorTable);


           ui.intDisplay->setPixmap(QPixmap::fromImage(bwImagetmp));
           ui.bwDisplay->setPixmap(QPixmap::fromImage(gscaleImagetmp));
           ui.colDisplay->setPixmap( QPixmap::fromImage(colImagetmp ));

感谢您的帮助。

编辑:

浏览完代码后,我发现这absdiff(avg, gscaleImage, fgImage);是程序崩溃的地方。我认为它可能会在第二个参数上崩溃,但不确定。

4

1 回答 1

0

我通过声明一个新的临时 Mat 并专门转换它(使用 avg.convert() )以匹配 gscaleImage 类型和大小来解决它(我认为)。

于 2011-03-22T16:15:52.343 回答