目录
1.轮廓外接最大矩形boundingRect()
2.轮廓外接最小矩形minAreaRect()
3.轮廓外接多边形approxPolyDP()
Rect cv::boundingRect ( InputArray array )
示例代码:
//轮廓最大外接矩阵
void Contour_external_maxmatrix(Mat image) {
Mat gray,binary;
cvtColor(image,gray,COLOR_BGR2GRAY);//灰度化
GaussianBlur(gray,gray,Size(9,9),2,2);//滤波
threshold(gray,binary,170,255,THRESH_BINARY|THRESH_OTSU);//自适应二值化
//轮廓检测
vector> contours;//轮廓
vector hierarchy;//存放轮廓结构变量
findContours(binary,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());
//寻找轮廓的外接矩阵
for(int n=0;n
轮廓外接最大矩形的结果:
RotatedRect cv::minAreaRect ( InputArray points )
示例代码:
//轮廓最小外接矩阵
void Contour_external_minmatrix(Mat image){
Mat gray,binary;
cvtColor(image,gray,COLOR_BGR2GRAY);//灰度化
GaussianBlur(gray,gray,Size(9,9),2,2);//滤波
threshold(gray,binary,170,255,THRESH_BINARY|THRESH_OTSU);//自适应二值化
//轮廓检测
vector> contours;//轮廓
vector hierarchy;//存放轮廓结构变量
findContours(binary,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());
//寻找轮廓的外接矩阵
for(int n=0;n
轮廓外接最小矩形的结果:
void cv::approxPolyDP (InputArray curve,
OutputArray approxCurve,
double epsilon,
bool closed
)
示例代码:
void drawapp(Mat result,Mat img2){
for(int i=0;i(i);
Vec2i point2=result.at(0);
line(img2,point1,point2,Scalar(0,0,255,255),4,8,0);
break;
}
Vec2i point1=result.at(i);
Vec2i point2=result.at(i+1);
line(img2,point1,point2,Scalar(0,0,255,255),4,8,0);
}
}
//轮廓多边形拟合
void Contour_external_matrix(Mat image){
Mat gray,binary;
cvtColor(image,gray,COLOR_BGR2GRAY);//灰度化
// GaussianBlur(gray,gray,Size(9,9),2,2);//滤波
// threshold(gray,binary,170,255,THRESH_BINARY|THRESH_OTSU);//自适应二值化
//轮廓的发现与绘制
vector> contours;//轮廓
vector hierarchy;//存放轮廓结构变量
findContours(gray,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point());
//绘制多边形
for(int n=0;n
多边形拟合的结果: