moments函数
moments函數(shù)
函數(shù)作用:
opencv中的矩主要包括以下幾種:空間矩,中心矩和中心歸一化矩。
?// 空間矩
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;
?}
1、空間矩的公式為:
可以知道,對于01二值化的圖像,m00即為輪廓的面積。
2、中心矩的公式為:
其中:
3、歸一化的中心矩公式為:
距的作用:1、計算一幅二值化圖像的白色區(qū)域的面積,其實是計算它的0階矩:
公式中x0和y0,不起作用,可以刪除:
現(xiàn)在,在一幅二值化的圖像中,一個像素點的值要么是0,要么是1;所以,對于每一個白色像素點,
一個‘1’被加到矩中。(這是一種高效的計算二值化圖像中白色點個數(shù)的方法)。
2、計算物體的質(zhì)心(或者是重心)
我們得到了白色像素點的x坐標的和、y坐標的和。由于是一個和,所以,我們需要得到其均值,即通過除以白色像素點的個數(shù)。白色像素點的個數(shù)可以通過(2)計算面積中的公式實現(xiàn),即圖像的0階矩,所以,得到:
這種計算物體質(zhì)心的方法,它的一個優(yōu)點是,對噪聲不敏感。當,有外部噪聲干擾的時候,計算出的質(zhì)心不會有太大的偏離。
從數(shù)學的角度來看,這種方法是計算一個連通域的質(zhì)心,或者說,是計算一個團塊(blob)的質(zhì)心。如果,你的圖像中有兩個連通域,即有兩個blob,那么,就需要把兩個blob提取出來,分別計算它們的質(zhì)心。
3、中心矩----計算質(zhì)心
這種除法很常見-- 一個矩除以其0階矩。由于很常見,所以,它有個專用的術(shù)語,稱之為-- 中心矩。
所以,計算質(zhì)心,也說為:計算一階中心矩。
函數(shù)形式:
C++:?Moments?moments(InputArray?array, bool?binaryImage=false?)
圖像矩的聲明:
class Moments { public:Moments();Moments(double m00, double m10, double m01, double m20, double m11,double m02, double m30, double m21, double m12, double m03 );Moments( const CvMoments& moments );operator CvMoments() const;// spatial moments(空間矩)double m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;// central moments(中心矩)double mu20, mu11, mu02, mu30, mu21, mu12, mu03;// central normalized moments(歸一化中心矩)double nu20, nu11, nu02, nu30, nu21, nu12, nu03; }
Note
?,???, hence the values are not stored.
opencv代碼:
#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);/// 函數(shù)聲明 void thresh_callback(int, void* );/** @主函數(shù) */ int main( int argc, char** argv ) {/// 讀入原圖像, 返回3通道圖像數(shù)據(jù)src = imread( argv[1], 1 );/// 把原圖像轉(zhuǎn)化成灰度圖像并進行平滑cvtColor( src, src_gray, CV_BGR2GRAY );blur( src_gray, src_gray, Size(3,3) );/// 創(chuàng)建新窗口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 函數(shù) */ void thresh_callback(int, void* ) {Mat canny_output;vector<vector<Point> > contours;vector<Vec4i> 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<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 );/// 通過m00計算輪廓面積并且和OpenCV函數(shù)比較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 );} }
總結(jié)
- 上一篇: opencv矩阵运算(1)
- 下一篇: HuMoments函数