C 使用PPM图像作为int*img

C 使用PPM图像作为int*img,c,char,int,mean,C,Char,Int,Mean,我一直在尝试使用灰度PPM图像和模板来实现MSF。我需要将图像声明为“*int”img,而不是unsigned char img。但是当我计算模板的零平均中心时,我得到的值没有任何意义。我尝试了unsigned char,它在零均值中心之前给出了正确的像素值,之后给出了错误的像素值,因为unsigned char没有足够的“空间”,不接受负值,但至少它的值是一致的。我知道这是一个愚蠢的错误,但我坚持到底。有人能帮助我如何使用int*实现它吗?谢谢 下面是我的部分代码和两个结果 FILE *fpt

我一直在尝试使用灰度PPM图像和模板来实现MSF。我需要将图像声明为“*int”img,而不是unsigned char img。但是当我计算模板的零平均中心时,我得到的值没有任何意义。我尝试了unsigned char,它在零均值中心之前给出了正确的像素值,之后给出了错误的像素值,因为unsigned char没有足够的“空间”,不接受负值,但至少它的值是一致的。我知道这是一个愚蠢的错误,但我坚持到底。有人能帮助我如何使用int*实现它吗?谢谢

下面是我的部分代码和两个结果

FILE *fpt1, *fpt2, *fpt3;
int *img, *gt, *int;
char    header[320], header2[320];
int COLS, ROWS, BYTES, COLS2, ROWS2, BYTES2, i, len;
int r , c, count, sum;

   /*Read template*/
fpt2 = fopen("parenthood_e_template.ppm", "r");
if (fpt2 == NULL){
    printf("Unable to open template of parenthood.ppm for reading\n");
    exit(0);
}
fscanf(fpt2,"%s %d %d %d\n", header2, &COLS2, &ROWS2, &BYTES2);
if (strcmp(header2, "P5") != 0 || BYTES2 != 255){
    printf("Not a template for parenthood.ppm\n");
    exit(0);
}

    /*Allocate memory for template*/
temp = (int*) calloc (COLS2*ROWS2, sizeof(int));

    /* Copy template into memory*/
header2[0] = fgetc(fpt2);
fread(temp,1,COLS2*ROWS2,fpt2);
fclose(fpt2);

              /* Matched Spatial-Filter */
    /* zero-mean the template*/
sum=0;
count=0;
for (r=0; r<ROWS2; r++) {
    for (c=0; c<COLS2; c++) {
        sum += temp[r*COLS2+c];
        printf("temp[%d]= %d\n", count, temp[r*COLS2+c]);
        count += 1;
    }
}

sum = sum/126;
和int的结果*

    temp[0]= -959986489
    temp[1]= -892876858
    temp[2]= -960248635
    temp[3]= -943209016
    temp[4]= -993737786
    temp[5]= -993606201
    temp[6]= -1010646588
    temp[7]= -1769628735
    temp[8]= -1010514757
    temp[9]= 1766157760
    temp[10]= -1028108712
    temp[11]= -1320513087
    temp[12]= 1768209085
    temp[13]= -1958166593
    temp[14]= -1464024897
    temp[15]= 733656878
    temp[16]= 1920101977
    temp[17]= -1078975125
    temp[18]= -2054848981
    temp[19]= -1148943991
    temp[20]= -1129892673
    temp[21]= -1010580544
    temp[22]= 1818148287
    temp[23]= -1077886531
    temp[24]= -1379811136
    temp[25]= -1313895119
    temp[26]= -943489684
    temp[27]= 1180345285
    temp[28]= -1078694581
    temp[29]= -1061042750
    temp[30]= -993805127
    temp[31]= 195
    temp[32]= 0
    temp[33]= 0
    temp[34]= 0
    temp[35]= 0
    temp[36]= 0
    temp[37]= 0
    temp[38]= 0
    temp[39]= 0
    temp[40]= 0
    temp[41]= 0
    temp[42]= 0
    temp[43]= 0
    temp[44]= 0
    temp[45]= 0
    temp[46]= 0
    temp[47]= 0
    temp[48]= 0
    temp[49]= 0
    temp[50]= 0
    temp[51]= 0
    temp[52]= 0
    temp[53]= 0
    temp[54]= 0
    temp[55]= 0
    temp[56]= 0
    temp[57]= 0
    temp[58]= 0
    temp[59]= 0
    temp[60]= 0
    temp[61]= 0
    temp[62]= 0
    temp[63]= 0
    temp[64]= 0
    temp[65]= 0
    temp[66]= 0
    temp[67]= 0
    temp[68]= 0
    temp[69]= 0
    temp[70]= 0
    temp[71]= 0
    temp[72]= 0
    temp[73]= 0
    temp[74]= 0
    temp[75]= 0
    temp[76]= 0
    temp[77]= 0
    temp[78]= 0
    temp[79]= 0
    temp[80]= 0
    temp[81]= 0
    temp[82]= 0
    temp[83]= 0
    temp[84]= 0
    temp[85]= 0
    temp[86]= 0
    temp[87]= 0
    temp[88]= 0
    temp[89]= 0
    temp[90]= 0
    temp[91]= 0
    temp[92]= 0
    temp[93]= 0
    temp[94]= 0
    temp[95]= 0
    temp[96]= 0
    temp[97]= 0
    temp[98]= 0
    temp[99]= 0
    temp[100]= 0
    temp[101]= 0
    temp[102]= 0
    temp[103]= 0
    temp[104]= 0
    temp[105]= 0
    temp[106]= 0
    temp[107]= 0
    temp[108]= 0
    temp[109]= 0
    temp[110]= 0
    temp[111]= 0
    temp[112]= 0
    temp[113]= 0
    temp[114]= 0
    temp[115]= 0
    temp[116]= 0
    temp[117]= 0
    temp[118]= 0
    temp[119]= 0
    temp[120]= 0
    temp[121]= 0
    temp[122]= 0
    temp[123]= 0
    temp[124]= 0
    temp[125]= 0
    C= 9  R= 14
    temp = -8605725   mean=8605725

unsigned char
首先应该是正确的类型。如果需要将其转换为更结构化的类型,则必须正确反序列化。注意:不要在C中强制转换
malloc
&friends的结果!您正在将4个
字节
像素读取为一个
int
像素。您的代码读取不正确。特别是,PPM用3字节或6字节表示每个像素,如您正在读取变量
BYTES2
的标题字段所示。我不知道有哪个系统的
int
是这两种宽度中的任何一种。此外,将像素读取为type
int
会设置字节顺序问题。您需要更丰富的数据类型来在内部表示PPM,需要更智能的代码来读取和处理PPM。为什么不仔细阅读(或使用)NetPBM自己的代码?这是开源的。@gre_gor是的。我还没意识到。但我如何才能为每个整数像素分配1字节的像素?谢谢
    temp[0]= -959986489
    temp[1]= -892876858
    temp[2]= -960248635
    temp[3]= -943209016
    temp[4]= -993737786
    temp[5]= -993606201
    temp[6]= -1010646588
    temp[7]= -1769628735
    temp[8]= -1010514757
    temp[9]= 1766157760
    temp[10]= -1028108712
    temp[11]= -1320513087
    temp[12]= 1768209085
    temp[13]= -1958166593
    temp[14]= -1464024897
    temp[15]= 733656878
    temp[16]= 1920101977
    temp[17]= -1078975125
    temp[18]= -2054848981
    temp[19]= -1148943991
    temp[20]= -1129892673
    temp[21]= -1010580544
    temp[22]= 1818148287
    temp[23]= -1077886531
    temp[24]= -1379811136
    temp[25]= -1313895119
    temp[26]= -943489684
    temp[27]= 1180345285
    temp[28]= -1078694581
    temp[29]= -1061042750
    temp[30]= -993805127
    temp[31]= 195
    temp[32]= 0
    temp[33]= 0
    temp[34]= 0
    temp[35]= 0
    temp[36]= 0
    temp[37]= 0
    temp[38]= 0
    temp[39]= 0
    temp[40]= 0
    temp[41]= 0
    temp[42]= 0
    temp[43]= 0
    temp[44]= 0
    temp[45]= 0
    temp[46]= 0
    temp[47]= 0
    temp[48]= 0
    temp[49]= 0
    temp[50]= 0
    temp[51]= 0
    temp[52]= 0
    temp[53]= 0
    temp[54]= 0
    temp[55]= 0
    temp[56]= 0
    temp[57]= 0
    temp[58]= 0
    temp[59]= 0
    temp[60]= 0
    temp[61]= 0
    temp[62]= 0
    temp[63]= 0
    temp[64]= 0
    temp[65]= 0
    temp[66]= 0
    temp[67]= 0
    temp[68]= 0
    temp[69]= 0
    temp[70]= 0
    temp[71]= 0
    temp[72]= 0
    temp[73]= 0
    temp[74]= 0
    temp[75]= 0
    temp[76]= 0
    temp[77]= 0
    temp[78]= 0
    temp[79]= 0
    temp[80]= 0
    temp[81]= 0
    temp[82]= 0
    temp[83]= 0
    temp[84]= 0
    temp[85]= 0
    temp[86]= 0
    temp[87]= 0
    temp[88]= 0
    temp[89]= 0
    temp[90]= 0
    temp[91]= 0
    temp[92]= 0
    temp[93]= 0
    temp[94]= 0
    temp[95]= 0
    temp[96]= 0
    temp[97]= 0
    temp[98]= 0
    temp[99]= 0
    temp[100]= 0
    temp[101]= 0
    temp[102]= 0
    temp[103]= 0
    temp[104]= 0
    temp[105]= 0
    temp[106]= 0
    temp[107]= 0
    temp[108]= 0
    temp[109]= 0
    temp[110]= 0
    temp[111]= 0
    temp[112]= 0
    temp[113]= 0
    temp[114]= 0
    temp[115]= 0
    temp[116]= 0
    temp[117]= 0
    temp[118]= 0
    temp[119]= 0
    temp[120]= 0
    temp[121]= 0
    temp[122]= 0
    temp[123]= 0
    temp[124]= 0
    temp[125]= 0
    C= 9  R= 14
    temp = -8605725   mean=8605725