資源描述:
《Canny算法源碼》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、Canny算法源碼/***************************************************************************函數(shù)名稱:*Canny()**輸入?yún)?shù):*unsignedchar*pUnchImage-圖象數(shù)據(jù)*intnWidth-圖象數(shù)據(jù)寬度*intnHeight-圖象數(shù)據(jù)高度*doublesigma-高斯濾波的標(biāo)準(zhǔn)方差*doubledRatioLow-低閾值和高閾值之間的比例*doubledRatioHigh-高閾值占圖象象素總數(shù)的比例*unsignedchar*pUnchEdge-canny算子計算后的分割圖*
2、*返回值:*無**說明:*canny分割算子,計算的結(jié)果保存在pUnchEdge中,邏輯1(255)表示該點為*邊界點,邏輯0(0)表示該點為非邊界點。該函數(shù)的參數(shù)sigma,dRatioLow*dRatioHigh,是需要指定的。這些參數(shù)會影響分割后邊界點數(shù)目的多少**************************************************************************/voidCanny(unsignedchar*pUnchImage,intnWidth,intnHeight,doublesigma,doubledRatioLo
3、w,doubledRatioHigh,unsignedchar*pUnchEdge){//經(jīng)過高斯濾波后的圖象數(shù)據(jù)unsignedchar*pUnchSmooth;//指向x方向?qū)?shù)的指針int*pnGradX;//指向y方向?qū)?shù)的指針int*pnGradY;//梯度的幅度int*pnGradMag;pUnchSmooth=newunsignedchar[nWidth*nHeight];pnGradX=newint[nWidth*nHeight];pnGradY=newint[nWidth*nHeight];pnGradMag=newint[nWidth*nHeight];//
4、對原圖象進行濾波GaussianSmooth(pUnchImage,nWidth,nHeight,sigma,pUnchSmooth);//計算方向?qū)?shù)DirGrad(pUnchSmooth,nWidth,nHeight,pnGradX,pnGradY);//計算梯度的幅度GradMagnitude(pnGradX,pnGradY,nWidth,nHeight,pnGradMag);//應(yīng)用non-maximum抑制NonmaxSuppress(pnGradMag,pnGradX,pnGradY,nWidth,nHeight,pUnchEdge);//應(yīng)用Hysteresis,
5、找到所有的邊界Hysteresis(pnGradMag,nWidth,nHeight,dRatioLow,dRatioHigh,pUnchEdge);//釋放內(nèi)存deletepnGradX;pnGradX=NULL;deletepnGradY;pnGradY=NULL;deletepnGradMag;pnGradMag=NULL;deletepUnchSmooth;pUnchSmooth=NULL;}voidGaussianSmooth(unsignedchar*pUnchImg,intnWidth,intnHeight,doublesigma,unsignedchar*pUn
6、chSmthdImg){//循環(huán)控制變量inty;intx;inti;//高斯濾波器的數(shù)組長度intnWindowSize;//窗口長度的1/2intnHalfLen;//一維高斯數(shù)據(jù)濾波器double*pdKernel;//高斯系數(shù)與圖象數(shù)據(jù)的點乘doubledDotMul;//高斯濾波系數(shù)的總和doubledWeightSum;//中間變量double*pdTmp;//分配內(nèi)存pdTmp=newdouble[nWidth*nHeight];//產(chǎn)生一維高斯數(shù)據(jù)濾波器//MakeGauss(sigma,&dKernel,&nWindowSize);MakeGauss(sigm
7、a,&pdKernel,&nWindowSize);//MakeGauss返回窗口的長度,利用此變量計算窗口的半長nHalfLen=nWindowSize/2;//x方向進行濾波for(y=0;y=0&&(i+x)