opencv:图像轮廓计算

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
RNG rng(12345);
int main(int argc, char** argv)
{
Mat src = imread("f:/images/qq/20200215213837.png");
//Mat src = imread("f:/images/qq/yezi.png");
if (src.empty())
{
printf("Could not find the image!\n");
return -1;
}
namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src);
// 做一个高斯模糊,消除一些细微的东西
GaussianBlur(src, src, Size(3, 3), 0);
GaussianBlur(src, src, Size(3, 3), 0);
Mat gray, binary;
cvtColor(src, gray, COLOR_BGR2GRAY);
imshow("gray", gray);
// 二值化
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binary", binary);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
//findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
// 只绘制最外层的轮廓
findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point());
// 一个一个绘制
for (size_t t = 0; t < contours.size(); t++) {
double area = contourArea(contours[t]);
double len = arcLength(contours[t], true);
printf("#%d === area: %.2f, length: %.2f\n", t, area, len);
// 可以在这里过滤较小的面积或周长的轮廓, 2改为-1,就是填充这个轮廓
drawContours(src, contours, t, Scalar(0, 0, 255), 1, 8);
Rect box = boundingRect(contours[t]);
rectangle(src, box, Scalar(255, 0, 0), 1, 8);
// 最小外接矩形矩形
Point2f pts[4];
RotatedRect rrt = minAreaRect(contours[t]);
rrt.points(pts);
for (int i = 0; i < 4; i++) {
line(src, pts[i], pts[(i+1)%4], Scalar(0, 255, 255), 1, 8);
}
// 绘制外接椭圆
ellipse(src, rrt, Scalar(255, 255, 0), 1, 8);
}
// -1代表绘制所有
//drawContours(src, contours, -1, Scalar(0, 0, 255), 2, 8);
imshow("result", src);
waitKey(0);
destroyAllWindows();
return 0;
}
opencv:图像轮廓计算的更多相关文章
- OpenCV图像轮廓检测
轮廓检测: 轮廓检测的原理通俗的说就是掏空内部点,比如原图中有3*3的矩形点.那么就可以将中间的那一点去掉. 一.关键函数1.1 cvFindContours函数功能:对图像进行轮廓检测,这个函数将 ...
- opencv 图像轮廓
图片解析: 原图: code: #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\c ...
- opencv 图像仿射变换 计算仿射变换后对应特征点的新坐标 图像旋转、缩放、平移
常常需要最图像进行仿射变换,仿射变换后,我们可能需要将原来图像中的特征点坐标进行重新计算,获得原来图像中例如眼睛瞳孔坐标的新的位置,用于在新得到图像中继续利用瞳孔位置坐标. 仿射变换在:http:// ...
- OpenCV图像的轮廓的匹配
http://blog.sina.com.cn/s/blog_67a7426a0101cxl0.html 一个跟轮廓相关的最常用到的功能是匹配两个轮廓.如果有两个轮廓,如何比较它们;或者如何比较一个轮 ...
- opencv 6 图像轮廓与图像分割修复 3 图像的矩,分水岭,图像修补
图像的矩 矩的计算:moments()函数 计算轮廓面积:contourArea()函数 #include "opencv2/highgui/highgui.hpp" #inclu ...
- OpenCV计算机视觉学习(8)——图像轮廓处理(轮廓绘制,轮廓检索,轮廓填充,轮廓近似)
如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 1, ...
- OpenCV笔记(3)(Canny边缘检测、高斯金字塔、拉普拉斯金字塔、图像轮廓、模板匹配)
一.Canny边缘检测 Canny边缘检测是一系列方法综合的结果.其中主要包含以下步骤: 1.使用高斯滤波器,平滑图像,滤除噪声. 2.计算图像中每个像素点的梯度强度和方向. 3.应用非极大值抑制(N ...
- opencv: 轮廓提取;
一般轮廓提取是通过对图像的梯度进行卷积计算,得到图像边缘(滤波),常用的边缘检测方法有candy.sobel. Laplacian等,再对二值化后的边缘图像进行轮廓计算: 1.Candy算子: cv: ...
- OpenCV3入门(十)图像轮廓
1.图像轮廓 1.1图像轮廓与API函数 轮廓是一系列相连的点组成的曲线,代表了物体的基本外形,相对于边缘,轮廓是连续的,边缘并不全部连续.一般地,获取图像轮廓要经过下面几个步骤: 1) 读取 ...
随机推荐
- ZOJ1310-Robot (BFS)
The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...
- Spring Boot整合Dubbo2.x,解决其中遇到的坑
Dubbo了解 a high-performance, java based open source RPC framework. Dubbo官网 源码 文档 快速知道用法 本地服务 Spring 配 ...
- Bootstrap Table Api手册
总是忘记在哪里,找起来又十分的费劲,特地记录下该手册地址. http://www.itxst.com/Bootstrap-Table/QuickStart.html
- python 处理protobuf协议
背景:需要用django基于python3模拟一个http接口,请求是post方式,body是protobuf string,返回也是protobuf string 设计:django获取pb str ...
- Python入门8 —— 逻辑运算符补充
一:优先级:not > and > or 1.not与紧跟其后的那个条件是不可分割的 2.如果条件语句全部由纯and.或纯or链接,按照从左到右的顺序依次计算即可 print(True a ...
- AcWing 278. 数字组合 求方案数目
//M看成背包容量,把每个数看成一个物品,Ai看成是体积 //目标:求总体积恰好为M的方案数目 #include <iostream> using namespace std; ; int ...
- AcWing 1014. 登山
#include<iostream> using namespace std ; ; int f[N],g[N]; int w[N]; int main() { int n; cin> ...
- 洛谷P1093 奖学金
https://www.luogu.org/problem/P1093 #include <bits/stdc++.h> using namespace std; struct Node{ ...
- 棋盘划分问题中4的k次方减一是三的倍数
1.数学归纳法(万物皆可数学归纳) ①当n=1时:4-1=3(是三的倍数) ②假设n-1成立证明n成立:4n-1=4n-1*(4-1)+4n-1-1 =3*4n-1+(4n-1-1) 所以4n-1%3 ...
- java简单学生成绩管理系统
题目要求: 一. 数据结构要求:(5 分) 1.定义 ScoreInformation 类,其中包括七个私有变量(stunumber, name, mathematicsscore, englishi ...