在OpenCV中,能夠很方便的計算多邊形區域的3階特徵矩,opencv中的矩主要包括如下幾種:空間矩,中心矩和中心歸一化矩。html
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; }空間矩的公式爲:算法
能夠知道,對於01二值化的圖像,m00即爲輪廓的面積。中心矩的公式爲:app
其中:函數
歸一化的中心矩公式爲:spa
矩的基本概念可參考:3d
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.htmlcode
OpenCV中計算矩的函數爲:Moments moments(InputArray array, bool binaryImage=false )orm
Hu不變矩主要是利用歸一化中心矩構造了7個不變特徵矩:htm
OpenCV中計算Hu矩的公式爲:blog
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("\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 );
}
程序執行後效果圖:
最後咱們再利用matchShape函數比較兩個輪廓,若是結果爲0,表示兩個輪廓徹底類似,結果值越大,越不類似,但這個最大值好像並無歸一化,我曾經比較兩個輪廓,結果值達到了10。
比較的代碼爲:
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);
matchShapes函數其實比較的是兩個輪廓的Hu不變矩,第三個參數決定比較的方式,下面是第三個參數的三個可選值。
CV_CONTOURS_MATCH_I1
CV_CONTOURS_MATCH_I2
CV_CONTOURS_MATCH_I3
這裏:
分別是A,B的Hu矩。
程序代碼:工程FirstOpenCV29