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


在python中, cv2.polylines和cv2.fillpoly对于参数pts的要求是一致的,而在c++中是不一致的。

python中,传参方式为:
image = np.zeros(512, 512, np.uint8)
pts = []
pts.append([x, y]) # 添加顶点
pts = pts.reshape((-1, 1, 2))
# 这里 reshape 的第一个参数为-1, 表明这一维的长度是根据后面的维度的计算出来的。
# OpenCV中需要先将多边形的顶点坐标变成顶点数×1×2维的矩阵,再来绘制

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

很明显可以看出,ploylines函数和fillPoly函数传参相同

对于c++而言,代码则为:
`
Mat image = Mat::ones(512, 512, CV_8UC3)
vector pts;
pts.push_back(Point(x, y));

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

vector<vector<Point>> ppts;
ppts.push_back(pts);

fillPoly(image , ppts, (255, 255, 255));

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM