Imread doesn't read PGM Image

I am struggling with this problem for several days now.
I want to read a .pgm Image from a TOF Camera with imread() but it just doesn’t.
There is no error or something. The cv::Mat just is empty when checked after with empty().
Every other kind of image and even other pgm from the internet work with my code.

The pgm Images from the camera are 16bit.
One could think that the images are just not compatible with opencv but the weird thing is that I have a Python Script that reads that same images with imread() without problems. For performance reasons I wanted to port the code to C++ but imread() doesn’t work anymore with these pgms.

The path is correct. I tried several versions of opencv. I reinstalled everything multiple times.
Nothing helped so far.

If you have any idea what I could try to get it to work, please tell me.
Thank you

May be you can zip image and give a link to zip file.

PGM is a strange old format. imread() may not support it. I’ll try to reproduce with the provided file on Windows 11 and OpenCV 4.9.0

I was able to read the file. there appears to be data.

>>> im = cv.imread("A.pgm", cv.IMREAD_UNCHANGED)
>>> im.shape
(240, 320)
>>> im.max()
>>> im.mean()
>>> cv.imshow("im", (im / im.max()) ** 0.45); cv.waitKey()


No problem

        Mat img = imread("c:/users/laurent/desktop/a.pgm");
        cout << img.rows;
        imshow("pgm", img*10);

Can you post getBuildInformation()?
My opencv information

  Media I/O:
    ZLib:                        optimized C:/install/zlib/lib/zlib.lib debug C:/install/zlib/lib/zlibd.lib (ver 1.2.13)
    JPEG:                        build-libjpeg-turbo (ver 2.1.3-62)
      SIMD Support Request:      YES
      SIMD Support:              NO
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         optimized C:/install/libpng/lib/libpng16.lib debug C:/install/libpng/lib/libpng16d.lib (ver 1.6.40.git)
    TIFF:                        build (ver 42 - 4.6.0)
    JPEG 2000:                   build (ver 2.5.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

my buildInformation gives this:
General configuration for OpenCV 4.5.3 =====================================
Version control: unknown

Timestamp: 2024-03-25T11:14:06Z
Host: Linux 5.15.133+ x86_64
CMake: 3.15.7
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: Release

CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
disabled: NEON
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (15 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (0 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (4 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (29 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

Built as dynamic libs?: NO
C++ standard: 17
C++ Compiler: /usr/local/bin/c++ (ver 11.1.0)
C++ flags (Release): -m64 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -m64 -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/local/bin/cc
C flags (Release): -m64 -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -m64 -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -m64 -Wl,–gc-sections -Wl,–as-needed
Linker flags (Debug): -m64 -Wl,–gc-sections -Wl,–as-needed
ccache: NO
Precompiled headers: NO
Extra dependencies: ade /home/conan/workspace/prod-v2/bsr/8120/bfffb/p/libwe0ab986b34fa07/p/lib/libwebp.a quirc::quirc dl m pthread rt
3rdparty dependencies:

OpenCV modules:
To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching video videoio
Disabled: python_tests world
Disabled by dependency: -
Unavailable: java python2 python3 ts
Applications: -
Documentation: NO
Non-free algorithms: NO

GTK+: YES (ver 2.24.30)
GThread : YES (ver 2.48.2)
GtkGlExt: NO

Media I/O:
ZLib: ZLIB::ZLIB (ver 1.3.1)
JPEG: (ver 90)
WEBP: /home/conan/workspace/prod-v2/bsr/8120/bfffb/p/libwe0ab986b34fa07/p/lib/libwebp.a (ver encoder: 0x020f)
PNG: (ver …)
TIFF: (ver 42 / 4.6.0)
JPEG 2000: (ver 4.2.0)
OpenEXR: OpenEXR::OpenEXR (ver 3.2.3)

Video I/O:
FFMPEG: YES (find_package)
avcodec: YES (58.134.100)
avformat: YES (58.76.100)
avutil: YES (56.70.100)
swscale: YES (5.9.100)
avresample: NO

Parallel framework: pthreads

Trace: YES (built-in)

Other third-party libraries:
Eigen: YES (ver …)
Custom HAL: NO
Protobuf: protobuf::libprotobuf (3.21.12)

Python (for build): NO

Install to: /home/conan/workspace/prod-v2/bsr/8120/bfffb/p/b/openc30b1a5ee1be59/p

PXM : NO means it’s compiled without pgm support i guess?
Then I don’t understand why other pgm images i downloaded worked.
But this helped me already. Thanks

I check which lib read your file in my configuration it is

In cmake configuration there is WITH_IMGCODEC_PXM Is it true in your config?

ppm images have a text header. can you show yours ?

i dont think, the pxm importer handles 16bit properly

any chance, you can debug this ?

that said, ppm is the most simple image format ever.
c++ ifstream, 20 lines of code or so …

No I don’t think code show is revelvant because in debug I reach this code

and later

1 Like