?? segapi.cpp
字號:
*(pdGrad+y*nWidth+x)=dGrad;
}
}
/*************************************************************************
*
* \函數名稱:
* RegionGrow()
*
* \輸入參數:
* CDib * pDib - 指向CDib類的指針,含有原始圖象信息
* unsigned char * pUnRegion - 指向區域生長結果的指針
*
* \返回值:
* 無
*
* \說明:
* pUnRegion指針指向的數據區存儲了區域生長的結果,其中1(邏輯)表示
* 對應象素為生長區域,0表示為非生長區域
* 區域生長一般包含三個比較重要的問題:
* 1. 種子點的選取
* 2. 生長準則
* 3. 終止條件
* 可以認為,這三個問題需要具體分析,而且每個問題解決的好壞直接關系到
* 區域生長的結果。
* 本函數的種子點選取為圖像的中心,生長準則是相鄰象素的象素值小于
* nThreshold, 終止條件是一直進行到再沒有滿足生長準則需要的象素時為止
*
*************************************************************************
*/
void RegionGrow(CDib * pDib, unsigned char * pUnRegion, int nThreshold)
{
static int nDx[]={-1,0,1,0};
static int nDy[]={0,1,0,-1};
// 遍歷圖象的縱坐標
// int y;
// 遍歷圖象的橫坐標
// int x;
// 圖象的長寬大小
CSize sizeImage = pDib->GetDimensions();
int nWidth = sizeImage.cx ;
int nHeight = sizeImage.cy ;
// 圖像在計算機在存儲中的實際大小
CSize sizeImageSave = pDib->GetDibSaveDim();
// 圖像在內存中每一行象素占用的實際空間
int nSaveWidth = sizeImageSave.cx;
// 初始化
memset(pUnRegion,0,sizeof(unsigned char)*nWidth*nHeight);
// 種子點
int nSeedX, nSeedY;
// 設置種子點為圖像的中心
nSeedX = nWidth /2 ;
nSeedY = nHeight/2 ;
// 定義堆棧,存儲坐標
int * pnGrowQueX ;
int * pnGrowQueY ;
// 分配空間
pnGrowQueX = new int [nWidth*nHeight];
pnGrowQueY = new int [nWidth*nHeight];
// 圖像數據的指針
unsigned char * pUnchInput =(unsigned char * )pDib->m_lpImage;
// 定義堆棧的起點和終點
// 當nStart=nEnd, 表示堆棧中只有一個點
int nStart ;
int nEnd ;
//初始化
nStart = 0 ;
nEnd = 0 ;
// 把種子點的坐標壓入棧
pnGrowQueX[nEnd] = nSeedX;
pnGrowQueY[nEnd] = nSeedY;
// 當前正在處理的象素
int nCurrX ;
int nCurrY ;
// 循環控制變量
int k ;
// 圖象的橫縱坐標,用來對當前象素的4鄰域進行遍歷
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<nHeight) && (yy>=0)
&& (pUnRegion[yy*nWidth+xx]==0)
&& abs(pUnchInput[yy*nSaveWidth+xx] - pUnchInput[nCurrY*nSaveWidth+nCurrX])<nThreshold )
{
// 堆棧的尾部指針后移一位
nEnd++;
// 象素(xx,yy) 壓入棧
pnGrowQueX[nEnd] = xx;
pnGrowQueY[nEnd] = yy;
// 把象素(xx,yy)設置成邏輯1(255)
// 同時也表明該象素處理過
pUnRegion[yy*nWidth+xx] = 255 ;
}
}
nStart++;
}
// 釋放內存
delete []pnGrowQueX;
delete []pnGrowQueY;
pnGrowQueX = NULL ;
pnGrowQueY = NULL ;
}
void DFT_2D(CDib * pDib,double * pTrRstRpart, double * pTrRstIpart)
{
double PI = 3.14159;
//遍歷圖象的縱坐標
int y;
//遍歷圖象的橫坐標
int x;
//頻域的橫坐標
int m;
//頻域的縱坐標
int n;
//圖象的長寬大小
CSize sizeImage = pDib->GetDimensions();
int nWidth = sizeImage.cx ;
int nHeight = sizeImage.cy ;
//圖像在計算機在存儲中的實際大小
CSize sizeImageSave = pDib->GetDibSaveDim();
int nSaveWidth = sizeImageSave.cx;
//圖像數據的指針
LPBYTE pImageData = pDib->m_lpImage;
//初始化結果數據
for(n=0; n<nHeight ; n++ )
for(m=0 ; m<nWidth ; m++ )
{
*( pTrRstRpart + n*nWidth + m ) =0;
*( pTrRstIpart + n*nWidth + m ) =0;
}
double fCosTable;
double fSinTable;
int nPxValue;
fCosTable=0 ;
nPxValue =0;
double fTmpRstR;
double fTmpRstI;
for(n=0; n<nHeight ; n++ )
for(m=0 ; m<nWidth ; m++ )
{
fTmpRstR=0;
fTmpRstI=0;
for(y=0; y<nHeight ; y++ )
for(x=0 ; x<nWidth ; x++ )
{
fCosTable= cos( 2*PI*( ((double)m*x)/nWidth + ((double)n*y)/nHeight) ) ;
fSinTable= sin( -2*PI*( ((double)m*x)/nWidth + ((double)n*y)/nHeight) ) ;
nPxValue = *(pImageData+ y*nSaveWidth + x ) ;
fTmpRstR+=fCosTable* nPxValue ;
fTmpRstI+=fSinTable* nPxValue ;
}
*( pTrRstRpart + nWidth * n + m ) = fTmpRstR;
*( pTrRstIpart + nWidth * n + m ) = fTmpRstI;
}
}
void IDFT_2D(CDib * pDib,double * pTrRstRpart, double * pTrRstIpart)
{
double PI = 3.14159;
//遍歷圖象的縱坐標
int y;
//遍歷圖象的橫坐標
int x;
//頻域的橫坐標
int m;
//頻域的縱坐標
int n;
//圖象的長寬大小
CSize sizeImage = pDib->GetDimensions();
int nWidth = sizeImage.cx ;
int nHeight = sizeImage.cy ;
//圖像在計算機在存儲中的實際大小
CSize sizeImageSave = pDib->GetDibSaveDim();
int nSaveWidth = sizeImageSave.cx;
//圖像數據的指針
LPBYTE pImageData = pDib->m_lpImage;
double fCosTable;
double fSinTable;
fCosTable=0 ;
fSinTable=0 ;
double fTmpPxValue;
double fRpartValue;
double fIpartValue;
fTmpPxValue=0;
fRpartValue=0;
fIpartValue=0;
for(y=0; y<nHeight ; y++ )
for(x=0 ; x<nWidth ; x++ )
{
fTmpPxValue=0;
for(n=0; n<nHeight ; n++ )
for(m=0 ; m<nWidth ; m++ )
{
fCosTable= cos( 2*PI*( ((double)m*x)/nWidth + ((double)n*y)/nHeight) ) ;
fSinTable= sin( 2*PI*( ((double)m*x)/nWidth + ((double)n*y)/nHeight) ) ;
fRpartValue=*(pTrRstRpart+ n*nHeight + m ) ;
fIpartValue=*(pTrRstIpart+ n*nHeight + m ) ;
fTmpPxValue+=fCosTable* fRpartValue-fSinTable*fIpartValue;
}
fTmpPxValue=fTmpPxValue/(nHeight*nWidth);
*( pImageData + nSaveWidth * y + x) = (unsigned char) fTmpPxValue ;
}
}
/*************************************************************************
*
* \函數名稱:
* SobelOperator()
*
* \輸入參數:
* CDib * pDib - 指向CDib類的指針,含有原始圖象信息
* double * pdGrad - 指向梯度數據的指針,含有圖像的梯度信息
*
* \返回值:
* 無
*
* \說明:
* Sobe算子
*
* 并行邊界分割
*
*************************************************************************
*/
void SobelOperator(CDib * pDib, double * pdGrad)
{
// 遍歷圖象的縱坐標
int y;
// 遍歷圖象的橫坐標
int x;
// 圖象的長寬大小
CSize sizeImage = pDib->GetDimensions();
int nWidth = sizeImage.cx ;
int nHeight = sizeImage.cy ;
// 圖像在計算機在存儲中的實際大小
CSize sizeImageSave = pDib->GetDibSaveDim();
// 圖像在內存中每一行象素占用的實際空間
int nSaveWidth = sizeImageSave.cx;
// 圖像數據的指針
LPBYTE lpImage = pDib->m_lpImage;
// 初始化
for(y=0; y<nHeight ; y++ )
for(x=0 ; x<nWidth ; x++ )
{
*(pdGrad+y*nWidth+x)=0;
}
// 設置模板系數
static int nWeight[2][3][3] ;
nWeight[0][0][0] = -1 ;
nWeight[0][0][1] = 0 ;
nWeight[0][0][2] = 1 ;
nWeight[0][1][0] = -2 ;
nWeight[0][1][1] = 0 ;
nWeight[0][1][2] = 2 ;
nWeight[0][2][0] = -1 ;
nWeight[0][2][1] = 0 ;
nWeight[0][2][2] = 1 ;
nWeight[1][0][0] = 1 ;
nWeight[1][0][1] = 2 ;
nWeight[1][0][2] = 1 ;
nWeight[1][1][0] = 0 ;
nWeight[1][1][1] = 0 ;
nWeight[1][1][2] = 0 ;
nWeight[1][2][0] = -1 ;
nWeight[1][2][1] = -2 ;
nWeight[1][2][2] = -1 ;
//這個變量用來表示Laplacian算子象素值
int nTmp[3][3];
// 臨時變量
double dGrad ;
double dGradOne;
double dGradTwo;
// 模板循環控制變量
int yy ;
int xx ;
// 下面開始利用Prewitt算子進行計算,為了保證計算所需要的
// 的數據位于圖像數據的內部,下面的兩重循環的條件是
// y<nHeight-1 而不是y<nHeight,相應的x方向也是x<nWidth-1
// 而不是x<nWidth
for(y=1; y<nHeight-1 ; y++ )
for(x=1 ; x<nWidth-1 ; x++ )
{
dGrad = 0 ;
dGradOne = 0 ;
dGradTwo = 0 ;
// Laplacian算子需要的各點象素值
// 模板第一行
nTmp[0][0] = lpImage[(y-1)*nSaveWidth + x - 1 ] ;
nTmp[0][1] = lpImage[(y-1)*nSaveWidth + x ] ;
nTmp[0][2] = lpImage[(y-1)*nSaveWidth + x + 1 ] ;
// 模板第二行
nTmp[1][0] = lpImage[y*nSaveWidth + x - 1 ] ;
nTmp[1][1] = lpImage[y*nSaveWidth + x ] ;
nTmp[1][2] = lpImage[y*nSaveWidth + x + 1 ] ;
// 模板第三行
nTmp[2][0] = lpImage[(y+1)*nSaveWidth + x - 1 ] ;
nTmp[2][1] = lpImage[(y+1)*nSaveWidth + x ] ;
nTmp[2][2] = lpImage[(y+1)*nSaveWidth + x + 1 ] ;
// 計算梯度
for(yy=0; yy<3; yy++)
for(xx=0; xx<3; xx++)
{
dGradOne += nTmp[yy][xx] * nWeight[0][yy][xx] ;
dGradTwo += nTmp[yy][xx] * nWeight[1][yy][xx] ;
}
dGrad = dGradOne*dGradOne + dGradTwo*dGradTwo ;
dGrad = sqrt(dGrad) ;
// 梯度值寫入內存
*(pdGrad+y*nWidth+x)=dGrad;
}
}
/*************************************************************************
*
* \函數名稱:
* PrewittOperator()
*
* \輸入參數:
* CDib * pDib - 指向CDib類的指針,含有原始圖象信息
* double * pdGrad - 指向梯度數據的指針,含有圖像的梯度信息
*
* \返回值:
* 無
*
* \說明:
* Prewitt算子
*
* 并行邊界分割
*
*************************************************************************
*/
void PrewittOperator(CDib * pDib, double * pdGrad)
{
// 遍歷圖象的縱坐標
int y;
// 遍歷圖象的橫坐標
int x;
// 圖象的長寬大小
CSize sizeImage = pDib->GetDimensions();
int nWidth = sizeImage.cx ;
int nHeight = sizeImage.cy ;
// 圖像在計算機在存儲中的實際大小
CSize sizeImageSave = pDib->GetDibSaveDim();
// 圖像在內存中每一行象素占用的實際空間
int nSaveWidth = sizeImageSave.cx;
// 圖像數據的指針
LPBYTE lpImage = pDib->m_lpImage;
// 初始化
for(y=0; y<nHeight ; y++ )
for(x=0 ; x<nWidth ; x++ )
{
*(pdGrad+y*nWidth+x)=0;
}
// 設置模板系數
static int nWeight[2][3][3] ;
nWeight[0][0][0] = -1 ;
nWeight[0][0][1] = 0 ;
nWeight[0][0][2] = 1 ;
nWeight[0][1][0] = -1 ;
nWeight[0][1][1] = 0 ;
nWeight[0][1][2] = 1 ;
nWeight[0][2][0] = -1 ;
nWeight[0][2][1] = 0 ;
nWeight[0][2][2] = 1 ;
nWeight[1][0][0] = 1 ;
nWeight[1][0][1] = 1 ;
nWeight[1][0][2] = 1 ;
nWeight[1][1][0] = 0 ;
nWeight[1][1][1] = 0 ;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -