簡體   English   中英

在C中將P2 .pgm圖像讀取到2D數組中

[英]Reading a P2 .pgm image into a 2D array in C

我已經開始學習圖像處理,並且試圖將.pgm文件讀入C的2D數組中。我正在測試輸入和輸出,但是該程序無法正常運行,因此,我嘗試進行一些更改。 如何改進/修改代碼以使其正常工作?

#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;
}

您的代碼看起來還不錯,唯一的問題是您僅在創建指針,而不是將其引用到內存中的實際結構。

您可以通過添加以下代碼行來做到這一點,一切都應按預期工作。

imginfo = malloc(sizeof(struct PGMstructure));

代碼的主要問題是MiteshNinja強調的問題,即您忘記為結構分配空間。
當您不再需要該結構時,請記住使用free函數free使用malloc分配的malloc 為了避免內存泄漏,這非常重要!

我想指出的另一件事是,每當處理fclose一個文件時,都應該記住使用fclose將其關閉。

然后,您可以在下面的代碼中找到許多瑣碎的問題。

最后,也許這篇文章也可能有所幫助。

#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM