OpenCV中的drawContours使用
来源:互联网 发布:淘宝网登陆不了 编辑:程序博客网 时间:2024/05/16 09:42
一、Canny检测轮廓
1
2
3
4
5
6
7
8
9
10
11
12
13
int
main()
{
Mat I=imread(
"../cat.png"
);
cvtColor(I,I,CV_BGR2GRAY);
Mat contours;
Canny(I,contours,125,350);
threshold(contours,contours,128,255,THRESH_BINARY);
namedWindow(
"Canny"
);
imshow(
"Canny"
,contours);
waitKey();
return
0;
}
二、直线检测
1
void
HoughLines(InputArray image, OutputArray lines,
double
rho,
double
theta,
int
threshold,
double
srn=0,
double
stn=0 );
1
void
HoughLinesP(InputArray image, OutputArray lines,
double
rho,
double
theta,
int
threshold,
double
minLineLength=0,
double
maxLineGap=0 );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int
main()
{
Mat image=imread(
"../car.png"
);
Mat I;
cvtColor(image,I,CV_BGR2GRAY);
Mat contours;
Canny(I,contours,125,350);
threshold(contours,contours,128,255,THRESH_BINARY);
vector<Vec4i> lines;
// 检测直线,最小投票为90,线条不短于50,间隙不小于10
HoughLinesP(contours,lines,1,CV_PI/180,80,50,10);
drawDetectLines(image,lines,Scalar(0,255,0));
namedWindow(
"Lines"
);
imshow(
"Lines"
,image);
waitKey();
return
0;
}
1
2
3
4
5
6
7
8
9
10
11
12
void
drawDetectLines(Mat& image,
const
vector<Vec4i>& lines,Scalar & color)
{
// 将检测到的直线在图上画出来
vector<Vec4i>::const_iterator it=lines.begin();
while
(it!=lines.end())
{
Point pt1((*it)[0],(*it)[1]);
Point pt2((*it)[2],(*it)[3]);
line(image,pt1,pt2,color,2);
// 线条宽度设置为2
++it;
}
}
三、轮廓的提取与描述
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int
main()
{
using
namespace
cv;
Mat image=imread(
"../shape.png"
);
cvtColor(image,image,CV_BGR2GRAY);
vector<vector<Point>> contours;
// find
findContours(image,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
// draw
Mat result(image.size(),CV_8U,Scalar(0));
drawContours(result,contours,-1,Scalar(255),2);
namedWindow(
"contours"
);
imshow(
"contours"
,result);
waitKey();
return
0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 轮廓表示为一个矩形
Rect r = boundingRect(Mat(contours[0]));
rectangle(result, r, Scalar(255), 2);
// 轮廓表示为一个圆
float
radius;
Point2f center;
minEnclosingCircle(Mat(contours[1]), center, radius);
circle(result, Point(center),
static_cast
<
int
>(radius), Scalar(255), 2);
// 轮廓表示为一个多边形
vector<Point> poly;
approxPolyDP(Mat(contours[2]), poly, 5,
true
);
vector<Point>::const_iterator itp = poly.begin();
while
(itp != (poly.end() - 1))
{
line(result, *itp, *(itp + 1), Scalar(255), 2);
++itp;
}
line(result, *itp, *(poly.begin()), Scalar(255), 2);
// 轮廓表示为凸多边形
vector<Point> hull;
convexHull(Mat(contours[3]), hull);
vector<Point>::const_iterator ith = hull.begin();
while
(ith != (hull.end() - 1))
{
line(result, *ith, *(ith + 1), Scalar(255), 2);
++ith;
}
line(result, *ith, *(hull.begin()), Scalar(255), 2);
0 0
- OpenCV中的drawContours使用
- OpenCV基础优化之使用drawContours
- opencv轮廓检测之 drawContours 函数 使用样例
- opencv开发中findcontour和drawcontours的使用
- OpenCV drawContours 内轮廓
- Opencv drawContours函数
- opencv寻找轮廓2--drawContours
- OpenCV Tutorial: 輪廓(findContours、drawContours)
- [OpenCV]绘制填充轮廓drawContours
- opencv findContours和drawContours使用方法
- findContours和drawContours的使用
- OpenCV——绘制填充轮廓drawContours
- OPENCV轮廓提取findContours和drawContours
- Opencv drawContours函数用于绘制和填充
- opencv中findContours 和drawContours画图函数
- OPENCV轮廓提取findContours和drawContours
- OPENCV轮廓提取findContours和drawContours
- OpenCV学习笔记(23)findContours与drawContours
- 一起来学springSecurity安全框架(二)
- QGIS, Open Source GIS & R
- java之中的四种访问权限和子类如何继承父类特性
- android apk汉化,及各种apktool bug汇总
- Android 百度地图学习笔记1
- OpenCV中的drawContours使用
- android拍照获得图片URI为空的处理方法
- 使用Object-C实现23种设计模式之适配器模式
- mysql更新时的safe_mode
- 【自定义view系列】View的layout过程
- IL指令详细(转自博客园【Zery】)
- STL next_permutation and prev_permutation
- 信息检索与数据挖掘的常用加权技术。
- 【自定义View系列】View的draw过程