OpenCV 中的 findContours 函数参数详解
OpenCV 中的 findContours 函数参数详解.
返回外部矩形边界
计算并返回指定点集最外面(up-right)的矩形边界
Rect boundingRect(InputArray points) //参数为输入的 std::vector 或 Mat 二维点集
寻找最小包围矩形
对于给定的2D点集,寻找可旋转的最小面积的包围矩形
RotatedRect minAreaRect(InputArray points) //参数为输入的 std::vector 或 Mat 二维点集
寻找最小包围圆形
利用一种迭代算法,对于给定的2D点集,寻找面积最小的可包围它们的圆形
void minEnclosingCircle(InputArray points, Point2f& center, floaat& radius)
//第一个参数,InputArray类型的 points,输入的二维点集,可以为 std::vector<> 或 Mat 类型
//第二个参数,Point2f&类型的 center,圆的输出圆心
//第三个参数,float&类型的radius,圆的输出半径
用椭圆拟合二维点集
RotatedRect fitEllipse(InputArray points) //参数为输入的 std::vector 或 Mat 二维点集
逼近多边形曲线
用指定精度逼近多边形曲线
void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)
//第一个参数,InputArray类型的 curve,输入的二维点集,可以为 std::vector<> 或 Mat 类型
//第二个参数,OutputArray类型的 approxCurve,多边形逼近的结果,其类型应该和输入的二维点集的类型一致
//第三个参数,double类型的epsilon,逼近的精度,为原始曲线和即近似曲线间的最大值
//第四个参数,bool类型的closed,如果其为真,则近似的曲线为封闭曲线,否则不封闭
示例:创建包围轮廓的矩形边界
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
using namespace std;
using namespace cv;
int main()
{
//初始化变量和随机值
Mat image(600, 600, CV_8UC3);
RNG& rng = theRNG();
while (1)
{
//参数初始化
int count = rng.uniform(3, 103);//随机生成点的数量
vector<Point> points;//点值
//随机生成点坐标
for (int i = 0; i < count; i++)
{
Point point;
point.x = rng.uniform(image.cols / 4, image.cols * 3 / 4);
point.y = rng.uniform(image.rows / 4, image.rows * 3 / 4);
points.push_back(point);
}
//对给定的2D点集,寻找最小面积的包围矩形
RotatedRect box = minAreaRect(Mat(points));
Point2f vertex[4];
box.points(vertex);//返回矩形的四个顶点
//绘制出随机颜色的点
image = Scalar::all(0);
for (int i = 0; i < count; i++)
{
circle(image, points[i], 3, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), FILLED, LINE_AA);
}
//绘制出最小面积的包围矩形
for (int i = 0; i < 4; i++)
{
line(image, vertex[i], vertex[(i + 1) % 4], Scalar(100, 200, 211), 2, LINE_AA);
}
imshow("矩形包围示例", image);
char key = (char)waitKey();
if (key == 27 || key == 'q' || key == 'Q')//'ESC'
break;
}
return 0;
}
示例:创建包围轮廓的圆形边界
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
using namespace std;
using namespace cv;
int main()
{
//初始化变量和随机值
Mat image(600, 600, CV_8UC3);
RNG& rng = theRNG();
while (1)
{
//参数初始化
int count = rng.uniform(3, 103);//随机生成点的数量
vector<Point> points;//点值
//随机生成点坐标
for (int i = 0; i < count; i++)
{
Point point;
point.x = rng.uniform(image.cols / 4, image.cols * 3 / 4);
point.y = rng.uniform(image.rows / 4, image.rows * 3 / 4);
points.push_back(point);
}
//对给定的2D点集,寻找最小面积的包围圆
Point2f center;
float radius = 0;
minEnclosingCircle(Mat(points), center, radius);
//绘制出随机颜色的点
image = Scalar::all(0);
for (int i = 0; i < count; i++)
{
circle(image, points[i], 3, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), FILLED, LINE_AA);
}
//绘制出最小面积的包围圆
for (int i = 0; i < 4; i++)
{
circle(image, center, cvRound(radius), Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 2, LINE_AA);
}
imshow("圆形包围示例", image);
char key = (char)waitKey();
if (key == 27 || key == 'q' || key == 'Q')//'ESC'
break;
}
return 0;
}