gpt4 book ai didi

c++ - 无法读取正确的 pgm 像素值

转载 作者:行者123 更新时间:2023-11-28 03:04:54 36 4
gpt4 key购买 nike

我有一个非常奇怪的错误,

所以我尝试通过将其像素值加载到数组中来读取 pgm 图像,我能够正确读取其版本、高度、宽度和最大可能像素值。然而,当我开始读取像素值时,我总是得到 0。(我知道它不是零,因为我可以在 matlab 中使用 imread 读取它,但必须在 c++ 中实现它,而且我不能使用 opencv 库。 .)

此外,当我像 NotePad++ 一样读取 pgm 文件时,前几行很好地表示了有关此图像的信息,但是,实际像素值不可读。我想知道我是否需要某种解析来读取 pgm 图像?它的版本是p5。

谢谢!

最佳答案

您必须有一个作业要解决,否则没有合理的理由实现 PGM 阅读器。

有两种不同的 PGM 格式:ASCII 和二进制。您似乎期待一个 ASCII PGM,但您拥有的是二进制。

查看规范:http://netpbm.sourceforge.net/doc/pgm.html

它说:

/1. A "magic number" for identifying the file type. A pgm image's magic number is the two characters "P5".

[…]

/9. A raster of Height rows, in order from top to bottom. Each row consists of Width gray values, in order from left to right. Each gray value is a number from 0 through Maxval, with 0 being black and Maxval being white. Each gray value is represented in pure binary by either 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes. The most significant byte is first.

您期望的格式在下面进一步描述为 Plain PGM 格式。它的魔数(Magic Number)是“P2”。

关于c++ - 无法读取正确的 pgm 像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993136/

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