To obtain the Hand guestures by Finding Contours and Convex Hull(凸包)

Finding Contours
找到影像的輪廓!!!!!





http://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html

C++:  findContours函數之參數兩種表達寫法

全寫6個參數

void findContours(InputOutputArray image, 
                              OutputArrayOfArrays contours,
                              OutputArray hierarchy, 
                              int mode, 
                              int method, 
                              Point offset=Point())
===========================================

  • image – 輸入圖,使用八位元單通道圖,所有非零的像素都會列入考慮,通常為二極化後的圖。
  • contours – 包含所有輪廓的容器(vector),每個輪廓都是儲存點的容器(vector),所以contours的資料結構為vector< vector>。
  • hierarchy – 可有可無的輸出向量,以階層的方式記錄所有輪廓。
  • mode –取得輪廓的模式,有以下幾種可選擇:
    • CV_RETR_EXTERNAL:只取最外層的輪廓。
    • CV_RETR_LIST:取得所有輪廓,不建立階層(hierarchy)。
    • CV_RETR_CCOMP:取得所有輪廓,儲存成兩層的階層,首階層為物件外圍,第二階層為內部空心部分的輪廓,如果更內部有其餘物件,包含於首階層。
    • CV_RETR_TREE:取得所有輪廓,以全階層的方式儲存。
  • method –儲存輪廓點的方法,有以下幾種可選擇:
    • CV_CHAIN_APPROX_NONE:儲存所有輪廓點。
    • CV_CHAIN_APPROX_SIMPLE:對水平、垂直、對角線留下頭尾點,所以假如輪廓為一矩形,只儲存對角的四個頂點。
  • offset – Optional offset by which every contour point is shifted. This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context.

=========================================== 省略OutputArray hierarchy 5個參數

void findContours(InputOutputArray image, 
                              OutputArrayOfArrays contours, 
                              int mode, 
                              int method, 
                              Point offset=Point())
===========================================



===========================================
Convex Hull(凸包)



圖1.1「凸包」/「凸殼」



多維空間中有一群散佈各處的點,「凸包」是包覆這群點的所有外殼當中,表面積暨容積最小的一個外殼,而最小的外殼一定是凸的。(如 圖1.1  所見)


至於「凸」的定義是:圖形內  任意兩點的連線  不會經過圖形外部。(如 圖1.2  所見)


     圖1.2 「凸凹區別」

「凸」並不是指表面呈弧狀隆起,事實上凸包是由許多平坦表面組成的。(如 圖1.3  所見)














圖1.3

二維平面上散佈的點,找到凸包

二維平面上的凸包是一個凸多邊形,在所有點的外圍繞一圈(可逆/可順)再連接縮緊得凸包。
(如 圖1.4  所見)








圖1.4  二維平面下凸包的生成

<PS:另外,最頂端、最底端、最左端、最右端的點,一定是凸包上的點。>



計算凸包時需考慮一些特殊情況:

  1. 凸包上多點重疊
  2. 凸包上多點共線
  3. 凸包呈一條線段、一個點、沒有點


通常我們會簡化資訊,以最少的點來記錄凸包,去掉重疊、共線的點。


=============================================================



Imagine that you have a piece of wood and a number of metal pins come out from it randomly (as in the above image), and you picked up an elastic rubber band and open it wide with your hand to contain all the metal pins and let it go. You will observe that the elastic rubber band will shrink and snap around the metal pins (only the outer metal pins). These outer segments of the elastic band form the convex hull.

想像你有一塊木片和一些金屬大頭針,隨機被插在木片上。(如上圖所示)
之後你拿起一個彈性鬆緊帶,並用你的手撐開它,讓鬆緊帶包覆含蓋所有的金屬大頭針,讓它去包圍、包覆。你會觀察到彈性橡膠帶將縮小和圍繞周圍的金屬針(只有最外圍金屬大頭針被環繞)。彈性帶的這些外圍區段形成的就叫做「凸包」。

















The algorithm of Convex Hull/凸包演算法




從一個凸包上的頂點開始,順著外圍繞一圈,順時針或逆時針都可以。

每當尋找下一個要被包覆的點,則窮舉平面上所有點,找出最外圍的一點來包覆。可以利用叉積運算來判斷。

Initialize all possible line segments between the given points:
For  i = 0  to i < number of segments
{
      For j = 0 to j < number of all points
        {
             get the points that are not in the current segment
         }
          Check if the segment is an Edge of the convex hull by
checking whether any of the  points
       Is located on its left
       If it is not an edge remove it from the segment list and
reset the iterator i and continue

       Else increment the iterator i and continue
}




留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題