看別人的代碼真的很辛苦,e文的資料,作者功底一般都比較nb,好在關鍵的地方總有些注釋的。國人更nb,看過得很多源碼根本就沒有注釋,只好自己邊理解邊寫注釋,費解死。
Mean Shift做映像分割的資料能找到的少之又少,看Rutgers上的論文,對我這個小本科來說還是有難度的。昨天上pudn淘了篇均值漂移分割映像的代碼,只能做灰階圖的,完全沒有注釋,想改一下卻有幾句看得不太懂,還沒注釋。。。
說明:下文注釋不一定對哦,我的極其有限的理解,而已。
//MeanShift 分割濾波演算法
//pImg 映像資料指標
//width height 寬度 高度
//bUseFilter true - filter false - image segmentation
//w - window size
// h1 space variance
// h2 range variance
void MeanShiftFilterOrSegmentation(bool bUseFilter,BYTE *pImg,int width,int height,int w,int h1,double h2,BYTE *pResImg)
{
BYTE *pCur,*CovG;
bool find,bOutOfImg;
int count,fwin = 2*w+1,top=-1,size=width*height;
int *G,*CovX,*CovY,x,y,i,j,new_x,new_y,new_g,old_x,old_y,old_g;
double sum,grayDif,*PosWeight,*pWCur,numerator_x,numerator_y,numerator_g,denominator,g;
//memory alloc
G = new int[width*height];
CovX = new int[width*height];
CovY = new int[width*height];
CovG = new BYTE[width*height];
PosWeight = new double[fwin*fwin];
//distance weight
// 根據到當前點的距離設定視窗鄰域內各點的權重
// weight = exp(-0.5*(distence^2)/(h1^2));
for(pWCur=PosWeight,sum=0.0,i=0;i<fwin;i++)
{
for(j=0;j<fwin;j++,pWCur++)
{
*pWCur = exp(-0.5 * ( (i-w)*(i-w)+(j-w)*(j-w) )/(h1*h1) );
}
}
//Iteration Until Convergence
// 漸進式掃描,y為行號,pCur指向映像資料
for(pCur=pImg,y=0;y<height;y++)
{
// 逐列掃描,x為列號
for(x=0;x<width;x++,pCur++)
{
// old_x紀錄當前像元列號
old_x = x;
// old_y記錄當前像元行號
old_y = y;
// old_g指向當前像元灰階值
old_g = *pCur;
// 初始化計數器
count=0;
// 掃描映像越界標誌,超出映像範圍時為true
bOutOfImg = false;
while(count<1000)
{
count++;
//
numerator_x = numerator_y = numerator_g = denominator = 0.0;
// 用i=old_y-w,i<old_y+w+1限定視窗行值範圍,pWCur指向視窗權重數組
for(pWCur=PosWeight,i=old_y-w;i<old_y+w+1;i++)
{
// 用j=old_x-w,j<old_x+w+1限定視窗列值範圍,當j++時掃描當前行的
// 下一個像元,同時pWCur指向權重數組中下一個值.
// 當掃描完視窗內當前行後,由於列值j不再滿足j<old_x+w+1條件,結束
// 本層迴圈,由i++將掃描像元推向視窗的下一行
for(j=old_x-w;j<old_x+w+1;j++,pWCur++)
{
//
if(i<0 || i>height-1 || j<0 || j>width-1 ) continue;
// *(pImg+i*width+j)是視窗內當前像元的灰階值,
// grayDif為視窗內當前像元灰階值與pCur指向的作為視窗中心的
// 當前點灰階值
grayDif = (*(pImg+i*width+j) - old_g);
// grayDif = grayDif^2;
grayDif = grayDif*grayDif;
// 用g表示當前視窗點與視窗中心樣本點的差異(帶權重)
// g為平移向量 ??????
// g為差異????
// exp(-0.5*grayDif/(h2*h2)) 高斯核???
// g = *pWCur*exp(-0.5*grayDif/(h2*h2));
// *pWCur為當前視窗位置的灰階值
g = *pWCur*exp(-0.5*grayDif/(h2*h2));
// numerator_x是列值平移向量的和
numerator_x += j*g;
// numerator_y是行值平移向量的和
numerator_y += i*g;
//
numerator_g += *(pImg+i*width+j)*g;
// denominator為平移向量(距離,權重~)的和
denominator += g;
}
}
new_x = (int)(numerator_x/denominator+0.5);
new_y = (int)(numerator_y/denominator+0.5);
new_g = (int)(numerator_g/denominator+0.5);
if(new_y<0 || new_y>height-1 || new_x<0 || new_x>width-1 )
{//超出映像範圍
new_x = old_x;
new_y = old_y;
new_g = old_g;
bOutOfImg = true;
break;
}
// 如果表單移動距離<1.1 ???
// 則結束迭代
if( abs(new_x-old_x) + abs(new_y-old_y) < 1.1) break;
// 變換視窗中心,繼續迭代
old_x = new_x;
old_y = new_y;
old_g = new_g;
}
if(count>999 ) printf("No Converge!/n");
if(bOutOfImg) printf("Out Of Image!/n");
// 將收斂點的列號(new_x),行號(new_y),灰階(new_g)賦給ConX,ConY,ConV中
// 的相應點
*(CovX + y*width + x) = new_x;
*(CovY + y*width + x) = new_y;
*(CovG + y*width + x) = new_g;
}
}
//僅用於映像分割
if(!bUseFilter)
{
for(i=0;i<size;i++)
{
for(find=false,j=0;j<=top;j++)
{
if( abs(*(CovG+i) - *(G+j))<h2 )
{
find = true;
*(CovG+i) = *(G+j);
break;
}
}
if(!find)
{
top++;
*(G+top) = *(CovG+i);
}
}
}
memcpy(pResImg,CovG,width*height);
delete G;
delete CovX;
delete CovY;
delete CovG;
delete PosWeight;
}
EDISON的分割流程有個大概的印象了,Filter裡面的一大塊比較費力,希望可以儘快搞得定。