1、相關類數組
MCvMomentside
inv_sqrt_m00 m00!=0?1/sqrt(m00):0函數
m00 spatial momentsoop
m01, m02, m03, m10, m11this
m12, m21, m30, mu02, mu03編碼
mu11, mu12, mu20, mu21, mu30spa
MCvHuMoments指針
hu1 Hu ivnariantscode
hu2, hu3, hu4, hu5, hu6, hu7orm
public static void cvMoments(
IntPtr arr, //Image (1-channel or 3-channel with COI set) or polygon (CvSeq of points or a vector of points)
ref MCvMoments moments, //Pointer to returned moment state structure
int binary=0 //(For images only) If the flag is non-zero, all the zero pixel values are treated as zeroes, all the others are treated as 1s
) Calculates spatial and central moments up to the third order and writes them to moments. The moments may be used then to calculate gravity center of the shape, its area, main axises and various shape characteristics including 7 Hu invariants.
註釋:用於計算Hu不變矩以及其餘歸一化矩
public static double cvGetSpatialMoments(
ref MCvMoments moments, //傳入值,傳入時必須含有值,不然沒法得到特定矩
int xOrder, //x order of the retrieved moment, xOrder>=0
int yOrder //y order of the retrieved moment, yOrder >= 0 and xOrder + y_order <= 3
) Retrieves the spatial moment, which in case of image moments is defined as: M_{x_order,y_order}=sum_{x,y}(I(x,y) * x^{x_order} * y^{y_order}) where I(x,y) is the intensity of the pixel (x, y).
return value: The spatial moment
註釋:用於得到特定的矩
public static double cvGetCentralMoment(
ref MCvMoments moments, //Reference to the moment state structure
int xOrder, //x order of the retrieved moment, xOrder >= 0.
int yOrder //y order of the retrieved moment, yOrder >= 0 and xOrder + y_order <= 3
) Retrieves the central moment, which in case of image moments is defined as: mu_{x_order,y_order}=sum_{x,y}(I(x,y)*(x-x_c)^{x_order} * (y-y_c)^{y_order}), where x_c=M10/M00, y_c=M01/M00 - coordinates of the gravity center
Return Value: The center moment
public static double cvGetMormalizedCentralMoment(
ref MCvMoments moments, //Reference to the moment state structure
int xOrder, //x order of the retrieved moment, xOrder >= 0.
int yOrder //y order of the retrieved moment, yOrder >= 0 and xOrder + y_order <= 3
) Retrieves normalized central moment, which in case of image moments is defined as: eta_{x_order,y_order}=mu_{x_order,y_order} / M00^{(y_order+x_order)/2+1}, where mu_{x_order,y_order} is the central moment
Return Value: The normalized center moment
public static void cvGetHuMoments(
ref MCvMoments moments, //Pointer to the moment state structure
ref MCvHuMoments huMoments //Pointer to Hu moments structure.
) Calculates seven Hu invariants
public static double cvMatchShapes(
IntPtr object1, //First contour or grayscale image
IntPtr object2, //Second contour or grayscale image
Emgu.CV.CvEnum.CONTOURS_MATCH_TYPE method, //Comparison method
double parameter //Method-specific parameter (is not used now)
) Compares two shapes.The 3 implemented methods all use Hu moments
Return Value: The result of the comparison
CV_CONTOUR_MATCH_I1: I_1(A,B)=sum_{i=1..7} abs(1/m^A_i - 1/m^B_i) where m^A_i=sign(h^A_i) log(h^A_i), m^B_i=sign(h^B_i) log(h^B_i), h^A_i, h^B_i - Hu moments of A and B, respectively
CV_CONTOURS_MATCH_I2: I_2(A,B)=sum_{i=1..7} abs(m^A_i - m^B_i) where m^A_i=sign(h^A_i) log(h^A_i), m^B_i=sign(h^B_i) log(h^B_i), h^A_i, h^B_i - Hu moments of A and B, respectively
CV_CONTOURS_MATCH_I3: I_3(A,B)=sum_{i=1..7} abs(m^A_i - m^B_i)/abs(m^A_i) where m^A_i=sign(h^A_i) log(h^A_i), m^B_i=sign(h^B_i) log(h^B_i), h^A_i, h^B_i - Hu moments of A and B, respectively
public static IntPtr cvConvexHull2(
IntPtr input, //Sequence or array of 2D points with 32-bit integer or floating-point coordinates
IntPtr hullStorage, //The destination array (CvMat*) or memory storage (CvMemStorage*) that will store the convex hull. If it is array, it should be 1d and have the same number of elements as the input array/sequence. On output the header is modified so to truncate the array downto the hull size
Emgu.CV.CvEnum.ORIENTATION orientation, //Desired orientation of convex hull: CV_CLOCKWISE or CV_COUNTER_CLOCKWISE
int returnPoints //If non-zero, the points themselves will be stored in the hull instead of indices if hull_storage is array, or pointers if hull_storage is memory storage
) The function cvConvexHull2 finds convex hull of 2D point set using Sklansky's algorithm.
Return Value: If hull_storage is memory storage, the function creates a sequence containing the hull points or pointers to them, depending on return_points value and returns the sequence on output
註釋:獲取輪廓的凸包。第一個參數是點的數組,這個數組是一個n行2列的矩陣,或者是一個輪廓。若是是點矩陣,點應該是32位整數型或者是浮點型。下一個參數是指向內存存儲的一個指針,爲結果分配內存空間。下一個參數決定了程序返回的點的排列方向。最後一個參數若是設置爲1,點會被存儲在返回數組中。若是設置爲0,只有索引被存儲在返回數組中,索引是傳遞給cvConvexHull2()的原始數組的索引。
public static int cvCheckContourConvexity(
IntPtr contour
) Tests whether the input contour is convex or not. The contour must be simple, i.e. without self-intersections.
Return Value: -1 if input is not valid, 1 if convex, 0 otherwise
public static IntPtr cvConvexityDefects(
IntPtr contour, //Input
IntPtr convexhull, //Convex hull obtained using cvConvexHull2 that should contain pointers or indices to the contour points, not the hull points themselves, i.e. return_points parameter in cvConvexHull2 should be 0
IntPtr storage //Container for output sequence of convexity defects. If it is NULL, contour or hull (in that order) storage is used
) Finds all convexity defects of the input contour and returns a sequence of the CvConvexityDefect structures.
註釋:計算凸缺陷並返回一個缺陷的序列。爲了完成這個任務,函數要求輸入序列,凸包和內存空間,從這個內存空間來得到存放結果序列的內存。返回一個MCvConvexityDefect結構體的序列,其中包括一些簡單的參數用來描述凸缺陷
MCvConvexityDefect
Depth //Distance between the farthest point and the convex hull
DepthPointPointer //Pointer to the farthest point from the convex hull within the defect
EndPointPointer //Pointer to the point of the contour where the defect ends
StartPointPointer //Pointer to the point of the contour where the defect begins
public static void cvCalcPGH(
IntPtr contour, //Input contour. Currently, only integer point coordinates are allowed
IntPtr hist //Calculated histogram; must be two-dimensional
) Calculates 2D pair-wise geometrical histogram (PGH), described in [Iivarinen97], for the contour. The algorithm considers every pair of the contour edges. The angle between the edges and the minimum/maximum distances are determined for every pair. To do this each of the edges in turn is taken as the base, while the function loops through all the other edges. When the base edge and any other edge are considered, the minimum and maximum distances from the points on the non-base edge and line of the base edge are selected. The angle between the edges defines the row of the histogram in which all the bins that correspond to the distance between the calculated minimum and maximum distances are incremented (that is, the histogram is transposed relatively to [Iivarninen97] definition). The histogram can be used for contour matching
2、輪廓的矩
矩是經過對輪廓上全部點進行積分運算(或者認爲是求和運算)而獲得的一個粗略特徵。在連續狀況下,圖像函數爲 f(x,y),那麼圖像的p+q階幾何矩(標準矩)定義爲:
p ,q = 0,1,2……
p+q階中心距定義爲:
p,q = 0,1,2……
其中和表明圖像的重心,
,
對於離散的數字圖像,採用求和號代替積分:
,,p,q = 0,1,2 ……
N和M分別是圖像的高度和寬度;
歸一化的中心距定義爲:;其中
在公式中,p對應x維度上的矩,q對應y維度上的矩,階數表示對應的部分的指數。該計算是對輪廓界上全部像素(數目爲n)進行求和。若是p和q所有爲0,那麼m00實際上對應輪廓邊界上點的數目。
雖然能夠直接計算出輪廓的矩,可是常常會用到歸一化的矩(所以不一樣大小可是形狀相同的物體會有相同的值)。一樣,簡單的矩依賴於所選座標系,這意味着物體旋轉後就沒法正確匹配。
因而就產生了Hu矩以及其餘歸一化矩的函數。
Hu矩是歸一化中心矩的線性組合。之因此這樣作是爲了可以獲取表明圖像某個特徵的矩函數。這些矩函數對縮放,旋轉和鏡像映射出了(h1)具備不變性。
Hu矩是從中心矩中計算獲得。即七個由歸一化中心矩組合成的矩:
其中中心矩和歸一化中心矩的定義爲:
咱們可使用cvContoursMoments函數、cvMoments函數方便的獲得輪廓的矩集,而後再相應的方法或函數獲取各類矩。
特定的矩:cvGetSpatialMoment函數
中心矩:cvGetCentralMoment函數
歸一化中心矩:cvGetNormalizedCentralMoment函數
Hu矩:cvGetHuMoments函數
3、輪廓的輪廓樹
輪廓樹用來描述某個特定輪廓的內部特徵。注意:輪廓樹跟輪廓是一一對應的關係;輪廓樹不用於描述多個輪廓之間的層次關係。
輪廓樹的建立過程:
從一個輪廓建立一個輪廓樹是從底端(葉子節點)到頂端(根節點)的。首先搜索三角形突出或者凹陷的形狀的周邊(輪廓上的每個點都不是徹底和它的相鄰點共線的)每一個這樣的三角形被一條線段代替,這條線段經過鏈接非相鄰點的兩點獲得;所以實際上三角形或者被削平或者被填滿。每一個這樣的替換都把輪廓的頂點減小,而且給輪廓樹建立一個新節點。若是這樣的一個三角形的兩側有原始邊,那麼她就是獲得的輪廓樹的葉子;若是一側已經是一個三角形,那麼它就是那個三角形的父節點。這個過程的迭代最終把物體的外形簡稱一個四邊形,這個四邊形也被剖開;獲得的兩個三角形是根節點的兩個子節點。
結果的二分樹最終將原始輪廓的形狀性比編碼。每一個節點被它所對應的三角形的信息所註釋。
這樣創建的輪廓樹並不太魯棒,由於輪廓上小的改變也可能會完全改變結果的樹,同時最初的三角形是任意選取的。爲了獲得較好的描述須要首先使用函數cvApproxPoly()以後將輪廓排列(運用循環移動)成最初的三角形不怎麼收到旋轉影響的狀態。
能夠用函數cvCreateContourTree來構造輪廓樹。
4、輪廓的凸包和凸缺陷
輪廓的凸包和凸缺陷用於描述物體的外形。凸包和凸缺陷很容易得到,不過我目前不知道它們到底怎麼使用。
若是要判斷輪廓是不是凸的,能夠用cvCheckContourConvexity函數。
若是要獲取輪廓的凸包,能夠用cvConvexHull2函數,返回的是包含頂點的序列。
若是要獲取輪廓的凸缺陷,能夠用cvConvexityDefects函數。
5、輪廓的成對幾何直方圖
成對幾何直方圖(pairwise geometrical histogram PGH)是鏈碼編碼直方圖(chain code histogram CCH)的一個擴展或者延伸。CCH是一種直方圖,用來統計一個輪廓的Freeman鏈碼編碼每一種走法的數字。這種直方圖的一個優良性質爲當物體旋轉45度,那麼新直方圖是老直方圖的循環平移。這樣就能夠不受旋轉影響。
(1)輪廓保存的是一系列的頂點,輪廓是由一系列線段組成的多邊形。對於看起來光滑的輪廓(例如圓),只是線段條數比較多,線段長度比較短而已。實際上,電腦中顯示的任何曲線都由線段組成。
(2)每兩條線段之間都有必定的關係,包括它們(或者它們的延長線)之間的夾角,兩條線段的夾角範圍是:(0,180)。
(3)每兩條線段上的點之間還有距離關係,包括最短(小)距離、最遠(大)距離,以及平均距離。最大距離我用了一個偷懶的計算方法,我把輪廓外界矩形的對角線長度看做了最大距離。
(4)成對幾何直方圖所用的統計數據包括了夾角和距離。
6、輪廓的匹配
若是要比較兩個物體,可供選擇的特徵不少。若是要判斷某我的的性別,能夠根據他(她)頭髮的長短來判斷,這很直觀,在長髮男稀有的年代準確率也很高。也能夠根據這我的尿尿的射程來判斷,若是射程大於0.50米,則是男性。總之,方法不少,不一而足。
咱們在上文中獲得了輪廓的這麼多特徵,它們也能夠用於進行匹配。典型的輪廓匹配方法有:Hu矩匹配、輪廓樹匹配、成對幾何直方圖匹配。1.Hu矩匹配 輪廓的Hu矩對包括縮放、旋轉和鏡像映射在內的變化具備不變性。cvMatchShapes函數能夠很方便的實現對2個輪廓間的匹配。2.輪廓樹匹配 用樹的形式比較兩個輪廓。cvMatchContourTrees函數實現了輪廓樹的對比。3.成對幾何直方圖匹配 在獲得輪廓的成對幾何直方圖以後,可使用直方圖對比的方法來進行匹。