bitwise_or Opencv Exception

bitwise_or Opencv Exception

本文关键字:Exception Opencv or bitwise      更新时间:2023-10-16

我正在尝试在我的图像上应用骨架化bitwise_or但它在 opencv 的功能上引发异常

    cv::threshold(input, input, 127, 255, cv::THRESH_BINARY);
    cv::Mat skel(input.size(), CV_8UC1, cv::Scalar(0));
    cv::Mat temp(input.size(), CV_8UC1);
    cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3));
    bool done;
    do
    {
        cv::morphologyEx(input, temp, cv::MORPH_OPEN, element);
        cv::bitwise_not(temp, temp);
        cv::bitwise_and(input, temp, temp);
        cv::bitwise_or(skel, temp, skel);
        cv::erode(input, input, element);
        double max;
        cv::minMaxLoc(input, 0, &max);
        done = (max == 0);
    } while (!done);

抛出bitwise_or例外情况是

C:buildsmaster_PackSlave-win32-vc12-staticopencvmodulescoresrcarithm.cpp:1573: error: (-209) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function cv::binary_op

代码来源:http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/

如果input图像不是CV_8UC1,我可以重现此错误。请确保input灰度图像。

你需要提供一些标量。例如:

bitwise_or(mat1, Scalar(0,0,0,255), mat2);

在您的代码中,temp Mat 中没有值(因为您只使用大小创建了它)。尝试类似操作:

cv::Mat temp(input.size(), CV_8UC1, cv::Scalar(0));

这将创建一个用零填充的矩阵,您可以将其用作 bitwise_or 函数的标量。