opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配
一、点与轮廓的距离及位置关系

#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv; void main()
{
//计算点到轮廓的距离与位置关系
Mat srcImg = imread("E://00.png");
imshow("src", srcImg); Mat dstImg = srcImg.clone();
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY);
imshow("threshold", srcImg); //查找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);//TREE:提取所有轮廓 NONE:画出轮廓所有点
cout << "contours.size()=" << contours.size() << endl;
for (int i = ; i < contours.size(); i++)//遍历每个轮廓
{
for (int j = ; j < contours[i].size(); j++)//遍历轮廓每个点
{
cout << "(" << contours[i][j].x << "," << contours[i][j].y << ")" << endl;
}
} double a0 = pointPolygonTest(contours[], Point(, ), true);//点到轮廓的最短距离
double b0 = pointPolygonTest(contours[], Point(, ), false);//点与轮廓的位置关系:-1表示外部;0表示在轮廓上;1表示轮廓内部
cout << "a0=" << a0 << endl;
cout << "b0=" << b0 << endl;
waitKey();
}

a0之所以是负数,是因为点在轮廓外部
二、轮廓的矩
轮廓矩的介绍:
http://blog.csdn.net/cp32212116/article/details/38374015
http://blog.csdn.net/huixingshao/article/details/42060231


#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv; void main()
{
Mat srcImg = imread("E://00.png");
imshow("src", srcImg); Mat dstImg = srcImg.clone();
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY);
//imshow("threshold", srcImg); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
cout << "contours.size()=" << contours.size() << endl;
Moments moment0 = moments(contours[], false);
cout << moment0.m00<< endl;
waitKey();
}

三、形状匹配--matchShapes()
注意与模板匹配matchTemplate()相区分。形状匹配对于旋转、尺度、位移都能适应。

#include "opencv2/opencv.hpp"
#include <iostream>
#include <iomanip>
using namespace std;
using namespace cv; void main()
{
Mat srcImg = imread("1.jpg"); //模板图像
imshow("src", srcImg);
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY);
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); Mat srcImg2 = imread("2.jpg"); //待测试图片
imshow("src2", srcImg2);
Mat dstImg = srcImg2.clone();
cvtColor(srcImg2, srcImg2, CV_BGR2GRAY);
threshold(srcImg2, srcImg2, , , CV_THRESH_BINARY);
vector<vector<Point>> contours2;
vector<Vec4i> hierarcy2;
findContours(srcImg2, contours2, hierarcy2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
while ()
{
for (int i = ; i<contours2.size(); i++)
{
double matchRate = matchShapes(contours[], contours2[i], CV_CONTOURS_MATCH_I1, 0.0);//形状匹配:值越小越相似
cout << "index=" << i << "---" << setiosflags(ios::fixed) << matchRate << endl;//setiosflags(ios::fixed)是用定点方式表示实数,保留相同位数,相同格式输出
if (matchRate <= 0.1)
drawContours(dstImg, contours2, i, Scalar(, , ), , );
imshow("dst", dstImg);
/*char key = waitKey();
if (key == 27)
break;*/
}
break;
}
waitKey();
}

opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配的更多相关文章
- 【OpenCV学习笔记】三十、轮廓特征属性及应用(七)—位置关系及轮廓匹配
http://blog.csdn.net/abc8730866/article/details/69219992 轮廓特征属性及应用(七)—位置关系及轮廓匹配 1.计算点与轮廓的距离及位置关系——po ...
- opencv学习之路(23)、轮廓查找与绘制(二)——访问轮廓每个点
一.简介 二.画出每个轮廓的每个点 #include "opencv2/opencv.hpp" using namespace cv; void main() { Mat src= ...
- opencv学习之路(22)、轮廓查找与绘制(一)
一.简介 图2 二.代码 #include"opencv2/opencv.hpp" #include<iostream> using namespace std; us ...
- opencv学习之路(16)、膨胀腐蚀应用之走迷宫
一.分析 贴出应用图片以供直观了解 红色部分,因图而异(某些参数,根据图片的不同需要进行相应的修改) 二.代码 #include "opencv2/opencv.hpp" #inc ...
- opencv学习之路(27)、轮廓查找与绘制(六)——外接圆、椭圆拟合、逼近多边形曲线、计算轮廓面积及长度、提取不规则轮廓
一.最小外接圆 #include "opencv2/opencv.hpp" #include<iostream> using namespace std; using ...
- opencv学习之路(25)、轮廓查找与绘制(四)——正外接矩形
一.简介 二.外接矩形的查找绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //外接矩形的查找 ...
- opencv学习之路(24)、轮廓查找与绘制(三)——凸包
一.简介 二.绘制点集的凸包 #include<opencv2/opencv.hpp> using namespace cv; void main() { //---绘制点集的凸包 Mat ...
- opencv学习之路(29)、轮廓查找与绘制(八)——轮廓特征属性及应用
一.简介 HSV颜色空间(hue色调,saturation饱和度,value亮度) 二.HSV滑动条 #include "opencv2/opencv.hpp" #include ...
- opencv学习之路(26)、轮廓查找与绘制(五)——最小外接矩形
一.简介 二.轮廓最小外接矩形的绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //轮廓最小外 ...
随机推荐
- CF 1131C Birthday
C. Birthday time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- D - Windows Message Queue
来源hdu1509 Message queue is the basic fundamental of windows system. For each process, the system mai ...
- 23.react-router 路由
箭头函数扩展: 箭头函数: functoin 函数名(参数){ 函数体 } 箭头函数: 1.把function删掉 , 2.参数和{}之间加上 箭头=> 简写: 1.参数的简写:只有一个参 ...
- CString 成员函数用法
参考文档:http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html CString的构造函数CString( );例:CStr ...
- python特殊函数__str__、__repr__和__len__
1.__str__ 首先介绍__str__ class Students(object): def __init__(self, *args): self.names = args # def __s ...
- Nginx之使用nginx搭建简单的文件服务器
使用nginx可以搭建简单文件服务器 安装nginx(不详述) 修改配置文件 /usr/local/nginx/conf/nginx.conf user root; /usr/local/nginx/ ...
- Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- [iptables] 如何用iptables管理桥接模式下的设备
场景:qemu虚拟机通过tap设备与host的物理网卡通过bridge桥接上网. 如下: [root@host100 ~]# brctl show bridge name bridge id STP ...
- AndrewNG Deep learning课程笔记 - CNN
参考, An Intuitive Explanation of Convolutional Neural Networks http://www.hackcv.com/index.php/archiv ...
- TCP/IP的分层管理
网络基础TCP/IP 我们通常所使用的网络(包括互联网)均是在TCP/IP协议族的基础上运作的.HTTP属于它内部的一个子集 TCP/IP协议族按层次分为:应用层,传输层,网络层和数据链路层(更好的划 ...