OpenCV(C++) 基础(六)-- 绘制轮廓与分割修复

1.寻找轮廓: findContours()

void findContours(src, contours, hierarchy, mode, method, offset);
// hierarchy: 图像拓扑信息,可选
// mode: 轮廓检索模式,RETR_EXTERNAL(外轮廓), RETR_LIST(所有轮廓存入list), RETR_CCOMP(双层), RETR_TREE(网状)
// method: CHAIN_APPROX_NONE(获取轮廓每个像素), CHAIN_APPROX_SIMPLE(压缩水平、垂直、对角方向), CHIAN_APPROX_TC89_L1(一种逼近算法)
// offset: Point类型,偏移量
// 常与drawContours配合使用
// case:
vector<vector<Point>>contours;
findContours(src, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

2.绘制轮廓: drawContours()

void drawContours(src, contours, contourIdx, color, thickness=1, lineType=8, hierarchy, maxLevel=INT_MAX, offset);
// lineType: 4 / 8(四连通/八连通线型), LINE_AA(抗锯齿线型)
// case:
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findCountours(src, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
int idx = 0;
for(; idx>=0; idx=hierarchy[idx[0]]){
    Scalar color(rand()&255, rand()&255, rand()&255);
    drawContours(src, contours, idx, color, FIILED, 8, hierarchy);
}

3.凸包检测: convexHull()

void convexHull(points, hull, clockwise=false, returnPoints=false);
// hull: 凸包, clockwise: 顺时针方向标识符
// case:
vector<Point>points;
vector<Point>hull;
convexHull(Mat(points), hull, true);
Mat image(600, 600, CV_8UC3);
image = Scalar::all(0);
Point point_0 = points[hull[hull.size()-1]];
for(int i=0; i<hull.size(); i++){
    Point point = points[hull[i]];
    line(image, point, Scalar(255, 255, 255), 2, LINE_AA);
    point_0 = point;
}

4.多边形包围轮廓

// 返回外部矩形边界:boundingRect()
Rect boundingRect(points); //输入为二维点集,vector或Mat

// 寻找最小包围矩形:minAreaRect(), 矩形可旋转
RotatedRect minAreaRect(points);

// 寻找最小包围圆形:minEnclosingCircle()
void minEnclosingCircle(points, Point2f& center, radius);

5.图像的矩

// 计算轮廓面积
double contourArea(contour, oriented=false);
// oriented: 为true返回带符号的面积值,正负由方向决定

6.分水岭算法:基于形态学的分割方法,是一个迭代标注的过程,排序+淹没

// 图像输入前需大致勾画标记期望进行分割的区域
void watershed(src, markers);
// markers: 存放函数调用后的结果

7.图像修补: inpaint()

void inpaint(src, inpaintMask, dst, inpaintRadius, flags);
// inpaintMask: 修复掩膜,8位单通道图像,非零像素表示需要修补的区域
// inpaintRadius: 需要修复点的圆形邻域
// flags:修补方法的标识符,INPAINT_NS, INPAINT_TELEA
// case:
inpaintMask = Mat::zeros(src,size(), CV_8U);
inpaint(src, inpaintMask, dst, 3, INPAINT_TELEA);

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值