如果没有特别的剧本,我怎么能用C语言阅读原始图像呢



我正在做一个大学项目,我必须读取C中的原始图像,将值保存到矩阵中,然后应用高斯模糊,我认为我读错了,因为我在Win控制台上得到了一个5 x 5像素的原始图像:

228 228 228 228 228
228 228 228 228 228
228 228 228 228 228
228 228 228 228 228
228 228 228 228 228

这是我打印dinamic矩阵的时候,我在linux中的合作伙伴只得到了零,这里是我的代码:

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


int main()
{
    FILE *info_image, *image_raw;
    info_image = fopen("picture.inf","r");
    int **matriz_image, test;
    int i, j, rows, colums;

    //i read dimension image
    fscanf(info_image,"%i %i",&colums, &rows);

    //i create dinamic rows
    matriz_image = (int **) malloc (rows*sizeof(int*));
    //i create dinamic colums
    for(i=0;i<rows;i++)
    {
         matriz_image[i] = (int*) malloc (colums*sizeof(int)); 

    }
    //i open image raw
    image_raw = fopen("picture.raw","r");
    //i copy values to matriz_image
    for(i=0;i<rows;i++)
    {
        for(j=0;j<colums;j++)
        {
            //fscanf(image_raw,"%i",*(*(matriz_image+i)+j)); 
            fscanf(image_raw,"%i",&test);
            *(*(matriz_image+i)+j)=test;
            //printf("%i n", test); 
        }
    }

    //i print matriz
    for(i=0;i<rows;i++)
    {
        for(j=0;j<colums;j++)
        {
            printf("%i ",*(*(matriz_image+i)+j)); 
            //printf("%i ",matriz_image[i][j]); 

        }
        printf("n");
    }



    getch();

}

只要你不能用文本编辑器打开它,用fscanf()读取文件是不合理的。相反,您应该尝试fread()。此外,对于非纯文本文件,您应该使用模式"rb"打开文件。

image_raw = fopen("picture.raw", "rb");
for (i = 0; i < rows; ++i) {
    fread(matriz_image[i], sizeof(int), columns, image_raw);
}

最新更新