1. 程式人生 > >opencv輪廓檢測之FindContours函式演算法解釋

opencv輪廓檢測之FindContours函式演算法解釋

在檢測物體的輪廓時,我們通常會使用到opencv中的findcontour和drawcontour,比較常用而且效果不錯。

1985年,satoshi suzuki發表了一篇論文,Topological structural analysis of digitized binary images by border following,他介紹了兩種演算法來實現輪廓的提取,當然輸入的影象是二值影象。findcontour就是基於這篇論文的思路來實現。

一、論文首先介紹了基本概念

       1. 上下左右極限位置構成了frame, 假定frame畫素由0 構成

       2. 由畫素0構成的componet :如果包含frame, 那將其稱之為background;若不包含frame,則稱為hole;

       3. 定義一: 

               In the 4- (8-) connected case, a 1-pixel (i, j) having a 0-pixel ( p, q) in its 8- (4) neighborhood is called a border point.

               It is also described as “a border point between a 1-component S1, and a 0-components S2,”

                       if (i, j) is a member of S1 and (p, q) is a member of S2.

           定義二: (surroundness among connected components).

                        For given two connected components S1 and S2 in a binary picture, if there exists a pixel belonging  to S2 for any 4-path from a pixel in S1 to a pixel on the frame, we say that S2 surrounds S1. 

                         If S2 surrounds S1 and there exists a border point between them, then S2 is said to surround 

S1 directly.

           定義三: (outer border and hole border). 

                        An outer border is defined as the set of the border points between an arbitrary 1-component and the 0-component which surrounds it directly. Similarly, we refer to the set of the border points between a hole and the 1-component which surrounds it directly asa hole border.
                        We use the term “border” for either an outer border or a hole border. Note that the hole border is defined as a set of 1-pixels (not 0-pixels)as well as the outer border.

           定義四:

                       The parent border of an outer border between a1-component S1 and the 0-component S2 which surrounds S1 directly is defined as:
                 (1) the hole border between S2 and the 1-component which surrounds S2directly, if S2 is ahole;
                 (2) the frame of the picture, if S2 is the background.

           定義五:

                     For two given borders B0 and Bn of a binary picture, we say that Bn surrounds B0 is there exists a sequence of border B0, B1, . . . , Bn such that Bk if the parent border of Bk-1, for all k (1 <= k <= n)

論文主要介紹了兩種演算法,用來對數字二值影象進行拓撲分析。第一種演算法是在確定二值影象邊界的圍繞關係,即確定外邊界、孔邊界以及他們的層次關係,由於這些邊界和原圖的區域具有一一對應關係(外邊界對應畫素值為1的連通區域,孔邊界對應畫素值為0的區域),因此我們就可以用邊界來表示原圖。第二種演算法,是第一種演算法的修改版本,本質一樣,但是它只找最外面的邊界。

即:一個是確定這些border的包含關係

       一個是隻找最外層的border

二、主要分析第一個演算法:

     1、每次行掃描,遇到以下兩種情況,確定外邊界(outer boeder)和孔邊界(hole border):

(1)f(i,j-1)=0,f(i,j)=1;//f(i,j)是外邊界的起始點

(2)f(i,j)>=1,f(i,j+1)=0;//f(i,j)是孔邊界的起始點

   2、然後給它編號(編號唯一,由NBD指定)

       在這裡分配一個唯一的標示符給新發現的邊界,叫做NBD。初始時NBD=1,每次發現一個新邊界加1。在這個過程中,遇到f(p,q)=1,f(p,q+1)=0時,將f(p,q)置為-NBD。什麼意思呢?就是右邊邊界的終止點。