zoukankan      html  css  js  c++  java
  • opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

    一、点与轮廓的距离及位置关系

     1 #include "opencv2/opencv.hpp"
     2 #include <iostream>
     3 using namespace std;
     4 using namespace cv;
     5 
     6 void main()
     7 {
     8     //计算点到轮廓的距离与位置关系
     9     Mat srcImg = imread("E://00.png");
    10     imshow("src", srcImg);
    11 
    12     Mat dstImg = srcImg.clone();
    13     cvtColor(srcImg, srcImg, CV_BGR2GRAY);
    14     threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);
    15     imshow("threshold", srcImg);
    16 
    17     //查找轮廓
    18     vector<vector<Point>> contours;
    19     vector<Vec4i> hierarcy;
    20     findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);//TREE:提取所有轮廓    NONE:画出轮廓所有点
    21     cout << "contours.size()=" << contours.size() << endl;
    22     for (int i = 0; i < contours.size(); i++)//遍历每个轮廓
    23     {
    24         for (int j = 0; j < contours[i].size(); j++)//遍历轮廓每个点
    25         {
    26             cout << "(" << contours[i][j].x << "," << contours[i][j].y << ")" << endl;
    27         }
    28     }
    29 
    30     double a0 = pointPolygonTest(contours[0], Point(3, 3), true);//点到轮廓的最短距离
    31     double b0 = pointPolygonTest(contours[0], Point(212, 184), false);//点与轮廓的位置关系:-1表示外部;0表示在轮廓上;1表示轮廓内部
    32     cout << "a0=" << a0 << endl;
    33     cout << "b0=" << b0 << endl;
    34     waitKey(0);
    35 }

    a0之所以是负数,是因为点在轮廓外部

    二、轮廓的矩

    轮廓矩的介绍:

    http://blog.csdn.net/cp32212116/article/details/38374015
    http://blog.csdn.net/huixingshao/article/details/42060231

     1 #include "opencv2/opencv.hpp"
     2 #include <iostream>
     3 using namespace std;
     4 using namespace cv;
     5 
     6 void main()
     7 {
     8     Mat srcImg = imread("E://00.png");
     9     imshow("src", srcImg);
    10 
    11     Mat dstImg = srcImg.clone();
    12     cvtColor(srcImg, srcImg, CV_BGR2GRAY);
    13     threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);
    14     //imshow("threshold", srcImg);
    15 
    16     vector<vector<Point>> contours;
    17     vector<Vec4i> hierarcy;
    18     findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
    19     cout << "contours.size()=" << contours.size() << endl;
    20     Moments moment0 = moments(contours[0], false);
    21     cout << moment0.m00<< endl;
    22     waitKey(0);
    23 }

    三、形状匹配--matchShapes()

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

     1 #include "opencv2/opencv.hpp"
     2 #include <iostream>
     3 #include <iomanip>
     4 using namespace std;
     5 using namespace cv;
     6 
     7 void main()
     8 {
     9     Mat srcImg = imread("1.jpg");  //模板图像
    10     imshow("src", srcImg);
    11     cvtColor(srcImg, srcImg, CV_BGR2GRAY);
    12     threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);
    13     vector<vector<Point>> contours;
    14     vector<Vec4i> hierarcy;
    15     findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
    16 
    17     Mat srcImg2 = imread("2.jpg");  //待测试图片
    18     imshow("src2", srcImg2);
    19     Mat dstImg = srcImg2.clone();
    20     cvtColor(srcImg2, srcImg2, CV_BGR2GRAY);
    21     threshold(srcImg2, srcImg2, 100, 255, CV_THRESH_BINARY);
    22     vector<vector<Point>> contours2;
    23     vector<Vec4i> hierarcy2;
    24     findContours(srcImg2, contours2, hierarcy2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
    25     while (1)
    26     {
    27         for (int i = 0; i<contours2.size(); i++)
    28         {
    29             double matchRate = matchShapes(contours[0], contours2[i], CV_CONTOURS_MATCH_I1, 0.0);//形状匹配:值越小越相似
    30             cout << "index=" << i << "---" << setiosflags(ios::fixed) << matchRate << endl;//setiosflags(ios::fixed)是用定点方式表示实数,保留相同位数,相同格式输出
    31             if (matchRate <= 0.1)
    32                 drawContours(dstImg, contours2, i, Scalar(0, 255, 0), 2, 8);
    33             imshow("dst", dstImg);
    34             /*char key = waitKey();
    35             if (key == 27)
    36                 break;*/
    37         }
    38         break;
    39     }
    40     waitKey(0);
    41 }

  • 相关阅读:
    OpenLayers 添加OpenStreetMap(OSM)瓦片层示例
    [转]Net Framework引路蜂地图开发示例
    SVG关注复杂图形的网页绘制技术
    解决Easyui1.3.3 IE8兼容性问题
    Ajax学习教程在线阅读
    判断访问站点的浏览器类型
    修改TOMCAT服务器图标为应用LOGO
    ArcGIS学习推荐基础教程摘录
    Jquery html页面处理基础
    android AppWidget的使用以及利用TimerTask实现widget的定时更新
  • 原文地址:https://www.cnblogs.com/little-monkey/p/7475481.html
Copyright © 2011-2022 走看看