2014-12-07 78 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