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