[英]Inverting a PGM's Color Values Distorts the Image
我的目标是读入PGM图像并生成具有反转颜色值的图像。 但是当我放入这张图片时 ,我会收回这张图片 。 我在C中编程,在Windows 7(64位)上使用Eclipse和MinGW GCC。 为什么图像变得如此剧烈扭曲?
int complement(PGMImage *img) {
int i, j;
// set up new PGM to copy onto
PGMImage* comImg = (PGMImage*)malloc(sizeof(PGMImage));
(*comImg).width = (*img).width;
(*comImg).height = (*img).height;
// invert each pixel
for(i = 0; i <= (*img).width; i++) {
for(j = 0; j <= (*img).height; j++) {
// set inverted value for each new pixel
(*comImg).data[i][j].red = abs((*img).maxVal - (*img).data[i][j].red);
(*comImg).data[i][j].green = abs((*img).maxVal - (*img).data[i][j].green);
(*comImg).data[i][j].blue = abs((*img).maxVal - (*img).data[i][j].blue);
}
}
// save a copy of the complement image
save("C:\\image_complement.pgm", comImg);
printf("A copy of your image has been saved as \"C:\\image_complement.pgm\"\n\n");
void free(comImg);
return 0;
}
我不确定,但我怀疑你的图像的外部数组索引是行,内部是列。 因此,我希望i
应该运行到(*img).height
和j
到(*img).width
。
我认为<=
应该是<
在你的两个循环中。 通过修改后的图像,从评论到其他答案,它看起来就像是在每一行上“踩到边缘”。
嗯, pgm
是灰色图 ,对吧? “红色”“绿色”和“蓝色”是什么意思?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.