[英]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.