文章目录
图像轮廓与图像分割修复(opencv3编程入门第八章)第三节 使用多边形将轮廓包围
返回外部矩形边界:boundingRect()
Rect boundingRect(InputArray points)
points 读入的参数必须是vector或者Mat点集
寻找最小包围矩形:minAreaRect()
RotatedRect minAreaRect(InputArray points)
points:输入信息,可以为包含点的容器(vector)或是Mat。
返回包覆输入信息的最小斜矩形
寻找最小包围圆形:minEnclosingCircle()
void minEnclosingCircle(InputArray points, Point2f& center, float& radius)
- points:输入信息,可以为包含点的容器(vector)或是Mat。
- center:包覆圆形的圆心。
- radius:包覆圆形的半径。
椭圆你和二维点集:fitEllipse()
RotatedRect fitEllipse(InputArray points)
输入:二维点集,要求拟合的点至少为6个点。存储在std::vector<> or Mat
处理:该函数使用的是最小二乘的方法进行拟合的。
逼近多边形曲线:approxPolyDP()
void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)
- InputArray curve:一般是由图像的轮廓点组成的点集
- OutputArray approxCurve:表示输出的多边形点集
- double epsilon:主要表示输出的精度,为原始曲线和近似曲线间的最大值
- bool closed:表示输出的多边形是否封闭
示例代码1
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image(600, 600, CV_8UC3);
RNG& rng = theRNG();
while (1)
{
char key;
int count = (unsigned)rng % 100 + 3; //随机生成点的数量
vector<Point> points(count);
//随机生成点的坐标
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[i] = point;
}
//检测凸包
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(255, 255, 255), 2, LINE_AA);
}
//显示效果图
imshow("矩形包围示例", image);
key = (char)waitKey();
if (key == 27)
break;
}
return 0;
}
结果展示:
示例代码2
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image(600, 600, CV_8UC3);
RNG& rng = theRNG();
while (1)
{
char key;
int count = (unsigned)rng % 50 + 50; //随机生成点的数量
vector<Point> points(count);
//随机生成点的坐标
for (int i = 0; i< count; i++)
{
Point point;
point.x = rng.uniform(image.cols / 6, image.cols * 5 / 6);
point.y = rng.uniform(image.rows / 4, image.rows * 3 / 4);
points[i] = point;
}
//检测斜矩形凸包
RotatedRect box = minAreaRect(Mat(points));
Point2f vertex[4];
box.points(vertex);
//检测正矩形凸包
Rect rect = boundingRect(Mat(points));
//检测圆形凸包
Point2f center;
float radius = 0;
minEnclosingCircle(Mat(points), center, radius);
//检测三角形凸包
vector<Point2f> triangle;
minEnclosingTriangle(Mat(points), triangle);
//检测椭圆凸包
RotatedRect ellip = fitEllipse(Mat(points)); //目前这个语句不稳定,可能是由于样本点太少导致该函数无法正确执行
//凸包
vector<int> hull;
convexHull(Mat(points), hull, true);
//绘制出随机颜色的点
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(255, 255, 255), 2, LINE_AA);
}
//绘制矩形
rectangle(image, rect.tl(), rect.br(), Scalar(0, 255, 255), 2, LINE_AA);
//绘制圆形
circle(image, center, round(radius), Scalar(255, 0, 255), 2, LINE_AA);
//绘制三角形
for (int i = 0; i < 3; i++)
{
line(image, triangle[i], triangle[(i + 1) % 3], Scalar(255, 255, 0), 2, LINE_AA);
}
//绘制椭圆
ellipse(image, ellip, Scalar(0, 0, 255), 2, LINE_AA);
绘制凸包的边
int hullcount = (int)hull.size(); //凸包的边数
Point point0 = points[hull[hullcount - 1]]; //连接凸包边的坐标点
for (int i = 0; i < hullcount; i++)
{
Point point = points[hull[i]];
line(image, point0, point, Scalar(0, 255, 0), 2, LINE_AA);
point0 = point;
}
//显示效果图
imshow("矩形包围示例", image);
key = (char)waitKey();
if (key == 27)
break;
}
return 0;
}
示例代码3
/*
* 对于一幅图像,使用矩形和圆形来包围轮廓
*/
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
#define WINDOW_NAME1 "【原始图窗口】"
#define WINDOW_NAME2 "【效果图窗口】"
Mat g_srcImage;
Mat g_grayImage;
int g_nThresh = 50;
int g_nThreshMax = 255;
RNG g_rng(12345);
void on_ContourChange(int, void*);
int main()
{
VideoCapture cap(0);
if (!cap.isOpened()) // 判断是否打开成功
{
cout << "open camera failed. " << endl;
return -1;
}
while(1)
{
cap >> g_srcImage;
cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY);
blur(g_grayImage, g_grayImage, Size(3, 3));
namedWindow(WINDOW_NAME1, WINDOW_NORMAL);
imshow(WINDOW_NAME1, g_srcImage);
createTrackbar("阈值", WINDOW_NAME1, &g_nThresh, g_nThreshMax, on_ContourChange);
on_ContourChange(0, 0);
waitKey(10);
}
return 0;
}
void on_ContourChange(int, void*)
{
Mat threshold_output;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
//使用threshold检测边缘
threshold(g_grayImage, threshold_output, g_nThresh, 255, THRESH_BINARY);
//找出轮廓
findContours(threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
//多边形轮廓逼近 + 获取矩形和圆形边界框
vector<vector<Point>> contour_poly(contours.size());
vector<Rect> boundRect(contours.size());
vector<Point2f> center(contours.size());
vector<float> radius(contours.size());
//一个循环、遍历所有部分、进行本程序最核心的操作
for(int i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contour_poly[i], 3, true);
boundRect[i] = boundingRect(Mat(contour_poly[i]));
minEnclosingCircle(contour_poly[i], center[i], radius[i]);
}
//绘制
Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
for(int i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));
drawContours(drawing, contour_poly, i, color, 1,
8, vector<Vec4i>(), 0, Point());
rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
circle(drawing, center[i], (int)radius[i], color, 2, 8, 0);
}
namedWindow(WINDOW_NAME2, WINDOW_NORMAL);
imshow(WINDOW_NAME2, drawing);
}
参考文献
- opencv3编程入门 电子工业出版社 毛星云等编著