reading pgm images with cv2 in python

Liang Xiao picture Liang Xiao · Apr 5, 2016 · Viewed 9.3k times · Source

I want to read pgm image in python. I use cv2.imread('a.pgm') but it returns wrong results. In Matlab, I use imread and get the right result which is a single channel 16-bit image. But cv2.imread in python returns a 3-channel image and the pixel values are also wrong. Why it happens? How should I read the 16-bit pgm images in python? And what libraries?

Thanks in advance.

Answer

Liang Xiao picture Liang Xiao · Apr 6, 2016

I got it.

cv2.imread('a.pgm',-1) 

works.