【opencv編程基礎】fillpoly和polylines函數(shù)的理解
前言
最近編程實現(xiàn)車輛邊界接地線算法時,遇到fillpoly函數(shù)的使用問題,特此記錄。
具體問題
調試時定位的錯誤代碼:
cv::Mat mask_parking = cv::Mat::zeros(segImg.size(), CV_8UC1); cv::fillPoly(mask_parking, cur_parking, cv::Scalar(255));
其中cur_parking數(shù)據(jù)類型是std::vector<cv::Point>
出現(xiàn)錯誤:
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)
查看資料發(fā)現(xiàn)是fillpoly函數(shù)的參數(shù)類型有問題;
修改后的代碼:
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函數(shù)的注意事項博文中記錄,在python中, cv2.polylines和cv2.fillpoly對于參數(shù)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函數(shù)和fillPoly函數(shù)所需參數(shù)不同,fillPoly函數(shù)需要二維嵌套vector<vector> ppts;,否則無法正常使用。若使用vector pts;,會導致程序中斷,但不會在編譯器中報錯。
參考
1. c++ opencv fillpoly函數(shù)的注意事項;
完
各美其美,美美與共,不和他人作比較,不對他人有期待,不批判他人,不鉆牛角尖。
心正意誠,做自己該做的事情,做自己喜歡做的事情,安靜做一枚有思想的技術媛。
版權聲明,轉載請注明出處:http://www.rzrgm.cn/happyamyhope/
心正意誠,做自己該做的事情,做自己喜歡做的事情,安靜做一枚有思想的技術媛。
版權聲明,轉載請注明出處:http://www.rzrgm.cn/happyamyhope/
浙公網(wǎng)安備 33010602011771號