【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函数的注意事项;
完
    各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
 
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号