文章目录

  • 前言
  • 一、效果
  • 二、opencv对应函数介绍
    • 1.轮廓面积
    • 2.轮廓周长
    • 3.轮廓几何矩
    • 4.轮廓的最大外接矩形
    • 5.轮廓的凸包
    • 6.轮廓的最小外接矩形
    • 7.轮廓的最小外接三角形
    • 8.轮廓的最小外接椭圆
    • 9.轮廓的多边形逼近
    • 10.轮廓的最小外接圆
  • 三、完整代码

前言

二值化轮廓发现之后,下一步最主要的就是轮廓特征分析了,常见的特征有:
轮廓面积
轮廓周长
轮廓几何矩、质心
轮廓的最大外接矩形
轮廓的凸包
轮廓的最小外接矩形
轮廓的最小外接三角形
轮廓的最小外接椭圆
轮廓的多边形逼近
轮廓的最小外接圆

一、效果

轮廓几何矩的质心

轮廓的最大外接矩形

轮廓的凸包

轮廓的最小外接矩形

轮廓的最小外接三角形

轮廓的最小外接椭圆

轮廓的多边形逼近

轮廓的最小外接圆

轮廓面积和轮廓周长:

二、opencv对应函数介绍

1.轮廓面积

CV_EXPORTS_W double contourArea( InputArray contour, bool oriented = false );

2.轮廓周长

CV_EXPORTS_W double arcLength( InputArray curve, bool closed );

3.轮廓几何矩

CV_EXPORTS_W Moments moments( InputArray array, bool binaryImage = false );

4.轮廓的最大外接矩形

CV_EXPORTS_W Rect boundingRect( InputArray array );

5.轮廓的凸包

详细介绍可参考之前文章:https://blog.csdn.net/weixin_44901043/article/details/123242496?spm=1001.2014.3001.5502

CV_EXPORTS_W void convexHull( InputArray points, OutputArray hull,bool clockwise = false, bool returnPoints = true );

6.轮廓的最小外接矩形

CV_EXPORTS_W RotatedRect minAreaRect( InputArray points );

7.轮廓的最小外接三角形

CV_EXPORTS_W double minEnclosingTriangle( InputArray points, CV_OUT OutputArray triangle );

8.轮廓的最小外接椭圆

CV_EXPORTS_W RotatedRect fitEllipse( InputArray points );

9.轮廓的多边形逼近

CV_EXPORTS_W void approxPolyDP( InputArray curve,OutputArray approxCurve,double epsilon, bool closed );

10.轮廓的最小外接圆

CV_EXPORTS_W void minEnclosingCircle( InputArray points,CV_OUT Point2f& center, CV_OUT float& radius );

三、完整代码

void test_contours_feature(){cv::Mat src;src = cv::imread("D:\\QtProject\\Opencv_Example\\contours_feature\\contours_feature.png", cv::IMREAD_GRAYSCALE);if (src.empty()) {cout << "Cannot load image" << endl;return;}cv::imshow("src", src);cv::Mat binary;cv::threshold(src, binary, 150, 255, cv::THRESH_BINARY_INV);cv::Mat k55 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5), cv::Point(-1, -1));cv::morphologyEx(binary, binary, cv::MORPH_OPEN, k55, cv::Point(-1, -1), 1);cv::namedWindow("binary",cv::WINDOW_NORMAL);cv::imshow("binary", binary);std::vector<std::vector<cv::Point> > defectContours;std::vector<cv::Vec4i> hierarchyDefect;cv::findContours(binary, defectContours, hierarchyDefect, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);cv::Mat contours = cv::Mat(src.size(), CV_8UC3, cv::Scalar(0,0,0));cv::Mat drawCenterPos = cv::Mat(src.size(), CV_8UC3, cv::Scalar(0,0,0));cv::Mat drawRect = cv::Mat(src.size(), CV_8UC3, cv::Scalar(0,0,0));cv::Mat drawHull = cv::Mat(src.size(), CV_8UC3, cv::Scalar(0,0,0));cv::Mat drawRotateRect = cv::Mat(src.size(), CV_8UC3, cv::Scalar(0,0,0));cv::Mat drawMinEnclosingCircle = cv::Mat(src.size(), CV_8UC3, cv::Scalar(0,0,0));cv::Mat drawMinEnclosingTriangle = cv::Mat(src.size(), CV_8UC3, cv::Scalar(0,0,0));cv::Mat drawEllipse = cv::Mat(src.size(), CV_8UC3, cv::Scalar(0,0,0));std::vector<std::vector<cv::Point>> PolyDPs;cv::Mat drawPolyDP = cv::Mat(src.size(), CV_8UC3, cv::Scalar(0,0,0));std::vector<std::vector<cv::Point> > hull(defectContours.size());for (int i = 0; i < defectContours.size(); i++){//轮廓 白色cv::drawContours(contours, defectContours, i, cv::Scalar(255,255,255), 1);//求面积double fArea = cv::contourArea(defectContours[i]);qDebug()<<QStringLiteral("面积")<<fArea;//求周长double fLength = cv::arcLength(defectContours[i],true);qDebug()<<QStringLiteral("周长")<<fLength;//矩求质心 红色cv::Moments mu = cv::moments(defectContours[i]);cv::Point2d centerPos = cv::Point2d( mu.m10/mu.m00 , mu.m01/mu.m00);cv::circle(drawCenterPos, centerPos, 10, cv::Scalar(0, 0, 255));//最大外接矩形 蓝色cv::Rect rect = cv::boundingRect(defectContours[i]);cv::rectangle(drawRect, rect, cv::Scalar(255,0,0));//凸包 黄色cv::convexHull(cv::Mat(defectContours[i]), hull[i], false);cv::drawContours(drawHull, hull, i, cv::Scalar(0,255,255), 1);//最小外接矩形 青色cv::RotatedRect rotateRect = cv::minAreaRect(defectContours[i]);vector<cv::Point2f> boxPts(4);rotateRect.points(boxPts.data());for (int j = 0; j < 4; j++){cv::line(drawRotateRect, boxPts[j], boxPts[(j + 1) % 4], cv::Scalar(128, 128, 0), 1, 8);  //绘制最小外接矩形每条边}//最小外接圆 棕褐色cv::Point2f center;float fRadius = 0;cv::minEnclosingCircle(defectContours[i], center, fRadius);cv::circle(drawMinEnclosingCircle, center, fRadius, cv::Scalar(140,180,210),1);//最小外界三角形 粉色std::vector<cv::Point2f> points;cv::minEnclosingTriangle(defectContours[i], points);cv::line(drawMinEnclosingTriangle, points[0], points[1], cv::Scalar(193, 182, 255), 1, 8);  //绘制最小外接三角形每条边cv::line(drawMinEnclosingTriangle, points[2], points[1], cv::Scalar(193, 182, 255), 1, 8);  //绘制最小外接三角形每条边cv::line(drawMinEnclosingTriangle, points[2], points[0], cv::Scalar(193, 182, 255), 1, 8);  //绘制最小外接三角形每条边//椭圆 紫罗兰色cv::RotatedRect ellipse =  cv::fitEllipse(defectContours[i]);cv::ellipse(drawEllipse, ellipse, cv::Scalar(226,43,138));//多边形逼近 洋红色std::vector<cv::Point> PolyDP;//cv::Mat PolyDP ;cv::approxPolyDP(defectContours[i],PolyDP, 0.5, true);PolyDPs.push_back(PolyDP);//cv::drawContours(drawPolyDP, PolyDP, 0, cv::Scalar(23,56,234));//drawPolyDP += cv::Mat(PolyDP);}//画多边形逼近for(int j = 0; j < PolyDPs.size(); j++){cv::drawContours(drawPolyDP, PolyDPs, j, cv::Scalar(255,0,255),1);}drawCenterPos += contours;drawRect += contours;drawHull += contours;drawRotateRect += contours;drawMinEnclosingCircle += contours;drawMinEnclosingTriangle += contours;drawEllipse += contours;cv::namedWindow("contours",cv::WINDOW_NORMAL);cv::imshow("contours", contours);cv::namedWindow("drawRect",cv::WINDOW_NORMAL);cv::imshow("drawRect", drawRect);cv::namedWindow("drawHull",cv::WINDOW_NORMAL);cv::imshow("drawHull", drawHull);cv::namedWindow("drawRotateRect",cv::WINDOW_NORMAL);cv::imshow("drawRotateRect", drawRotateRect);cv::namedWindow("drawCenterPos",cv::WINDOW_NORMAL);cv::imshow("drawCenterPos", drawCenterPos);cv::namedWindow("drawMinEnclosingCircle",cv::WINDOW_NORMAL);cv::imshow("drawMinEnclosingCircle", drawMinEnclosingCircle);cv::namedWindow("drawMinEnclosingTriangle",cv::WINDOW_NORMAL);cv::imshow("drawMinEnclosingTriangle", drawMinEnclosingTriangle);cv::namedWindow("drawEllipse",cv::WINDOW_NORMAL);cv::imshow("drawEllipse", drawEllipse);cv::namedWindow("drawPolyDP",cv::WINDOW_NORMAL);cv::imshow("drawPolyDP", drawPolyDP);}

opencv findContours()轮廓特征分析大全(求面积、周长、几何矩、质心、凸包、最小外接矩形、最小外接三角形、最小外接椭圆等)相关推荐

  1. python opencv最小外接矩形中心点_Opencv绘制最小外接矩形、最小外接圆

    Opencv中求点集的最小外结矩使用方法minAreaRect,求点集的最小外接圆使用方法minEnclosingCircle. minAreaRect方法原型: RotatedRect minAre ...

  2. opencv 轮廓查找, 凸包,最小外接矩形,最小外接圆,最小外接椭圆

    本章内容: 1. 轮廓查找 2. 绘制轮廓 3. 凸包 4.最小外接矩形 5.最小外接圆 6.最小外接椭圆 1.搜索轮廓 2.绘制轮廓 输出结果 3.凸包 输出结果 4.最小外接矩形 输出结果 5.最 ...

  3. POJ3348 Cows【凸包+多边形求面积】

    POJ3348Cows 凸包+多边形求面积 个人分类: 计算几何凸包 Language: Default Cows Time Limit: 2000MS   Memory Limit: 65536K ...

  4. OpenCV4学习笔记(23)——几何矩、中心矩、归一化矩和Hu矩的计算,以及基于Hu矩的轮廓匹配

    在上次的笔记中,整理记录了有关轮廓发现及轮廓信息提取的一部分内容,同时还记录了Hu矩的计算方式,今天就来记录一下Hu矩的一个应用--轮廓匹配. 在<OpenCV学习笔记(19)--模板匹配> ...

  5. 求取SHP文件的最小外接矩形并裁剪图像

    目的: 求取shp文件中每一个形状的最小外接矩形. 根据每一个形状的最小外接矩形裁剪图像. 已知数据: 一个shp文件,包含若干个形状. 2.shp文件对应的影像. 工具 ARCGIS10.4 pyt ...

  6. OpenCV——计算轮廓长度/周长和面积

    轮廓面积 轮廓面积是指每个轮廓中所有的像素点围成区域的面积,单位为像素. double contourArea( InputArray contour, bool oriented = false ) ...

  7. opencv 图像轮廓特征 图像面积,轮廓周长,外接矩形、最小外接矩形、最小外接圆、拟合椭圆

    找出图像轮廓 contours, hierarchy = cv.findContours(thresh, 3, 2) 画出图像轮廓 cnt = contours[1] cv.drawContours( ...

  8. 【图像处理】——实现二值图像的轮廓边界跟踪以及轮廓面积周长的求解(connectedComponentsWithStats()函数和connectedComponents()函数)

    目录 一.cv2.connectedComponents() 函数原型: 输入参数解析: 返回参数解析: 二.cv2.connectedComponentsWithStats() 函数原型: 输入参数 ...

  9. 【OpenCV函数】轮廓提取;轮廓绘制;轮廓面积;外接矩形

    FindContours 在二值图像中寻找轮廓  int cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_cont ...

最新文章

  1. android无法实例化服务,Android:无法实例化类:没有空的构造函数
  2. IT团队之非正式沟通
  3. C++如何把字符串转化为数字?
  4. 分类模型的评估方法-召回率(Recall)
  5. VTK:PolyData之MeshQuality
  6. OpenCV形态学变换函数morphologyEx()黑帽运算的使用
  7. Eclipse上GIT插件EGIT使用手册之五_查看历史记录
  8. java接口自动化Excel占位符_基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport的接口自动化测试框架...
  9. oracle 10 expdp impdp 导入、导出
  10. TeeChart.Pro.v7.06在BDS2006中的安装(For Win32)
  11. Google AI “作恶”,4000 员工抗议,十余人失望辞职!
  12. L2-028 秀恩爱分得快(25 分)
  13. Python 在图片加上消息通知的文字
  14. 在线光纤网速测试软件,在线网速测试_测网速_宽带测速
  15. C语言六边形蜂巢数组,CSS 实现蜂巢/六边形图集
  16. 华为鸿蒙系统正式登场,曝华为鸿蒙2.0将于6月2日正式登场 用户可一键升级成鸿蒙系统...
  17. 编写一个520表白网站并发布Web教程
  18. LRC 文件格式定义
  19. 计算机维修技术精解,《显卡维修知识精解——计算机维修技术精解》【价格 目录 书评 正版】_中图网...
  20. 把codeblock变好看

热门文章

  1. 爬虫做js逆向分析的思路
  2. C++变量的引用 | 使用变量的引用
  3. C++中的引用变量详解
  4. 如何在互联网公司求职成功
  5. git具体作用_Git是什么
  6. Empire信息收集
  7. jq轮播图——无缝轮播
  8. Python 中列表与元组的异同
  9. 福建省三明市谷歌卫星地图下载
  10. Python爬虫解决推广链接(跳转网页)的办法