Question

I have a really weird error,

so I'm trying to read a pgm image by loading its pixel values into an array, I was able to correctly read in its version, height, width, and maximum possible pixel value. However, when I start reading the pixel values, I always get 0. (I know it's not zero because I can read it using imread in matlab, but have to implement it in c++, plus I couldn't use the opencv library so..)

And besides, when I read the pgm file in like NotePad++, the first few lines are good representing the information about this image ,how ever, the actual pixel values are not readable. I'm wondering if I need some sort of parsing to read a pgm image? Its version is p5.

Thanks!

Was it helpful?

Solution

You must have an assignment to solve as there is no sane reason implementing a PGM reader otherwise.

There are two different PGM formats: ASCII and binary. You seem to expect an ASCII PGM but the one you have is binary.

Have a look at the specs: http://netpbm.sourceforge.net/doc/pgm.html

It says:

/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.

The format you are expecting is described further down below as the Plain PGM format. Its magic number is "P2".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top