我目前正致力于使用空间自相关算法分析图像中的图案的程序。它应该返回0到2之间的值。当我运行代码时,我得到了一些0值,但其余的值都是数十亿(负数和正数)。我认为我的数学在某个地方出了问题,但我还没有弄清楚。以下是我使用的算法的链接(Geary'或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 :(得分:0)
您的权重属于double
类型,但您的sum1R
等属于int
类型。也许sum1R
等变量也应该是double
?