OpenCV3对accumulateWeighted的断言失败

OpenCV3 Assertion failed on accumulateWeighted

本文关键字:断言 失败 accumulateWeighted OpenCV3      更新时间:2023-10-16

我试着做一个普通的背景,但是有些问题,因为它会导致应用程序崩溃

cv::Mat firstFrame;
cv::Mat averageBackground;
int frameCounter=0;
// this function is called for every frame of the camera
- (void)processImage:(Mat&)image; {
    cv::Mat diffFrame;
    cv::Mat currentFrame;
    cv::Mat colourCopy;
    cvtColor(image, currentFrame, COLOR_BGR2GRAY);
    averageBackground = cv::Mat::zeros(image.size(), CV_32FC3); 
    cv::accumulateWeighted(currentFrame, averageBackground, 0.01);

    cvtColor(image, colourCopy, COLOR_BGR2RGB);

我看到在崩溃日志

OpenCV Error: Assertion failed (_src.sameSize(_dst) && dcn == scn) in accumulateWeighted, file /Volumes/Linux/builds/precommit_ios/opencv/modules/imgproc/src/accum.cpp, line 1108
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Volumes/Linux/builds/precommit_ios/opencv/modules/imgproc/src/accum.cpp:1108: error: (-215) _src.sameSize(_dst) && dcn == scn in function accumulateWeighted

cv::accumulateWeighted中,输入和输出图像必须具有相同的通道数。在您的例子中,currentFrame只有一个通道,因为您之前做了COLOR_BGR2GRAY,而averageBackground有三个通道。

对于averageBackground = cv::Mat::zeros(image.size(), CV_32FC3);也要小心,因为在这一行中,您每次都初始化结果图像(因此您删除了允许您计算平均值的先前图像值)。您只需要在程序开头或任何地方初始化这个映像一次。