c++ - C++ 中的 OpenCV fillConvexPoly 函数抛出异常

标签 c++ opencv

我正在尝试使用 fillConvexPoly 函数在 mask 中填充三角形。 但是我收到以下错误。

OpenCV Error: Assertion failed (points.checkVector(2, CV_32S) >= 0) in fillConvexPoly, file /home/iris/Downloads/opencv-3.1.0/modules/imgproc/src/drawing.cpp, line 2256
terminate called after throwing an instance of 'cv::Exception'
 what():  /home/iris/Downloads/opencv-3.1.0/modules/imgproc/src/drawing.cpp:2256: error: (-215) points.checkVector(2, CV_32S) >= 0 in function fillConvexPoly

我是这样调用函数的,

cv::Mat mask = cv::Mat::zeros(r2.size(), CV_32FC3);
cv::fillConvexPoly(mask, trOutCroppedInt, cv::Scalar(1.0, 1.0, 1.0), 16, 0);

trOutCroppedInt 是这样定义的,

std::vector<cv::Point> trOutCroppedInt

然后我在 vector 中推 3 个点,

[83, 46; 0, 48; 39, 0]

我应该如何纠正这个错误?

最佳答案

遇到points.checkVector(2, CV_32S) >= 0)时

当数据类型比 CV_32S 更复杂且维度大于二时,可能会出现此错误,例如所有数据类型如 vector<Point2f>会造成问题。结果我们可以使用 fillConvexpoly按照以下步骤:

<强>1。使用

读取图像
cv::Mat src=cv::imread("what/ever/directory");

<强>2。确定点 您必须确定您的点,如下图所示

enter image description here

因此,我们针对这一点的代码是:

vector<cv::Point> point;
point.push_back(Point(163,146));  //point1
point.push_back(Point(100,148));  //point2
point.push_back(Point(100,110));  //point3
point.push_back(Point(139,110));  //point4

3.使用cv::fillConvexPoly功能

考虑图像 src并在此图像上绘制一个多边形((带点)),然后代码如下:

   cv::fillConvexPoly(src,               //Image to be drawn on
                      point,             //C-Style array of points
                      Scalar(255, 0, 0), //Color , BGR form
                      CV_AA,             // connectedness, 4 or 8
                      0);                // Bits of radius to treat as fraction

(所以输出图像如下:before:left side - after:right side)

enter image description here

关于c++ - C++ 中的 OpenCV fillConvexPoly 函数抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40284369/

相关文章:

c++ - 架构 arm64 的 undefined symbol : "cv::String::deallocate()"

c++ - MOG 和 GMM 算法之间有区别吗?或者是一样的吗?

c++ - opencv FLANN radiusSearch 给出不好的结果

python - 如何在 OpenCV 中找到 T 形的角度

c++ - 新手 - c++ 中的矩阵加法实现

c++ - 在自定义 boost 日志格式函数中格式化范围属性

C++ 类型将基础对象转换为派生对象

c++ - 如何在 C++ 中设置定时器?

javascript - 后端带有 C++ 类的 Electron nodejs

matlab - 用于图像处理/计算机视觉的良好且兼容的网络摄像头?