Ask Your Question
0

Working with PGM file. Why my generated PGM file has 3 channels (RGB) ?

asked 2020-04-24 00:17:41 -0600

raisa_ gravatar image

updated 2020-04-24 00:28:58 -0600

I tried to generate PGM file, with this :

ori = imread("images/apple.jpg");
cvtColor(ori, gray, COLOR_BGR2GRAY);    

vector<int> params;
params.push_back(IMWRITE_PXM_BINARY);
params.push_back(1); 

imwrite("images/apple1.pgm", gray, params);
pgm = imread("images/apple1.pgm");
imshow("PGM", pgm);

I could generate PGM file successfully and show it using imshow. However I noticed that my PGM file has 3 channels (RGB), which was unexpected since it's supposed to have just 1 channel per pixel.

I loaded the same file into GIMP and exported it into PGM format. When I tried to show it on OpenCV using imshow as well, it displayed the right channel (1 channel) comparison from GIMP and PGM from my code is below :

image description

Notice at bottom left of each image, number of channel. Any advice which part I'm missing from my PGM conversion ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-24 01:54:19 -0600

berak gravatar image

the default flag for imread() is IMREAD_COLOR , and will force 3 channels

if you saved it as single channel grayscale image, using

pgm = imread("images/apple1.pgm", IMREAD_GRAYSCALE);

should retrieve it correctly.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-04-24 00:17:41 -0600

Seen: 1,691 times

Last updated: Apr 24 '20