【opencv编程基础】fillpoly和polylines函数的理解

前言

最近编程实现车辆边界接地线算法时,遇到fillpoly函数的使用问题,特此记录。

具体问题

调试时定位的错误代码:

            cv::Mat mask_parking = cv::Mat::zeros(segImg.size(), CV_8UC1);
            cv::fillPoly(mask_parking, cur_parking, cv::Scalar(255));

其中cur_parking数据类型是std::vector<cv::Point>

出现错误:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(3.4.5) /tmp/tmp.fAVojfHTaz/modules/core/src/matrix_wrap.cpp:50: error: (-215:Assertion failed) i < 0 in function 'getMat_'

Aborted (core dumped)

查看资料发现是fillpoly函数的参数类型有问题;

修改后的代码:

            cv::Mat mask_parking = cv::Mat::zeros(segImg.size(), CV_8UC1);
            std::cout << __FUNCTION__ << __LINE__ << ", fillPoly mask_parking start..." << std::endl;
            std::vector<std::vector<cv::Point>> pcur_parking;
            pcur_parking.emplace_back(cur_parking);
            cv::fillPoly(mask_parking, pcur_parking, cv::Scalar(255));

错误解决。

其他

c++ opencv fillpoly函数的注意事项博文中记录,在python中, cv2.polylines和cv2.fillpoly对于参数pts的要求是一致的,而在c++中是不一致的。

python:

# --------------画多边形---------------------
image = cv2.polylines(image , [pts], True, (255, 255, 255))
##-------------填充多边形---------------------
image = cv2.fillPoly(image , [pts], (255, 255, 255))

c++:

polylines(image , pts, true, (255, 255, 255));

vector<vector<Point>> ppts;
ppts.push_back(pts);
fillPoly(image , ppts, (255, 255, 255));

由此得出结论,很明显,c++中polylines函数和fillPoly函数所需参数不同,fillPoly函数需要二维嵌套vector<vector> ppts;,否则无法正常使用。若使用vector pts;,会导致程序中断,但不会在编译器中报错。

 

参考

1. c++ opencv fillpoly函数的注意事项

posted on 2025-02-14 16:31  鹅要长大  阅读(182)  评论(0)    收藏  举报

导航