Opencv学习笔记(10)——轮廓
本篇文章将为进行Opencv图像处理中有关轮廓的相关知识,具体有:
1.轮廓查找(find contour in your image)
2.多边形逼近——凸包(Convex Hull)
3.轮廓几何及特性概括——轮廓周围绘制矩形框和圆形
4.图像矩(Image Moments)
5.点多边形测试
一.轮廓查找
1.相关理论知识
如上图所示,轮廓查找就是找寻图像中所存在的图像中的轮廓。
- 轮廓发现是基于图像边缘提取的基础寻找对象轮廓的方法。
所以边缘提取的阈值选定会影响最终轮廓发现结果 - API介绍
findContours 发现轮廓
drawContours 绘制轮廓
轮廓查找步骤:
- 输入图像转为灰度图像cvtColor
- 使用Canny进行边缘提取或者threshold阈值操作,得到二值图像
- 使用findContours寻找轮廓
- 使用drawContours绘制轮廓
2.相关API操作
1.轮廓发现(find contour)
在二值图像上发现轮廓使用API cv::findContours。
cv::findContours(
InputOutputArray binImg, // 输入图像,非0的像素被看成1,0的像素值保持不变,8-bit
OutputArrayOfArrays contours,// 全部发现的轮廓对象
OutputArray hierachy// 图该的拓扑结构,可选,该轮廓发现算法正是基于图像拓扑结构实现。
int mode, // 轮廓返回的模式
int method,// 发现方法
Point offset=Point()// 轮廓像素的位移,默认(0, 0)没有位移
)
2.轮廓绘制(draw contour)
在二值图像上发现轮廓使用API cv::findContours之后对发现的轮廓数据进行绘制显示。
cv::drawContours(
InputOutputArray binImg, // 输出图像
OutputArrayOfArrays contours,// 全部发现的轮廓对象
Int contourIdx// 轮廓索引号
const Scalar & color,// 绘制时候颜色
int thickness,// 绘制线宽
int lineType ,// 线的类型LINE_8
InputArray hierarchy,// 拓扑结构图
int maxlevel,// 最大层数, 0只绘制当前的,1表示绘制绘制当前及其内嵌的轮廓
Point offset=Point()// 轮廓位移,可选
}
3.程序示例
//轮廓发现:通过cv::fingContoursAPI查找轮廓,通过cv::drawContours绘制轮廓
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int threshold_value = 100;
int threshold_max = 255;
RNG rng;
const char* output_win = "Demo_Contour";
void Demo_Contours(int, void*);
Mat src,dst;
int main(int argc, char** argv) {
src = imread("D:/photos/45.png");
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
namedWindow(output_win, CV_WINDOW_AUTOSIZE);
imshow("input image", src);
cvtColor(src, src, CV_BGR2GRAY);//灰度化图像,为Canny边缘检测做准备
const char* trackbar_title = "threshold_value";
createTrackbar(trackbar_title, output_win, &threshold_value, threshold_max, Demo_Contours);//动态调整Canny边缘检测的阈值
Demo_Contours(0, 0);//使程序刚开始就有结果,与createTrackbar无关
waitKey(0);
return 0;
}
void Demo_Contours(int, void*) {
Mat canny_output;
vector<vector<Point>> contours;
vector<Vec4i> hierachy;
Canny(src, canny_output, threshold_value, threshold_value * 2, 3, false);//Canny边缘检测,3代表算子尺寸
imshow("canny image", canny_output);
findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
//contours储存轮廓的点集,轮廓提取方式为RETR_TREE,轮廓表达为:CHAIN_APPROX_SIMPLE
dst = Mat::zeros(src.size(), CV_8UC3);
RNG rng(12345);
for (size_t i = 0; i < contours.size(); i++) {
//逐条绘制轮廓
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(dst, contours, i, color, 2, 8, hierachy, 0, Point(0, 0));
}
imshow(output_win, dst);
}
运行结果:
二.几何及特性概括——凸包(Convex Hull)
1.相关理论
凸包概念介绍
什么是凸包(Convex Hull),在一个多变形边缘或者内部任意两个点的连线都包含在多边形边界或者内部。
**正式定义:**包含点集合S中所有点的最小凸多边形称为凸包。
凸包扫描算法介绍——Graham扫描算法
- 首先选择Y方向最低的点作为起始点p0。
- 从p0开始极坐标扫描,依次添加p1….pn(排序顺序是根据极坐标的角度大小,逆时针方向)。
- 对每个点pi来说,如果添加pi点到凸包中导致一个左转向(逆时针方法)则添加该点到凸包, 反之如果导致一个右转向(顺时针方向)删除该点从凸包中。
关于上述扫描算法的代码移植,OpenCV已经实现了凸包发现算法和API提供我们使用。
2.相关API介绍
cv::convexHull
convexHull(
InputArray points,// 输入候选点,来自findContours
OutputArray hull,// 凸包
bool clockwise,// default true, 顺时针方向
bool returnPoints)// true 表示返回点个数,如果第二个参数是 vector<Point>则自动忽略
}
凸包逼近实现步骤:
- 首先把图像从RGB转为灰度。
- 然后再转为二值图像。
- 在通过发现轮廓得到候选点。
- 凸包API调用。
- 绘制显示。
3.程序示例
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int threshold_value = 100;
int threshold_max = 255;
RNG rng(12345);
const char* output_win = "Demo_convex hull";
void threshold_callback(int, void*);
Mat src, dst,dst2,gray_src;
int main(int argc, char** argv