- 提取轮廓
std::vector<std::vector<cv::Point> > contours;
cv::Mat image = cv::imread(“”);
只检索最外面的轮廓且用端点代替直线段(水平,竖直、对角线)
cv::findContours(image, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
// 另一种方式
vector<cv::Vec4i> hierarchy; //next,pre,child,parent
cv::findContours(mask, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
- 求轮廓的外接矩形
cv::Rect rect = cv::boundingRect(contours[0]);
- 判断点是否在轮廓内
cv::Point point = cv::Point(2, 2);
false只判断,true返回具体距离
double a = cv::pointPolygonTest(contours[0], point, false)