zoukankan      html  css  js  c++  java
  • OpenCV学习(33) 轮廓的特征矩Moment

          在OpenCV中,可以很方便的计算多边形区域的3阶特征矩,opencv中的矩主要包括以下几种:空间矩,中心矩和中心归一化矩。

    class Moments { public: ...... // 空间矩 double m00, m10, m01, m20, m11, m02, m30, m21, m12, m03; // 中心矩 double mu20, mu11, mu02, mu30, mu21, mu12, mu03; // 中心归一化矩 double nu20, nu11, nu02, nu30, nu21, nu12, nu03; }空间矩的公式为:

    image

    可以知道,对于01二值化的图像,m00即为轮廓的面积。中心矩的公式为:

    image

    其中:

    image

    归一化的中心矩公式为:

    image
     

    矩的基本概念可参考:

    http://www.opencvchina.com/thread-509-1-1.html
     

    在OpenCV中,还可以很方便的得到Hu不变距,Hu不变矩在图像旋转、缩放、平移等操作后,仍能保持矩的不变性,所以有时候用Hu不变距更能识别图像的特征。Hu不变矩的基本概念请参考paper:Hu. Visual Pattern Recognition by Moment Invariants, IRE Transactions on Information Theory, 8:2, pp. 179-187, 1962, 或者参考中文介绍:http://www.cnblogs.com/skyseraph/archive/2011/07/19/2110183.html

     

    OpenCV中计算矩的函数为:Moments moments(InputArray array, bool binaryImage=false )

     

    Hu不变矩主要是利用归一化中心矩构造了7个不变特征矩:

    image

    OpenCV中计算Hu矩的公式为:

    HuMoments(const Moments& m, OutputArray hu)

    void HuMoments(const Moments& moments, double hu[7])

    下面的代码计算轮廓的矩,并根据1阶中心矩得到轮廓的质心,代码如下:

    src = imread( "../star1.jpg" ,1 );

    /// Convert image to gray and blur it
    cvtColor( src, src_gray, CV_BGR2GRAY );
    blur( src_gray, src_gray, Size(3,3) );

    namedWindow( "image", CV_WINDOW_AUTOSIZE );
    imshow( "image", src );

    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    //利用canny算法检测边缘
    Canny( src_gray, canny_output, thresh, thresh*2, 3 );
    namedWindow( "canny", CV_WINDOW_AUTOSIZE );
    imshow( "canny", canny_output );
    //查找轮廓
    findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    //计算轮廓矩
    vector<Moments> mu(contours.size() );
    for( int i = 0; i < contours.size(); i++ )
    { mu[i] = moments( contours[i], false ); }

    //计算轮廓的质心
    vector<Point2f> mc( contours.size() );
    for( int i = 0; i < contours.size(); i++ )
    { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }

    //画轮廓及其质心
    Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
    for( int i = 0; i< contours.size(); i++ )
    {
    Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
    drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
    circle( drawing, mc[i], 4, color, -1, 8, 0 );
    }

    namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
    imshow( "Contours", drawing );

    //打印轮廓面积和轮廓长度
    printf(" Info: Area and Contour Length ");
    for( int i = 0; i< contours.size(); i++ )
    {
    printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f ", i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], true ) );
    Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
    drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
    circle( drawing, mc[i], 4, color, -1, 8, 0 );
    }

    程序执行后效果图:

    imageimage

           最后我们再利用matchShape函数比较两个轮廓,如果结果为0,表示两个轮廓完全相似,结果值越大,越不相似,但这个最大值好像并没有归一化,我曾经比较两个轮廓,结果值达到了10。

    image

    比较的代码为:

    double comres;
    comres = matchShapes(contours[0], contours[1],CV_CONTOURS_MATCH_I1, 0.0);
    printf("CV_CONTOURS_MATCH_I1 比较结果是: %f ", comres);
    comres = matchShapes(contours[0], contours[1],CV_CONTOURS_MATCH_I2, 0.0);
    printf("CV_CONTOURS_MATCH_I2 比较结果是: %f ", comres);
    comres = matchShapes(contours[0], contours[1],CV_CONTOURS_MATCH_I3, 0.0);
    printf("CV_CONTOURS_MATCH_I3 比较结果是: %f ", comres);

    matchShapes函数其实比较的是两个轮廓的Hu不变矩,第三个参数决定比较的方式,下面是第三个参数的三个可选值。

    • CV_CONTOURS_MATCH_I1

      I_1(A,B) =  sum _{i=1...7}  left |  frac{1}{m^A_i} -  frac{1}{m^B_i} 
ight |

    • CV_CONTOURS_MATCH_I2

      I_2(A,B) =  sum _{i=1...7}  left | m^A_i - m^B_i  
ight |

    • CV_CONTOURS_MATCH_I3

      I_3(A,B) =  max _{i=1...7}  frac{ left| m^A_i - m^B_i 
ight| }{ left| m^A_i 
ight| }

    这里:

    egin{array}{l} m^A_i =  mathrm{sign} (h^A_i)  cdot log{h^A_i} \ m^B_i =  mathrm{sign} (h^B_i)  cdot log{h^B_i} end{array}

     h^A_i, h^B_i 分别是A,B的Hu矩。

    程序代码:工程FirstOpenCV29

  • 相关阅读:
    electron入坑指南
    记CSS格式化上下文
    Ruby安装Scss
    向量
    产生指定范围的随机数
    纯CSS 图片演示
    socket 函数
    C++网络套接字编程TCP和UDP实例
    技巧收藏
    伤心啊
  • 原文地址:https://www.cnblogs.com/mikewolf2002/p/3427564.html
Copyright © 2011-2022 走看看