我目前正在使用空間自相關算法來分析圖像中的模式。它應該返回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;
}
}
}
任何調試器的用法?這就是你如何找到你錯誤的地方 - 使用調試器。 – PaulMcKenzie 2014-12-07 06:41:11
我並不確定如何使用調試器,但在此課程之前,我從來沒有任何編碼經驗。編譯代碼時沒有任何錯誤,但返回的值不是他們應該的。 – 2014-12-07 07:03:06
你使用什麼編譯器?如果是Visual Studio,調試器就像使用「調試」菜單一樣簡單。如果它是gcc,那麼gdb就是調試器。底線是,如果你不熟悉交易工具,那麼除了玩具程序之外,幾乎不可能寫任何東西。 – PaulMcKenzie 2014-12-07 07:21:45