c++ - OpenCV C++ 断言

标签 c++ opencv

我一直在为 opencv 项目中的一些断言而苦苦挣扎。

首先,我正在转换 Mat 对象以确定它们的类型:

gray_im.convertTo(gray_im, CV_8U);
diff_im.convertTo(diff_im, CV_8U);

然后我做一个减法。这一行是我得到断言的地方:

diff_im = gray_im - prev_im;

这是断言:

OpenCV Error: Bad argument (When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified) in arithm_op, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/arithm.cpp, line 1313

当我打印有关我在减法中使用的图像的信息时;

diff_im. type: 5 rows: 600 cols 800 
gray_im. type: 5 rows: 600 cols 800 
prev_im. type: 0 rows: 600 cols 800 

我认为我通过将其转换为 CV_8U 来明确指定输出数组(如果我是正确的,这里 diff_im 是输出数组,对吗?)。另外,当我在运行时打印 diff_im 的类型信息时,它说它是“5”,这意味着我已经明确指定了“diff_im”的类型。

我错了吗?有什么建议吗?

OpenCV 版本:2.4.8 提前致谢。

最佳答案

gray_im 的类型为 5,prev_im 的类型为 0。您可能忘记正确初始化 prev_im,即:

prev_im = cv::Mat::zeros(gray_im.size(), gray_im.type());

更新 #1:

嗯,dst = src1 - src2 等价于subtract(dst, src1, dst)。参数dstOutputArray,仅用于输出函数参数。 dst 的类型不能由 operator- 定义,只有 subtract() 为其参数 dtype 提供了可能性, 请参阅 here .

如果未给出dtype 并且两个输入参数都是数组,则src1.type() 必须等于src2.type(),请参阅 here .

所以你应该如下覆盖operator-:

cv::Mat src1(600, 800, CV_8U);
cv::Mat src2(600, 800, CV_32F);
cv::Mat dst;

//dst = src1 - src2; // --> this will give the same assert
cv::subtract(src1, src2, dst, cv::noArray(), CV_32F); // type of dst is CV_32F

关于c++ - OpenCV C++ 断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34996190/

相关文章:

opencv - 从 canny 计算非定向边

android - 在 Android 上为 OpenCV 设置相机场景模式

c++ - ComputeLibrary 数据类型模板

c++ - lambda 捕获列表中的 weak_ptr、shared_ptr 和 std::queue<T,Container>::emplace

c++ - Delphi 中的动态数组和内存管理

c++ - 如何覆盖 exit(),可能是通过抛出异常

c++ - Visual Studio 跳过代码行

c++ - 我只想获得高质量的特征点

java - 请有人帮助将 opencv 代码转换为 javacv?

opencv - 筛选和冲浪 : Set OPENCV_ENABLE_NONFREE CMake ==> Solution OpenCV 3 & OpenCV 4