2014-12-07 79 views
0

我目前正在使用空间自相关算法来分析图像中的模式。它应该返回0到2之间的值。当我运行代码时,我得到了一些0值,但其余的都是数十亿(包括负值和正值)。我认为我的数学在某个地方出了问题,但我一直没有弄明白。以下是我正在使用的算法(Geary's C或Geary's Coefficient)的链接以及我一直在研究的代码。Geary's Coefficient算法获得不正确的结果(不正确的数学?)

http://faculty.salisbury.edu/~ajlembo/419/lecture15.pdf

 /* 
     CREATING WEIGHT MATRIX (QUEEN'S CASE) 
    */ 

    int wSize = 3; 
    int wHalf = (wSize - 1)/2; 
    double weight[3][3] = {{1,1,1},{1,1,1},{1,1,1}}; 

cout << "test2" << endl; 
    /* 
     APPLYING WEIGHT SUM FOR (NAME OF FUNCTION) 
    */ 
    unsigned char* c = new unsigned char[pixels*3]; 

cout << "test3" << endl; 
    for (int j = 0; j < columns; j++) { 
     for (int i = 0; i < lines; i++) { 
      int index = (i * lines + j) * 3; 
      double sum1R = 0, sum1G = 0, sum1B = 0; 
      double sum2R = 0, sum2G = 0, sum2B = 0; 
      double weightsum = 9; 
      for (int l = 0; l < wSize; l++) { 
       for (int k = 0; k < wSize; k++) { 
        int tempk = (k+j) - wHalf; 
        int templ = (l+i) - wHalf; 
        // making sure we are not out of bounds of the image 
        if((tempk > (columns - 1)) || (tempk < 0)){ 
         tempk = j; 
        } 
        if((templ > (lines - 1)) || (templ < 0)){ 
         templ = i; 
        } 
        int pos1 = (tempk*lines + 0) * 3; 
        int pos2 = (0 + templ) * 3; 
        int r1 = originalpixmap[pos1], g1 = originalpixmap[pos1+1], b1 = originalpixmap[pos1+2]; 
        int r2 = originalpixmap[pos2], g2 = originalpixmap[pos2+1], b2 = originalpixmap[pos2+2]; 
        sum1R += (weight[tempk][templ])*pow((r1-r2),2); 
        sum1G += (weight[tempk][templ])*pow((g1-g2),2); 
        sum1B += (weight[tempk][templ])*pow((b1-b2),2); 
       } 
      } 

      double meanSumR = 0, meanSumG = 0, meanSumB = 0; 
      for(int l = 0; l < wSize; l++){ 
       int templ = (l+i) - wHalf; 
       int pos = (0 + templ) * 3; 
       if((templ > (lines - 1)) || (templ < 0)){ 
        templ = i; 
       } 
       int r = originalpixmap[pos], g = originalpixmap[pos+1], b = originalpixmap[pos+2]; 

       meanSumR += r; 
       meanSumG += g; 
       meanSumB += b; 
      } 

      double meanR = meanSumR/wSize; 
      double meanG = meanSumG/wSize; 
      double meanB = meanSumB/wSize; 
      for(int l = 0; l < wSize; l++){ 
       int templ = (l+i) - wHalf; 
       int pos = (0 + templ) * 3; 
       if((templ > (lines - 1)) || (templ < 0)){ 
        templ = i; 
       } 
       int r = originalpixmap[pos], g = originalpixmap[pos+1], b = originalpixmap[pos+2]; 

       sum2R += pow((r-meanR),2); 
       sum2G += pow((g-meanG),2); 
       sum2B += pow((b-meanB),2); 
      } 

      double rval = ((pixels-1)/2)*((sum1R)/(weightsum*sum2R)); 
      double gval = ((pixels-1)/2)*((sum1G)/(weightsum*sum2G)); 
      double bval = ((pixels-1)/2)*((sum1B)/(weightsum*sum2B)); 
      c[index] = (int)rval; 
      c[index+1] = (int)gval; 
      c[index+2] = (int)bval; 
      cout<< "C.r: " << rval <<" C.g: "<< gval <<" C.b: " << bval << endl; 
     } 
    } 

} 
+0

任何调试器的用法?这就是你如何找到你错误的地方 - 使用调试器。 – PaulMcKenzie 2014-12-07 06:41:11

+0

我并不确定如何使用调试器,但在此课程之前,我从来没有任何编码经验。编译代码时没有任何错误,但返回的值不是他们应该的。 – 2014-12-07 07:03:06

+0

你使用什么编译器?如果是Visual Studio,调试器就像使用“调试”菜单一样简单。如果它是gcc,那么gdb就是调试器。底线是,如果你不熟悉交易工具,那么除了玩具程序之外,几乎不可能写任何东西。 – PaulMcKenzie 2014-12-07 07:21:45

回答

0

你的权重是double类型,但你sum1R,等等,都是int类型。也许sum1R等变量也应该是double

+0

这绝对改变了一些东西,但我仍然得到很多像'9.17528e + 272'这样的值以及大量零中的一些无穷大。我将把我的新代码放在上面。 – 2014-12-07 07:43:48

+1

您需要开始输出中间值以查看它们在每个循环的迭代中的含义。或者提供一个完整但小例子的数据。 – PaulMcKenzie 2014-12-07 07:53:30