地區增長演算法

來源:互聯網
上載者:User

 地區增長的演算法實現: 1)根據映像的不同應用選擇一個或一組種 子,它或者是最亮或最暗的點,或者是位 於點簇中心的點 2...通過像素集合的地區增長 演算法實現:
地區A 地區B 種子像素增長.3)增長的規則 4)
結束條件.


BOOL RegionGrow(int nSeedX, int nSeedY, BYTE * pUnchInput,int nWidth, int nHeight, BYTE * pUnRegion,CRect &R)
{

  int nDx[] = {-1,1,0,0};
  int nDy[] = {0,0,-1,1};
 int nSaveWidth = nWidth;
  
 // 定義堆棧,儲存座標
 int * pnGrowQueX ;
 int * pnGrowQueY ;

 // 分配空間
 pnGrowQueX = new int [nWidth*nHeight];
 pnGrowQueY = new int [nWidth*nHeight];

 // 定義堆棧的起點和終點
 // 當nStart=nEnd, 表示堆棧中只有一個點
 int nStart ;
 int nEnd ;

 //初始化
 nStart = 0 ;
 nEnd = 0 ;

 // 把種子點的座標壓入棧
 pnGrowQueX[nEnd] = nSeedX;
 pnGrowQueY[nEnd] = nSeedY;

 // 當前正在處理的象素
 int nCurrX ;
 int nCurrY ;

 // 迴圈控制變數
 int k ;

 // 圖象的橫縱座標,用來對當前象素的8鄰域進行遍曆
 int xx;
 int yy;

 while (nStart<=nEnd)
 {
  // 當前種子點的座標
  nCurrX = pnGrowQueX[nStart];
  nCurrY = pnGrowQueY[nStart];

  // 對當前點的4鄰域進行遍曆
  for (k=0; k<4; k++) 
  { 
   // 4鄰域象素的座標
   xx = nCurrX+nDx[k];
   yy = nCurrY+nDy[k];

   // 判斷象素(xx,yy) 是否在映像內部
   // 判斷象素(xx,yy) 是否已經處理過
   // pUnRegion[yy*nWidth+xx]==0 表示還沒有處理

   // 生長條件:判斷象素(xx,yy)和當前象素(nCurrX,nCurrY) 象素值差的絕對值
   if ( (xx < nWidth) && (xx>=0) && (yy>=0) && (yy<nHeight) 
   && (pUnRegion[yy*nWidth+xx]==0) && (pUnchInput[yy*nSaveWidth+xx]==1)) 
   {
    // 堆棧的尾部指標後移一位
    nEnd++;

    // 象素(xx,yy) 壓入棧
    pnGrowQueX[nEnd] = xx;
    pnGrowQueY[nEnd] = yy;

    // 把象素(xx,yy)設定成邏輯1(255)
    // 同時也表明該象素處理過
    pUnRegion[yy*nWidth+xx] = 255 ;
   }
  }
  nStart++;
 }
    
 
 //找出地區的範圍
    int nMinx=pnGrowQueX[0], nMaxx=pnGrowQueX[0], nMiny=pnGrowQueY[0], nMaxy = pnGrowQueY[0];
    for (k=0; k<nEnd; k++)
 {
        if (pnGrowQueX[k] > nMaxx)
             nMaxx = pnGrowQueX[k];
       if (pnGrowQueX[k] < nMinx) 
            nMinx = pnGrowQueX[k];
       if (pnGrowQueY[k] > nMaxy)
            nMaxy = pnGrowQueY[k];
       if (pnGrowQueY[k] < nMiny) 
           nMiny = pnGrowQueY[k];
 }

    if ((nMaxy - nMiny) > 40 && (nMaxx - nMinx) > 40)
 {
    R.left = nMinx;
    R.right = nMaxx;
    R.top = nMiny;
    R.bottom = nMaxy;
       return TRUE;
 }
    // 釋放記憶體
 delete []pnGrowQueX;
 delete []pnGrowQueY;
 pnGrowQueX = NULL ;
 pnGrowQueY = NULL ;
 return FALSE;
}

//調用方法
void OnButton(LPBYTE S,int ImageWidth,int ImageHeight)
{
 int i=0,j=0;
CRect rect;
 LPBYTE lpFlag = new BYTE[ImageWidth*ImageHeight];
 memset(lpFlag,0,ImageWidth*ImageHeight);
 for (i=0; i<ImageHeight; i++)
 {
  for (j=0; j<ImageWidth; j++)
  {
   if (S[i*ImageWidth+j] == 1 && lpFlag[i*ImageWidth+j] == 0)
   {
    RegionGrow(j, i, S, ImageWidth, ImageHeight, lpFlag,rect);
   }
  }
 
 }

 if(lpFlag!=NULL)
 {
  delete []lpFlag;
  lpFlag = NULL;
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.