2017-03-26 37 views
-2

我已經爲圖像計算了chebyshev時刻,下面是主代碼。我有相同的圖像不同的結果,每次我運行我得到新的價值。這裏是功能和主要代碼。C++代碼中具有相同函數和參數的不同結果

 float calcul_ro(int p,int N) 
{int i; float ro_p=1,float_N=(float)N; 
if (p==0) 
    ro_p=(float)N; 
else { 
    for (i=1;i<=p;i++) 
    ro_p=ro_p*(1-((i*i)/(N*N))); 

ro_p=(ro_p*float_N)/(2*p+1); 
} 
return ro_p; 
} 
///______________________________________________________________________________/// 
float calcul_tp(int x,int p,int N,float tp_1,float tp_2) 
{float tp, float_N=(float)N; 
if (p==0) 
    tp=1; 
else if(p==1) 
    tp=2*x+1-N; 
else 
    { //tp=((2*p-1)*tp_1)-((p-1)*(1-(pow((float)(p-1),2)/pow(float_N,2)))*tp_2); 
     tp=((2*p-1)*tp_1)-(((p-1)*(1-((p-1)*(p-1))/(N*N)))*tp_2); 
    tp=tp/p;} 
return tp; 
} 
///______________________________________________________________________/// 
float *chebychev_moment(Mat image,int N) 
{int p,q,x=0,y=0,i=0,j,compt=0,hml=0; float rslt,alpha_p,alpha_q,beta_p,beta_q,ro_p=1,ro_q=1,tp,tq; 
float *vect=new float[55]; 
float tp_moins_1[100][100], tp_moins_2[100][100], tq_moins_1[100][100],tq_moins_2[100][100]; 
///****************************************************************** 
///initialisation de tp_moins_1 
for(i=0;i<100;i++) 
{ 
    for(j=0;j<100;j++) 
     tp_moins_1[i][j]=1; 
} 
///************************************************************************ 
for (p=0;p<9;p++) 
{ 
    for(i=0;i<100;i++) 
    { 
     for(j=0;j<100;j++) 
      tq_moins_1[i][j]=1; 
    } 
    for(q=0;q<=9-p;q++) 
    { 

    ro_p=calcul_ro(p,N); 
    ro_q=calcul_ro(q,N); 
    ///************************************ 
    for (x=0;x<image.rows;x++) 
     { y=0; 
      tp=calcul_tp(x,p,N, tp_moins_1[x][y], tp_moins_2[x][y]); 
      tp_moins_2[x][y]=tp_moins_1[x][y]; 
      tp_moins_1[x][y]=tp; 
       for(y=0;y<image.cols;y++) 
        { if(image.at<int>(x,y)!=0) 
        {tq=calcul_tp(y,q,N,tq_moins_1[x][y],tq_moins_2[x][y]); 
        tq_moins_2[x][y]=tq_moins_1[x][y]; 
        tq_moins_1[x][y]=tq; 
        rslt=rslt+tp*tq*image.at<int>(x,y);} 
        } 
     } 
///************************ 
    rslt=rslt*(1/(ro_p*ro_q)); 
    printf("rslt %d ,p=%d,q=%d, =%f \n",hml,p,q,rslt); 
    vect[compt]=rslt; 
    compt++; 
    rslt=0; 

}} 
return vect; 
} 
///______________________________________________________________________________/// 
int main() 
{ Mat image=imread("2_.png",CV_LOAD_IMAGE_GRAYSCALE); 
float *vect=new float[55], *vect2=new float[55]; 

vect=chebychev_moment(image,100); 
Mat image2=imread("2_.png",CV_LOAD_IMAGE_GRAYSCALE); 
vect2=chebychev_moment(image2,100); 

//function that copy the vector in a file 
write_on_file(vect,"vector_image3.txt"); 
write_on_file(vect2,"vector_image4.txt"); 
return 0;} 

有人能幫我嗎?

回答

1

看起來像rsltchebychev_moment被使用之前,它被設置。可能你忘了將它初始化爲0alpha_palpha_q,beta_p,beta_q根本不使用。

+0

謝謝你的幫助。我只是初始化爲0,但我有同樣的問題。 – azertq

+0

檢查您的其他代碼是否存在相同類型的錯誤。例如tp_moins_2和tq_moins_2也都在設置之前使用。 – yar

+0

我已經做到了,但仍然是同樣的問題。 – azertq

相關問題