OpenCV使用findContours查找轮廓和相关函数——C++

  • findContours函数的使用

findContours函数的使用

//查找前景的区域
vector<vector<Point>> contours;
vector<Vec4i>hierarchy;
findContours(Image_morp, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));

自己写的代码:用于得到轮廓的最小外界矩阵、最小外接圆、轮廓的面积等

//查找前景的区域
vector<vector<Point>> contours;
vector<Vec4i>hierarchy;
findContours(Image_morp, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));//cout << "查找前景的区域" << endl;
vector<vector<Point> > contours_poly(contours.size());
vector<Rect> boundRect(contours.size());
vector<Point2f>center(contours.size());
vector<float>radius(contours.size());
cout << "查找前景的区域-定义存储器" << endl;vector<int> xs, xy;
cout << "contours.size(): " << contours.size() << endl;
if (contours.size() == 0)
{cout << "没有任何前景" << endl;return 0;
}float image_area = 0.0;
for (int i = 0; i < contours.size(); i++)
{approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);cout << "进入函数" << endl;boundRect[i] = boundingRect(Mat(contours[i]));image_area += contourArea(contours[i]);cout << "是空的" << endl;cout << boundRect[i].x << "     ii " << endl;cout << boundRect[i].y << "     ii " << endl;cout << boundRect[i].width << "     ii " << endl;cout << boundRect[i].height << "     ii " << endl;cout << boundRect[i] << "     ii " << endl;xs.push_back(boundRect[i].x);xs.push_back(boundRect[i].x + boundRect[i].width);xy.push_back(boundRect[i].y);xy.push_back(boundRect[i].y + boundRect[i].height);minEnclosingCircle(contours[i], center[i], radius[i]);
}
int x_max, x_min, y_max, y_min;
x_max = *max_element(xs.begin(), xs.end());
x_min = *min_element(xs.begin(), xs.end());
y_max = *max_element(xy.begin(), xy.end());
y_min = *min_element(xy.begin(), xy.end());cout << "x_max: v " << x_max << endl;
cout << "x_min: v " << x_min << endl;
cout << "y_max: v " << y_max << endl;
cout << "y_min: v " << y_min << endl;
cout << "src.col: v " << pBkImage.cols << endl;
cout << " src.rows: v " << pBkImage.rows << endl;
cout << " 总面积为:  " << image_area << endl;//画出矩阵和圆形
Mat drawing = Mat::zeros(Image_threshold.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
{Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));drawContours(drawing, contours_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("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", drawing);
waitKey(0);

OpenCV使用findContours查找轮廓和相关函数——C++相关推荐

  1. OpenCV使用findContours()查找轮廓

    C++: void findContours (InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, ...

  2. 利用OpenCV的findContours作轮廓检测

    图像处理开发需求.图像处理接私活挣零花钱,请加微信/QQ 2487872782 图像处理开发资料.图像处理技术交流请加QQ群,群号 271891601 问: 边缘检测与轮廓检测有什么区别? 边缘检测是 ...

  3. OpenCV之findContours获取轮廓(Python版)

    参考:https://blog.csdn.net/loovelj/article/details/78739790 OpenCV自带寻找轮廓的函数,流程是:获取灰度图→图片二值化→寻找轮廓 直接上代码 ...

  4. 基于OpenCV的findContours查找图像连通域,并进行排序

    //基于OpenCV,对读入图片查找连通域,并把每个连通域包含的坐标点根据y值从小到大进行排序. #include <opencv2/legacy/legacy.hpp> #include ...

  5. 使用OpenCV的findContours获取轮廓并切割(python)

    1 获取轮廓 OpenCV2获取轮廓主要是用cv2.findContours import numpy as np import cv2 im = cv2.imread('test.jpg') img ...

  6. (openCV 十九)轮廓Contours形状拟合

    什么是轮廓? 轮廓可以简单认为成将连续的点(连着边界)连在一起的曲线,具有相同 的颜色或者灰度.轮廓在形状分析和物体的检测和识别中很有用. • 为了更加准确,要使用二值化图像.在寻找轮廓之前,要进行阈 ...

  7. 计算机视觉开源库OpenCV之查找轮廓函数cv2.findContours()介绍

    计算机视觉开源库OpenCV之查找轮廓函数cv2.findContours说明如下: 示例代码: #!/usr/bin/env python3import cv2image = cv2.imread( ...

  8. python使用opencv查找轮廓_(八)OpenCV-Python学习—轮廓查找,绘制和拟合

    针对物体轮廓,opencv还提供了一些相关的函数,来处理轮廓查找,绘制,拟合,以及计算轮廓周长和面积等,详细介绍如下: 1. 寻找和绘制轮廓 opencv的findContours()能寻找图片中的轮 ...

  9. 【图像处理】——Python+opencv实现二值图像的轮廓边界跟踪以及轮廓面积周长的求解(findcontours函数和contourArea函数)

    目录 一.函数 cv.findContours 二.轮廓层级(返回参数hierarchy) 三.轮廓寻找方式 1. RETR_LIST 2. RETR_TREE 3. RETR_EXTERNAL 4. ...

最新文章

  1. 线程间操作无效: 从不是创建控件的线程访问它
  2. DPM 2007SRT及DPM 2010 BMR祼金属还原总结
  3. 【Windows 逆向】使用 CE 工具挖掘关键数据内存真实地址 ( CE 找出子弹数据内存地址是临时地址 | 挖掘真实的子弹数据内存地址 )
  4. 五问弄懂液冷数据中心
  5. 电源pd功能的充电器_PD快充充电器电源芯片和普通充电器电源芯片有什么区别?...
  6. 【撸啊撸 Docker】搭建 Jenkins
  7. Redis 5.0 正式版发布了,19 个新特性
  8. 中文网页重设与排版:TYPO.CSS
  9. [LeetCode][JavaScript]Palindrome Linked List
  10. FISCO BCOS rpc端口、channel端口、p2p端口 怎么用是什么
  11. Lingo 基本使用
  12. 希腊字母表 ← LaTeX
  13. PCI及PCIe基本知识
  14. 【测试】11月11日的测试
  15. 运营书籍:从零开始做
  16. spring boot V部落 V人事项目
  17. CSS揭秘:6.复杂的背景图案(下)
  18. HMM(Hidden Markov Model)
  19. Oracle 10g 在win10下的安装
  20. VisualVm的理解

热门文章

  1. SpringBoot-Druid-Atomikos分布式事务
  2. aspera下载sra文件linux,Aspera Connect,高速下载sra数据
  3. 大模型时代,推荐一下专注原创和热点的几个公众号
  4. 关于SAP EWM看着一篇就够了(三)完结篇
  5. Apollo项目坐标系研究
  6. 小白也能看懂的踩坑日记--ubuntu(rk3588)安装LXR(阅读源码工具)服务器
  7. ①学习Android前言(关于环境搭建)
  8. 宽带断电后连不上网服务器无响应,为什么我家装的电信宽带一停电就连不上去...
  9. C语言程序设计 | 程序编译与预处理
  10. 基于ROS搭建简易软件框架实现ROV水下目标跟踪(一)--简述