gpt4 book ai didi

c - 在 C 中将 P2 .pgm 图像读入二维数组

转载 作者:行者123 更新时间:2023-11-30 18:51:09 26 4
gpt4 key购买 nike

我已经开始学习图像处理,我正在尝试将 .pgm 文件读入 C 中的二维数组。我正在测试输入和输出,但程序无法正常工作,所以我尝试进行一些更改。我如何改进/修改代码以使其正常工作?

#include<stdio.h>
#include<stdlib.h>

struct PGMstructure
{
int maxVal;
int width;
int height;
int data[800][800];
};


int main()
{
FILE *imagein,*imageout;

int row, col;

int i,j;
int ch_int;
struct PGMstructure *imginfo;
char infpath[500],outfpath[500];

printf("Enter PGM file path:");
scanf("%s",infpath);
imagein = fopen(infpath,"r+");

if(imagein == NULL)
{
printf("Error opening first file");
exit(8);
}

while(getc(imagein) != '\n');

while (getc(imagein) == '#')
{
while (getc(imagein) != '\n');
}

fseek(imagein, -1, SEEK_CUR);
fscanf(imagein,"%d", &imginfo->width);
fscanf(imagein,"%d", &imginfo->height);
fscanf(imagein,"%d", &imginfo->maxVal);
printf("\n width = %d\n",imginfo->width);
printf("\n height = %d\n",imginfo->height);
printf("\n maxVal = %d\n",imginfo->maxVal);

for (row=0; row<imginfo->height; row++){

for (col=0; col < imginfo->width; col++)
{
fscanf(imagein,"%d", &ch_int);
imginfo->data[row][col] = ch_int;
}
}

printf("Enter path of output file:");

scanf("%s",outfpath);
imageout = fopen(outfpath,"w+");


for ( i = 0 ; i < row ; i++ )
{
for ( j = 0 ; j < col ; j++ )
{
fprintf( imageout,"%d" , imginfo->data[row][col] );
}
printf( "\n" ) ;
}

return 0;
}

最佳答案

代码的主要问题是 MiteshNinja 强调的问题,即您忘记为结构分配空间。
当您不再需要该结构时,请记住使用函数free释放通过malloc分配的内存。这对于避免内存泄漏非常重要!

我认为值得指出的另一件事是,每当您使用完一个文件时,您应该记住使用 fclose 关闭它。

然后您可以在下面的代码中找到许多小问题。

最后,也许this post也可能有帮助。

#include<stdio.h>
#include<stdlib.h>

struct PGMstructure
{
int maxVal;
int width;
int height;
int data[800][800];
};


int main()
{
FILE *imagein,*imageout;

int row, col;

int i,j;
int ch_int;
//--- CHANGED ------ Start
struct PGMstructure *imginfo = malloc(sizeof(struct PGMstructure));
//--- CHANGED ------ End
char infpath[500],outfpath[500];

printf("Enter PGM file path:");
scanf("%s",infpath);
imagein = fopen(infpath,"r+");

if(imagein == NULL)
{
printf("Error opening first file");
exit(8);
}

//--- CHANGED ------ Start
while(getc(imagein) != '\n'); // Ignore the first line in the input file

if (getc(imagein) == '#' ) // If it is the case, ignore the second line in the input file
{
while(getc(imagein) != '\n');
}
else
{
fseek(imagein, -1, SEEK_CUR);
}
//--- CHANGED ------ End

fscanf(imagein,"%d", &imginfo->width);
fscanf(imagein,"%d", &imginfo->height);
fscanf(imagein,"%d", &imginfo->maxVal);
printf("\n width = %d\n",imginfo->width);
printf("\n height = %d\n",imginfo->height);
printf("\n maxVal = %d\n",imginfo->maxVal);

for (row=0; row<imginfo->height; row++){

for (col=0; col < imginfo->width; col++)
{
fscanf(imagein,"%d", &ch_int);
imginfo->data[row][col] = ch_int;
}
}

//--- CHANGED ------ Start
fclose(imagein);
//--- CHANGED ------ End

printf("Enter path of output file:");

scanf("%s",outfpath);
imageout = fopen(outfpath,"w+");

//--- CHANGED ------ Start
for ( i = 0 ; i < imginfo->height ; i++ )
{
for ( j = 0 ; j < imginfo->width ; j++ )
{
fprintf( imageout,"%d " , imginfo->data[i][j] );
}
fprintf( imageout,"\n" );
}

fclose(imageout);
free(imginfo);
//--- CHANGED ------ End

return 0;
}

关于c - 在 C 中将 P2 .pgm 图像读入二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37909889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com