http://blog.csdn.net/abc8730866/article/details/69219992

轮廓特征属性及应用(七)—位置关系及轮廓匹配

1.计算点与轮廓的距离及位置关系——pointPolygonTest()

2.矩的计算——moments()

3.形状匹配(比较两个形状或轮廓间的相似度)——matchShapes()

先上ppt:

代码:1.计算点到轮廓的距离与位置关系

  1. ///计算点到轮廓的距离与位置关系
  2. #include "opencv2/opencv.hpp"
  3. using namespace cv;
  4. #include <iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. //1.查找轮廓前的预处理
  9. Mat srcImg = imread("00.png",CV_LOAD_IMAGE_COLOR);
  10. Mat copyImg = srcImg.clone();
  11. cvtColor(srcImg,srcImg,CV_BGR2GRAY);
  12. threshold(srcImg,srcImg,100,255,CV_THRESH_BINARY);//确保黑中找白
  13. imshow("thresh",srcImg);
  14. //2.查找轮廓
  15. vector<vector<Point>> contours;
  16. findContours(srcImg,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);//最外层轮廓
  17. drawContours(copyImg, contours, -1, Scalar(0, 255, 0), 2, 8);
  18. //3.计算点到轮廓的距离与位置关系
  19. Point2f p1(20, 20);
  20. circle(copyImg,p1,3,Scalar(0,0,255),-1,8);
  21. double a0 = pointPolygonTest(contours[0], p1, true);//true表示点到轮廓的距离
  22. double b0 = pointPolygonTest(contours[0], p1, false);//false表示计算点与轮廓的位置关系
  23. cout << "a0=" << a0 << endl;
  24. cout << "b0=" << b0 << endl;
  25. imshow("contours",copyImg);
  26. waitKey(0);
  27. return 0;
  28. }

运行结果:

代码:2.轮廓矩的计算

  1. ///轮廓矩的计算
  2. #include "opencv2/opencv.hpp"
  3. using namespace cv;
  4. #include <iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. //1.查找轮廓前的预处理
  9. Mat srcImg = imread("00.png", CV_LOAD_IMAGE_COLOR);
  10. Mat copyImg = srcImg.clone();
  11. cvtColor(srcImg, srcImg, CV_BGR2GRAY);
  12. threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);//确保黑中找白
  13. imshow("thresh", srcImg);
  14. //2.查找轮廓
  15. vector<vector<Point>> contours;
  16. findContours(srcImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//最外层轮廓
  17. drawContours(copyImg, contours, -1, Scalar(0, 255, 0), 2, 8);
  18. //3.轮廓矩的计算
  19. Moments moments0 = moments(contours[0],false);//计算轮廓矩
  20. cout << moments0.m00<< endl;//输出空间矩之一的m00
  21. imshow("contours", copyImg);
  22. waitKey(0);
  23. return 0;
  24. }

运行结果:

代码:3.形状匹配---比较两个形状或轮廓间的相似度

  1. ///形状匹配---比较两个形状或轮廓间的相似度
  2. #include "opencv2/opencv.hpp"
  3. using namespace cv;
  4. #include <iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. //1.查找模版图像的轮廓
  9. Mat templateImg = imread("1.jpg", CV_LOAD_IMAGE_COLOR);
  10. Mat copyImg1 = templateImg.clone();
  11. cvtColor(templateImg, templateImg, CV_BGR2GRAY);
  12. threshold(templateImg, templateImg, 100, 255, CV_THRESH_BINARY);//确保黑中找白
  13. imshow("thresh1", templateImg);
  14. vector<vector<Point>> contours1;
  15. findContours(templateImg, contours1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//最外层轮廓
  16. drawContours(copyImg1, contours1, -1, Scalar(0, 255, 0), 2, 8);
  17. //2.查找待测试图像的轮廓
  18. Mat testImg = imread("2.jpg", CV_LOAD_IMAGE_COLOR);
  19. Mat copyImg2 = testImg.clone();
  20. cvtColor(testImg, testImg, CV_BGR2GRAY);
  21. threshold(testImg, testImg, 100, 255, CV_THRESH_BINARY);//确保黑中找白
  22. imshow("thresh2", testImg);
  23. vector<vector<Point>> contours2;
  24. findContours(testImg, contours2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//最外层轮廓
  25. //3.形状匹配---比较两个形状或轮廓间的相似度
  26. for (int i = 0; i < contours2.size();i++)//遍历待测试图像的轮廓
  27. {
  28. //返回此轮廓与模版轮廓之间的相似度,a0越小越相似
  29. double a0 = matchShapes(contours1[0],contours2[i],CV_CONTOURS_MATCH_I1,0);
  30. cout << "模版轮廓与待测试图像轮廓" << i << "的相似度:" << a0 << endl;//输出两个轮廓间的相似度
  31. if (a0<0.1)//如果此轮廓与模版轮廓的相似度小于0.1
  32. {
  33. drawContours(copyImg2, contours2, i, Scalar(0, 255, 0), 2, 8);//则在待测试图像上画出此轮廓
  34. }
  35. imshow("copyImg2", copyImg2);
  36. if (waitKey(0) == 27)//等待按键进行下一个轮廓,ESC则退出
  37. {
  38. cout << "ESC退出" << endl;
  39. break;
  40. }
  41. }
  42. waitKey(0);
  43. return 0;
  44. }

运行结果:

图像与轮廓的相似度匹配

http://blog.csdn.net/zt271675484/article/details/21305893

1 普通局

2 中心距:平移不变性

3 归一化中心距:缩放不变性

4 hu矩:旋转不变性

iplImage* img=cvload("xxxxx");

//计算普通局和中心距

CvMoments moment;

cvMoments(img,&moment,2);//第三个参数:>0  0/1组成图像

//计算hu矩

CVHuMoments humoment;

cvGetHuMoments(&moment,&humoment);

图像1---》hu矩

图像2---》hu矩

通过比较 图像1和2 的hu矩 --- 值越小 相似度就越大。

  1. // contourMatch.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "cv.h"
  5. #include "highgui.h"
  6. #include "cxcore.h"
  7. #include "cvaux.h"
  8. //www.opencvchina.com
  9. int main(int argc, char* argv[])
  10. {
  11. //产生一幅图像
  12. IplImage* src;
  13. src = cvCreateImage(cvSize(10,10),8,1);
  14. //图像初始值清零
  15. cvZero(src);
  16. //图像的前面5行5列赋值为255
  17. for(int yy=0;yy<5;yy++)
  18. {
  19. for(int xx=0;xx<5;xx++)
  20. {
  21. cvSetReal2D(src,yy,xx,255);
  22. }
  23. }
  24. double m00,m10,m01;
  25. //定义矩变量
  26. CvMoments moment;
  27. //计算原始矩和中心矩
  28. cvMoments(src,&moment,2);
  29. m00 = cvGetSpatialMoment(&moment,0,0);
  30. m10 = cvGetSpatialMoment(&moment,1,0);
  31. m01 = cvGetSpatialMoment(&moment,0,1);
  32. //计算质心坐标
  33. float x = (float)(m10/m00);
  34. float y = (float)(m01/m00);
  35. //定义hu矩变量
  36. CvHuMoments humoment;
  37. //计算hu矩
  38. cvGetHuMoments(&moment,&humoment);
  39. return 0;
  40. }

OPencv 比较hu矩的函数(已经封装的上述的运算)

CvSeq* contours1=通过函数获取img的轮廓 指针。

CvSeq* contours1=通过函数获取img的轮廓 指针。

double result=cvMatchShapes(contours1,contours2,1);//第三个参数 为比较的方式。 输出比较的相似度浮点值。

    1. #pragma comment(lib,"cxcore.lib")
    2. #pragma comment(lib,"cv.lib")
    3. #pragma comment(lib,"highgui.lib")
    4. #pragma comment(lib,"ml.lib")
    5. #pragma comment(lib,"cvcam.lib")
    6. #pragma comment(lib,"cvaux.lib")
    7. #include <stdio.h>
    8. #include <iostream>
    9. #include <cv.h>
    10. #include <cxcore.h>
    11. #include <highgui.h>
    12. using namespace std;
    13. CvSeq* getImageContous(IplImage* srcin)
    14. {
    15. IplImage*src;
    16. src=cvCreateImage(cvGetSize(srcin),8,1);
    17. cvCopy(srcin,src);
    18. CvMemStorage* mem=cvCreateMemStorage(0);
    19. CvSeq* seq;
    20. if (!mem)
    21. {
    22. printf("mem is null");
    23. }
    24. cvThreshold(src,src,200,255,CV_THRESH_BINARY);// 二值化
    25. cvFindContours(src,mem,&seq,sizeof(CvContour),CV_RETR_CCOMP);
    26. cvReleaseImage(&src);
    27. return seq;
    28. }
    29. int main()
    30. {
    31. IplImage* src1=cvLoadImage("img//contour.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    32. CvSeq* contours1=getImageContous(src1);
    33. IplImage* src2=cvLoadImage("img//carno//2.bmp",CV_LOAD_IMAGE_GRAYSCALE);
    34. CvSeq* contours2=getImageContous(src2);
    35. double result=cvMatchShapes(contours1,contours2,1);
    36. printf("%f \n",result);
    37. cvWaitKey(0);
    38. //release
    39. cvReleaseImage(&src1);
    40. cvReleaseImage(&src2);
    41. return 0;
    42. }
    43. #include "cv.h"
      #include "cxcore.h"
      #include "highgui.h"
      #include <iostream>
      #include "function.h"
      int MatchContour(int argc,char** argv)
      {
      IplImage *Src1=cvLoadImage("e:\\picture\\jiantou.jpg",0);
      IplImage *Src2=cvLoadImage("e:\\picture\\jiantou2.jpg",0);
      IplImage *BinaryImage1=cvCreateImage(cvGetSize(Src1),Src1->depth,1);
      IplImage *BinaryImage2=cvCreateImage(cvGetSize(Src2),Src2->depth,1);
      IplImage *SrcColor1=cvCreateImage(cvGetSize(Src1),Src1->depth,3);
      IplImage *SrcColor2=cvCreateImage(cvGetSize(Src2),Src2->depth,3);
      cvThreshold(Src1,BinaryImage1,100,255,CV_THRESH_BINARY);
      cvThreshold(Src2,BinaryImage2,100,255,CV_THRESH_BINARY);
      CvMemStorage* storage1=cvCreateMemStorage(0);
      CvMemStorage* storage2=cvCreateMemStorage(0);
      CvSeq* ContourSeq1=NULL;
      CvSeq* ContourSeq2=NULL;
      cvFindContours(BinaryImage1,storage1,&ContourSeq1,sizeof(CvContour));
      cvFindContours(BinaryImage2,storage2,&ContourSeq2,sizeof(CvContour));
      cvCvtColor(Src1,SrcColor1,CV_GRAY2BGR);
      cvCvtColor(Src2,SrcColor2,CV_GRAY2BGR);
      cvDrawContours(
      return 0;
      }

       

【OpenCV学习笔记】三十、轮廓特征属性及应用(七)—位置关系及轮廓匹配的更多相关文章

  1. angular学习笔记(三十)-指令(10)-require和controller

    本篇介绍指令的最后两个属性,require和controller 当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子: html: <outer‐direct ...

  2. angular学习笔记(三十)-指令(7)-compile和link(2)

    继续上一篇:angular学习笔记(三十)-指令(7)-compile和link(1) 上一篇讲了compile函数的基本概念,接下来详细讲解compile和link的执行顺序. 看一段三个指令嵌套的 ...

  3. angular学习笔记(三十)-指令(7)-compile和link(1)

    这篇主要讲解指令中的compile,以及它和link的微妙的关系. link函数在之前已经讲过了,而compile函数,它和link函数是不能共存的,如果定义了compile属性又定义link属性,那 ...

  4. angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令

    在angular学习笔记(三十)-指令(4)-transclude文章的末尾提到了,如果在指令中需要反复使用被嵌套的那一坨,需要使用transclude()方法. 在angular学习笔记(三十)-指 ...

  5. angular学习笔记(三十)-指令(5)-link

    这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数 ...

  6. angular学习笔记(三十)-指令(2)-restrice,replace,template

    本篇主要讲解指令中的 restrict属性, replace属性, template属性 这三个属性 一. restrict: 字符串.定义指令在视图中的使用方式,一共有四种使用方式: 1. 元素: ...

  7. angular学习笔记(三十)-指令(1)-概述

    之前在 angular学习笔记(十九)-指令修改dom 里面已经简单的提到了angular中的指令,现在来详细的介绍 '指令' 一.指令的创建: dirAppModule.directive('dir ...

  8. 【Unity 3D】学习笔记三十五:游戏实例——摄像机切换镜头

    摄像机切换镜头 在游戏中常常会切换摄像机来观察某一个游戏对象,能够说.在3D游戏开发中,摄像头的切换是不可或缺的. 这次我们学习总结下摄像机怎么切换镜头. 代码: private var Camera ...

  9. angular学习笔记(三十)-指令(8)-scope

    本篇讲解指令的scope属性: scope属性值可以有三种: 一.scope:false 默认值,这种情况下,指令的作用域就是指令元素当前所在的作用域. 二.scope:true 创建一个继承了父作用 ...

随机推荐

  1. 一个php日志类

    <?php //author:lixiuran class Log { public static function writeLog($string) { $string = date('H: ...

  2. C++异常 将对象用作异常类型

    通常,引发异常的函数将传递一个对象.这样做的重要有点之一是,可以使用不同的异常类型来区分不同的函数在不同情况下引发的异常.另外,对象可以携带信息,程序员可以根据这些信息来确定引发异常的原因.同时,ca ...

  3. jQuery之ajaxForm提交表单

      1.jQuery的设计非常优雅,其源代码亦给人以美感,利用jQuery框架写出来的js既简练又能完美跨浏览器.   2.jquery form插件是基于jQuery开发的一套能够利用ajax技术提 ...

  4. vux组件绑定事件

    我一开始是这样绑定事件的,但是没有效果: <box gap="15px 45px"> <x-button plain type="primary&quo ...

  5. 【BZOJ2668】[cqoi2012]交换棋子 费用流

    [BZOJ2668][cqoi2012]交换棋子 Description 有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态.要求第i行第j列 ...

  6. java如何发起https请求

    1.写一个SSLClient类,继承至HttpClient import java.security.cert.CertificateException; import java.security.c ...

  7. windows本地环境如何用wamp配置多域名绑定访问

    https://jingyan.baidu.com/article/acf728fd5fcdadf8e510a3e5.html

  8. 系统事件管理(Events) ---- HTML5+

    模块:events Events模块管理客户端事件,包括系统事件,如扩展API加载完毕.程序前后台切换等. 比如说:网络的链接的和断开这种事件,系统从前台走到后台这种事件: 不包括:点击和滑动页面事件 ...

  9. 阅读笔记:A Few useful things to Know About machine Learning

    这是Machine Learning领域的经典论文,文中提到了ML相关的12个keys,并自称这些keys是“black art”,我觉得有点像ML的“最佳实践”. 网上有此文的中文翻译,写得很详细, ...

  10. 详探TextRange对象--查找与选择(转载)

    TextRange对象是动态HTML(DHTML)的高级特性,使用它可以实现很多和文本有关的任务,例如搜索和选择文本.文本范围让您可以选择性的将字符.单词和句子从文档中挑选出来.TextRange对象 ...