• OpenCV——轮廓特征描述


    检测出特定轮廓,可进一步对其特征进行描述,从而识别物体。

    1. 如下函数,可以将轮廓以多种形式包围起来。

    // 轮廓表示为一个矩形 
    Rect r = boundingRect(Mat(contours[0])); 
    rectangle(result, r, Scalar(255), 2); 
    // 轮廓表示为一个圆 
    float radius; 
    Point2f center; 
    minEnclosingCircle(Mat(contours[1]), center, radius); 
    circle(result, Point(center), static_cast<int>(radius), Scalar(255), 2); 
    // 轮廓表示为一个多边形 
    vector<Point> poly; 
    approxPolyDP(Mat(contours[2]), poly, 5, true); 
    vector<Point>::const_iterator itp = poly.begin(); 
    while (itp != (poly.end() - 1)) 
    { 
        line(result, *itp, *(itp + 1), Scalar(255), 2); 
        ++itp; 
    } 
    line(result, *itp, *(poly.begin()), Scalar(255), 2); 
    // 轮廓表示为凸多边形 
    vector<Point> hull; 
    convexHull(Mat(contours[3]), hull); 
    vector<Point>::const_iterator ith = hull.begin(); 
    while (ith != (hull.end() - 1)) 
    { 
        line(result, *ith, *(ith + 1), Scalar(255), 2); 
        ++ith; 
    } 
    line(result, *ith, *(hull.begin()), Scalar(255), 2); 

    2. 将轮廓数据存储在记事本中,然后读取数据,存入vector<cv::Point>中

    void readFromTxt(string name,int q)
    {
        ifstream file(name);
        int i = 0;
        while (file) {
            string line;
            getline(file, line);
            if (line == "")break;
            cv::Point p;
            int num;
            for (int i = 0;; i++)
            {
                if (line[i] == ';') {
                    num = i + 1;
                    break;
                }
            }
            int x = 0, y = 0;
            int k;
            for (int i = 0; i < num; i++) {
                if (line[i] == ',') {
                    k = i;
                    for (int j = 2; j < k; j++)
                    {
                        x += (line[j] - '0') * (pow(10, k - j - 1));
                    }
                }
                if (line[i] == ';') {
                    for (int j = k + 2; j < i; j++) {
                        y += (line[j] - '0') * (pow(10, i - j - 1));
                    }
                }
            }
            p.x = x;
            p.y = y;
            if(q == 0)shitou.push_back(p);
            else if (q == 1)jiandao.push_back(p);
            else bu.push_back(p);
        }
    }

    其中每行的存取格式为:

      136, 30;
      135, 31;
      134, 31;

    3. 使用mathShapes函数比较两个形状的相似度

    函数返回值 为 相似度大小,完全相同的图像返回值是0。对于第一种比较方法来说返回值最大是1。
    double cvMatchShapes( const void* object1, const void* object2,
                          int method, double parameter=0 );
    
    参数含义
    object1——第一个轮廓或灰度图像
    object2——第二个轮廓或灰度图像
    method——比较方法:
       CV_CONTOURS_MATCH_I1
       CV_CONTOURS_MATCH_I2 
       CV_CONTOURS_MATCH_I3.
    parameter——比较方法的参数 

    4. 判断某点是否在轮廓内

    double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist)

       contour——输入轮廓

       pt ——要测试的点

       measureDist ——为真则计算点到最近轮廓的整数距离,

                                     否则,只判断点的位置。返回值+1(在轮廓里面)、-1(在轮廓外面)、0(在轮廓上)。

    参考:http://mobile.51cto.com/aengine-435442.htm

  • 相关阅读:
    yjh_study_command
    installed_oracle_can't_use
    Database 2 Day DBA guide_Chapter3
    oracle_great_integration_译文
    oracle_set_autocommit
    Database 2 Day DBA guide_Chapter2
    linux_base_commond_two
    linux_base_commond_one
    Oracle_11gR2_概念_第06章_数据字典和动态性能视图_英文词汇
    angular 自定义select选项,tab切换!!!
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/7137862.html
Copyright © 2020-2023  润新知