Opencv矩运算

5,434次阅读
没有评论

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;

}

空间矩的公式为:

<a href="http://images.cnitblog.com/blog/361409/201311/17103211-a8da2974ffd742958875cc72fd55acb3.png" target="_blank"><img loading="lazy" title="image" data-src="http://images.cnitblog.com/blog/361409/201311/17103215-44ba2a0aab8f48c28f001bb8ded4df79.png" data-lazy="true" src="https://www.deeplearn.me/wp-content/themes/wordpress-theme-puock-1.4/assets/img/z/load-tip.png" alt="image" width="240" height="46" border="0" /></a></pre/>
可以知道,对于01二值化的图像,m00即为轮廓的面积。

中心矩的公式为:
<pre><a href="http://images.cnitblog.com/blog/361409/201311/17103216-5af69503a5f8440bb5257da9e70120cd.png" target="_blank"><img loading="lazy" title="image" data-src="http://images.cnitblog.com/blog/361409/201311/17103217-0189c4ec7e764191930a3d370b8a4856.png" data-lazy="true" src="https://www.deeplearn.me/wp-content/themes/wordpress-theme-puock-1.4/assets/img/z/load-tip.png" alt="image" width="307" height="55" border="0" /></a></pre/>
其中:
<pre><a href="http://images.cnitblog.com/blog/361409/201311/17103217-0d7d17635eb24c71b089598df0552182.png" target="_blank"><img loading="lazy" title="image" data-src="http://images.cnitblog.com/blog/361409/201311/17103217-8834f4029d8e48f3b083b2b58c114b1a.png" data-lazy="true" src="https://www.deeplearn.me/wp-content/themes/wordpress-theme-puock-1.4/assets/img/z/load-tip.png" alt="image" width="159" height="55" border="0" /></a></pre/>
归一化的中心矩公式为:
<pre><a href="http://images.cnitblog.com/blog/361409/201311/17103218-f6a80d91946f404d8f46dee760859aff.png" target="_blank"><img loading="lazy" title="image" data-src="http://images.cnitblog.com/blog/361409/201311/17103218-654a6dc0c8cf4a42add332ad0e47256a.png" data-lazy="true" src="https://www.deeplearn.me/wp-content/themes/wordpress-theme-puock-1.4/assets/img/z/load-tip.png" alt="image" width="164" height="62" border="0" /></a></pre/>
矩的基本概念可参考:
<pre><a title="http://www.opencvchina.com/thread-509-1-1.html" href="http://www.opencvchina.com/thread-509-1-1.html" target="_blank">http://www.opencvchina.com/thread-509-1-1.html</a>
在OpenCV中,还可以很方便的得到Hu不变距,Hu不变矩在图像旋转、缩放、平移等操作后,仍能保持矩的不变性,所以有时候用Hu不变距更能识别图像的特征。Hu不变矩的基本概念请参考paper:Hu. <em>Visual Pattern Recognition by Moment Invariants</em>, IRE Transactions on Information Theory, 8:2, pp. 179-187, 1962, 或者参考中文介绍:<a title="http://www.cnblogs.com/skyseraph/archive/2011/07/19/2110183.html" href="http://www.cnblogs.com/skyseraph/archive/2011/07/19/2110183.html" target="_blank">http://www.cnblogs.com/skyseraph/archive/2011/07/19/2110183.html</a>

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

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

Opencv矩运算

OpenCV中计算Hu矩的公式为:

HuMoments(const Moments& m, OutputArray hu)

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

matchShapes函数其实比较的是两个轮廓的Hu不变矩:

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

第三个参数决定比较的方式,下面是第三个参数的三个可选值。

  • CV_CONTOURS_MATCH_I1
  • CV_CONTOURS_MATCH_I2
  • CV_CONTOURS_MATCH_I3
  • 这里:分别是A,B的Hu矩。

 

 

 

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);

/// 函数声明
void thresh_callback(int, void* );

/** @主函数 */
int main( int argc, char** argv )
{
  /// 读入原图像, 返回3通道图像数据
  src = imread( argv[1], 1 );

  /// 把原图像转化成灰度图像并进行平滑
  cvtColor( src, src_gray, CV_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );

  /// 创建新窗口
  char* source_window = "Source";
  namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  imshow( source_window, src );

  createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
  thresh_callback( 0, 0 );

  waitKey(0);
  return(0);
}

/** @thresh_callback 函数 */
void thresh_callback(int, void* )
{
  Mat canny_output;
  vector<vector > contours;
  vector hierarchy;

  /// 使用Canndy检测边缘
  Canny( src_gray, canny_output, thresh, thresh*2, 3 );
  /// 找到轮廓
  findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

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

  ///  计算中心矩:
  vector 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 );

  /// 通过m00计算轮廓面积并且和OpenCV函数比较
  printf("\t Info: Area and Contour Length \n");
  for( int i = 0; i< contours.size(); i++ )
     {
       printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", 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 );
     }
}
admin
版权声明:本站原创文章,由admin2015-11-24发表,共计3200字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)