c - 如何从 .pgm 文件读取像素值并将其复制到数组中。 C语言编程

标签 c pgm

我已经被这个问题困扰了一段时间了。我如何将 pgm 文件中的像素值 0-255 复制到数组中?这是我到目前为止所拥有的。我知道我偏离了应有的样子。

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

int main(int argc, int *argv[])
{


      int i = 0, j = 0;
      unsigned int str[1000];
      FILE *pgmFile;

      if (argc != 2)
      {
          printf ("ERROR, Not enough arguments.");
          exit(EXIT_FAILURE);
      }

      if ((pgmFile = fopen(argv[1], "r")) == NULL)
      {
          printf ("ERROR, File cannot be opened");
          exit(EXIT_FAILURE);
      }

      do 
      {
          fscanf (pgmFile, "%d", str[i]);
          printf ("%d", str[i]);
          i++;
      }
      while (str[i] != EOF);



     fclose (pgmFile);

     return 0;
}

最佳答案

您应该首先阅读标题(3 行)。

使用标题中的数据,您可以设置 for 循环读取 Y 行数。在每一行中,您应该读取 X 数量的像素数据和换行符。如果任何读取函数返回错误代码,您应该打印出错误消息并中止。

https://en.wikipedia.org/wiki/Netpbm_format

关于c - 如何从 .pgm 文件读取像素值并将其复制到数组中。 C语言编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36972397/

相关文章:

python - 在 python 中使用 cv2 读取 pgm 图像

java - 尽快从 PGM 或 TIFF 创建 JavaFX 图像

Matlab interp1函数的C实现(线性插值)

c - 如何在文件中搜索以字符串开头的特定行

c++ - 如何生成循环展开的程序?

python - 在 PGM 格式文件中实现膨胀过滤器

c - 如何从 PGM 图像创建直方图

c - 未知类型名称 ‘caddr_t’ 错误

c - 连续发出的存储/加载是否以相同的顺序完成?

python - pgmpy 构建错误 Anaconda Windows