2016-02-19 16 views
0

我有一個二進制圖像數據,它的大小是1280(寬)x1024(長)。這些數據存儲在一個名爲「CamBuff」的1d數組中,大小爲1280 * 1024。如何在數組中找到特定點?

Image is something like this:| 0 1 2... 1278 1279 | 
          |1280...    2559 | 
          |. . .  .   | 
          |. . .  . 1310719 | 

CamBuff: [ 0 1 2.... 1279 1280 ... 1310719] 

應該只包含0和1,因爲它是一個二值圖像數據的圖像數據(假設背景= 0,對象= 1)。

圖像中有一個對象,我想找到它的中心位置。

我想是這樣的(找對象的寬度,但可能這是錯誤的):

int width = 1280; 
int height = 1024; 
int a = 0; 
int b = 0; 
int c, startrow, startcol, endrow, endcol, objectwidth, objectheight; 
int h = 1; 
int widthstart = 0; 
int colstart = 0; 


    for (int i=0; i<height; i++) 
     { 
      for(int k=0; k<width; k++)  
      {        
       a = CamBuff[k];   
       b = CamBuff[h];   
       c = b - a; 

       if(c != 0 && widthstart == 0) 
       {       
        startrow = h;         
        widthstart = 1; 

       } 
       if(c != 0 && widthstart == 1) 
       { 
        endrow = k; 
        widthstart = 0; 

       } 
       h++; 

       objectwidth = (endrow - startrow)*0.5;   

      } 
     } 

如果有變化(從背景到對象或反之亦然),C! = 0

如何找到對象的x和y中心位置?

+0

你如何定義中心?該對象所刻的最小矩形的中心,還是幾何(面積)中心?後者將需要更多的計算。 – owacoder

+0

OpenCV可能會對此有所幫助。 – Borgleader

回答

1

如果你想要一個窗體的重心。您必須累積想要保留的像素的所有位置,併除以這些位置的數量。

類似下面的代碼會給你解決方案。

代碼沒有測試,不進行編譯,我試圖把你的符號(在你的情況下0)替代Backgound通過backgound的價值

const size_t acc_x=0, acc_y=0; 
for (int i=0; i<height; i++) 
    { 
     for(int k=0; k<width; k++)  
     { if(image[i*width+k] != Backgound) 
      { 
       acc_x += k 
       acc_y += i; 
       ++counter 
      } 
     } 
    } 
    const size_t barycenter_x = acc_x/counter; 
    const size_t barycenter_y = acc_y/counter; 
0

你可以嘗試創建一個數組用於 寬度和身高。然後檢查每條線的中間對象(中間的對象由'1')並將其放入數組中。和列相同的東西。 在創建2個數組之後,通過計算寬度和高度數組的平均值來計算點。祝你好運

相關問題