'copyTo'中的断言失败错误。检查图像大小和类型

时间:2018-06-16 19:04:01

标签: opencv

我试过在这个论坛上搜索,阅读文档和谷歌搜索。我无法找到问题的答案。

以下是我要做的事情的简要说明:

  1. 从网络摄像头中读取Mat对象frame(在父类OpenCVVideoWorker中声明为私有成员)。
  2. 使用Rect_对象ROIbox给出的位置和坐标在框架上绘制一个矩形。
  3. 感兴趣的区域使用inRange进行阈值处理。
  4. 感兴趣区域中的阈值图像将转换为RGB,并在与其取出的位置相同的位置复制回输出帧。
  5. 我使用Qt作为GUI和Visual Studio作为我的开发环境。 当我运行程序时,尽管在使用copyTo时确保图像的行,列和类型相同,但仍会出现“断言失败”错误。错误发生在frameROIProcessed.copyTo(outputFrame(ROIbox))(我定期在控制台输出中找到它)。

    你能帮我找到我错的地方吗?

    void OpenCVVideoWorker::processFrame() {
        frame.copyTo(outputFrame);
    
        if (ROIbox.width != 0 && ROIbox.height != 0) {
            rectangle(outputFrame, Point(ROIbox.x, ROIbox.y), cvPoint(ROIbox.x + ROIbox.width, ROIbox.y + ROIbox.height), Scalar(0, 0, 255), 1);
        }
    
        Mat frameROI = frame(ROIbox);
    
        if (frameROI.rows != 0 && frameROI.cols != 0) {
            Mat frameROIProcessed;
            cvtColor(frameROI, frameROIProcessed, COLOR_BGR2HSV);
            inRange(frameROIProcessed, Scalar(hueLow, saturationLow, brightnessLow), Scalar(hueHigh, saturationHigh, brightnessHigh), frameROIProcessed);
            cvtColor(frameROIProcessed, frameROIProcessed, COLOR_GRAY2BGR);
    
            qInfo("1) outputFrame(ROIbox)");
            qInfo(getImageType(outputFrame(ROIbox)).c_str());
            qInfo("2) frameROIProcessed");
            qInfo(getImageType(frameROIProcessed).c_str());
            qInfo("\n");
    
            frameROIProcessed.copyTo(outputFrame(ROIbox));
        }
        cvtColor(outputFrame, outputFrame, COLOR_BGR2RGB);
    }
    

    在一个线程中每16ms运行一次的函数中就像这样调用它:

    void OpenCVVideoWorker::receiveTimerTick() {    
        // Capture video from the webcam
        videoCapture.read(frame);
        if (frame.empty()) {
            qInfo("ERROR! blank frame grabbed");
            return;
        }
    
        // Process the captured frame
        processFrame();
    
        // Send the processed frame
        QImage outputImage((const unsigned char *) outputFrame.data, outputFrame.cols, outputFrame.rows, outputFrame.step, QImage::Format_RGB888);
        emit sendImage(outputImage);
    }
    

    我在frameROIProcessed.copyTo(outputFrame(ROIbox))行发现了以下错误:

    Initializing the worker in its constructor...
    Opening camera...
    1) outputFrame(ROIbox)
    8UC3 rows:4 cols:5
    2) frameROIProcessed
    8UC3 rows:4 cols:5
    
    
    1) outputFrame(ROIbox)
    8UC3 rows:27 cols:22
    2) frameROIProcessed
    8UC3 rows:27 cols:22
    
    
    OpenCV(4.0.0-pre) Error: Assertion failed (!fixedSize() || ((Mat*)obj)->size.operator()() == Size(_cols, _rows)) in cv::debug_build_guard::_OutputArray::create, file d:\applications\opencv\source\opencv-master\modules\core\src\matrix_wrap.cpp, line 1227
    OpenCV: terminate handler is called! The last OpenCV error is:
    OpenCV(4.0.0-pre) Error: Assertion failed (!fixedSize() || ((Mat*)obj)->size.operator()() == Size(_cols, _rows)) in cv::debug_build_guard::_OutputArray::create, file d:\applications\opencv\source\opencv-master\modules\core\src\matrix_wrap.cpp, line 1227
    

    更新:我尝试使用非多线程格式的相同代码,但它确实有用。现在我不确定为什么函数不是线程安全的,即使我执行以下操作(我仍然得到相同的错误,但我认为我确保只有一个函数的实例一次运行):

        if (isFrameBeingProcessed) {
            return;
        }
        isFrameBeingProcessed = true;
        frame.copyTo(outputFrame);
    
        if (ROIbox.width != 0 && ROIbox.height != 0) {
            rectangle(outputFrame, Point(ROIbox.x, ROIbox.y), cvPoint(ROIbox.x + ROIbox.width, ROIbox.y + ROIbox.height), Scalar(0, 0, 255), 1);
        }
    
        Mat frameROI = frame(ROIbox);
    
        if (frameROI.rows != 0 && frameROI.cols != 0) {
            Mat frameROIProcessed;
            cvtColor(frameROI, frameROIProcessed, COLOR_BGR2HSV);
            inRange(frameROIProcessed, Scalar(hueLow, saturationLow, brightnessLow), Scalar(hueHigh, saturationHigh, brightnessHigh), frameROIProcessed);
            cvtColor(frameROIProcessed, frameROIProcessed, COLOR_GRAY2BGR);
    
            qInfo("1) outputFrame(ROIbox)");
            qInfo(getImageType(outputFrame(ROIbox)).c_str());
            qInfo("2) frameROIProcessed");
            qInfo(getImageType(frameROIProcessed).c_str());
            qInfo((string("Box x: ")+to_string(ROIbox.x) + string(", y: ") + to_string(ROIbox.y) + string(", width: ") + to_string(ROIbox.width) + string(", height: ") + to_string(ROIbox.height)).c_str());
            qInfo((string("Mat width: ") + to_string(outputFrame(ROIbox).cols) + string(", height: ") + to_string(outputFrame(ROIbox).rows)).c_str());
            qInfo("\n");
            frameROIProcessed.copyTo(outputFrame(ROIbox));
        }
        cvtColor(outputFrame, outputFrame, COLOR_BGR2RGB);
        isFrameBeingProcessed = false;
    

1 个答案:

答案 0 :(得分:1)

我在每一步都在控制台上显示调试语句,并发现问题所在。问题是ROIbox不是线程安全的(感谢@zindarod和@Micka)。给出感兴趣区域框ROIbox的GUI位于不同的线程上,并且在帧的处理期间ROIbox改变大小。简单的解决方法是在函数Rect_<int> box = ROIbox;的开头添加processFrame(),并在函数中使用此复制的框。