[英]Getting incorrect results for Geary's Coefficient algorithm (incorrect math?)
我目前正在開發一個使用空間自相關算法來分析圖像圖案的程序。 它應該返回0到2之間的值。當我運行代碼時,我得到了一些0值,但是其余的值都在數十億之內(負數和正數)。 我認為我的數學錯誤在某些地方,但我無法弄清楚。 以下是我正在使用的算法(Geary的C或Geary的系數)的鏈接,以及我一直在努力的代碼。
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;
}
}
}
您的權重為double
類型,但您的sum1R
等為int
類型。 也許sum1R
等,變量也應該是double
嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.